fix: use testify instead of t.Fatal or t.Error in pkg package
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
parent
b197332b13
commit
cad3fce1a0
@ -19,6 +19,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -272,32 +273,18 @@ func TestIntervalTreeIntersects(t *testing.T) {
|
|||||||
ivt := NewIntervalTree()
|
ivt := NewIntervalTree()
|
||||||
ivt.Insert(NewStringInterval("1", "3"), 123)
|
ivt.Insert(NewStringInterval("1", "3"), 123)
|
||||||
|
|
||||||
if ivt.Intersects(NewStringPoint("0")) {
|
assert.Falsef(t, ivt.Intersects(NewStringPoint("0")), "contains 0")
|
||||||
t.Errorf("contains 0")
|
assert.Truef(t, ivt.Intersects(NewStringPoint("1")), "missing 1")
|
||||||
}
|
assert.Truef(t, ivt.Intersects(NewStringPoint("11")), "missing 11")
|
||||||
if !ivt.Intersects(NewStringPoint("1")) {
|
assert.Truef(t, ivt.Intersects(NewStringPoint("2")), "missing 2")
|
||||||
t.Errorf("missing 1")
|
assert.Falsef(t, ivt.Intersects(NewStringPoint("3")), "contains 3")
|
||||||
}
|
|
||||||
if !ivt.Intersects(NewStringPoint("11")) {
|
|
||||||
t.Errorf("missing 11")
|
|
||||||
}
|
|
||||||
if !ivt.Intersects(NewStringPoint("2")) {
|
|
||||||
t.Errorf("missing 2")
|
|
||||||
}
|
|
||||||
if ivt.Intersects(NewStringPoint("3")) {
|
|
||||||
t.Errorf("contains 3")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntervalTreeStringAffine(t *testing.T) {
|
func TestIntervalTreeStringAffine(t *testing.T) {
|
||||||
ivt := NewIntervalTree()
|
ivt := NewIntervalTree()
|
||||||
ivt.Insert(NewStringAffineInterval("8", ""), 123)
|
ivt.Insert(NewStringAffineInterval("8", ""), 123)
|
||||||
if !ivt.Intersects(NewStringAffinePoint("9")) {
|
assert.Truef(t, ivt.Intersects(NewStringAffinePoint("9")), "missing 9")
|
||||||
t.Errorf("missing 9")
|
assert.Falsef(t, ivt.Intersects(NewStringAffinePoint("7")), "contains 7")
|
||||||
}
|
|
||||||
if ivt.Intersects(NewStringAffinePoint("7")) {
|
|
||||||
t.Errorf("contains 7")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntervalTreeStab(t *testing.T) {
|
func TestIntervalTreeStab(t *testing.T) {
|
||||||
@ -310,27 +297,13 @@ func TestIntervalTreeStab(t *testing.T) {
|
|||||||
|
|
||||||
tr := ivt.(*intervalTree)
|
tr := ivt.(*intervalTree)
|
||||||
require.Equalf(t, 0, tr.root.max.Compare(StringComparable("8")), "wrong root max got %v, expected 8", tr.root.max)
|
require.Equalf(t, 0, tr.root.max.Compare(StringComparable("8")), "wrong root max got %v, expected 8", tr.root.max)
|
||||||
if x := len(ivt.Stab(NewStringPoint("0"))); x != 3 {
|
assert.Len(t, ivt.Stab(NewStringPoint("0")), 3)
|
||||||
t.Errorf("got %d, expected 3", x)
|
assert.Len(t, ivt.Stab(NewStringPoint("1")), 2)
|
||||||
}
|
assert.Len(t, ivt.Stab(NewStringPoint("2")), 1)
|
||||||
if x := len(ivt.Stab(NewStringPoint("1"))); x != 2 {
|
assert.Empty(t, ivt.Stab(NewStringPoint("3")))
|
||||||
t.Errorf("got %d, expected 2", x)
|
assert.Len(t, ivt.Stab(NewStringPoint("5")), 1)
|
||||||
}
|
assert.Len(t, ivt.Stab(NewStringPoint("55")), 1)
|
||||||
if x := len(ivt.Stab(NewStringPoint("2"))); x != 1 {
|
assert.Len(t, ivt.Stab(NewStringPoint("6")), 1)
|
||||||
t.Errorf("got %d, expected 1", x)
|
|
||||||
}
|
|
||||||
if x := len(ivt.Stab(NewStringPoint("3"))); x != 0 {
|
|
||||||
t.Errorf("got %d, expected 0", x)
|
|
||||||
}
|
|
||||||
if x := len(ivt.Stab(NewStringPoint("5"))); x != 1 {
|
|
||||||
t.Errorf("got %d, expected 1", x)
|
|
||||||
}
|
|
||||||
if x := len(ivt.Stab(NewStringPoint("55"))); x != 1 {
|
|
||||||
t.Errorf("got %d, expected 1", x)
|
|
||||||
}
|
|
||||||
if x := len(ivt.Stab(NewStringPoint("6"))); x != 1 {
|
|
||||||
t.Errorf("got %d, expected 1", x)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type xy struct {
|
type xy struct {
|
||||||
@ -368,15 +341,11 @@ func TestIntervalTreeRandom(t *testing.T) {
|
|||||||
require.NotEmptyf(t, ivt.Stab(NewInt64Point(v)), "expected %v stab non-zero for [%+v)", v, xy)
|
require.NotEmptyf(t, ivt.Stab(NewInt64Point(v)), "expected %v stab non-zero for [%+v)", v, xy)
|
||||||
require.Truef(t, ivt.Intersects(NewInt64Point(v)), "did not get %d as expected for [%+v)", v, xy)
|
require.Truef(t, ivt.Intersects(NewInt64Point(v)), "did not get %d as expected for [%+v)", v, xy)
|
||||||
}
|
}
|
||||||
if !ivt.Delete(NewInt64Interval(ab.x, ab.y)) {
|
assert.Truef(t, ivt.Delete(NewInt64Interval(ab.x, ab.y)), "did not delete %v as expected", ab)
|
||||||
t.Errorf("did not delete %v as expected", ab)
|
|
||||||
}
|
|
||||||
delete(ivs, ab)
|
delete(ivs, ab)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ivt.Len() != 0 {
|
assert.Equalf(t, 0, ivt.Len(), "got ivt.Len() = %v, expected 0", ivt.Len())
|
||||||
t.Errorf("got ivt.Len() = %v, expected 0", ivt.Len())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestIntervalTreeSortedVisit tests that intervals are visited in sorted order.
|
// TestIntervalTreeSortedVisit tests that intervals are visited in sorted order.
|
||||||
@ -417,17 +386,13 @@ func TestIntervalTreeSortedVisit(t *testing.T) {
|
|||||||
last := tt.ivls[0].Begin
|
last := tt.ivls[0].Begin
|
||||||
count := 0
|
count := 0
|
||||||
chk := func(iv *IntervalValue) bool {
|
chk := func(iv *IntervalValue) bool {
|
||||||
if last.Compare(iv.Ivl.Begin) > 0 {
|
assert.LessOrEqualf(t, last.Compare(iv.Ivl.Begin), 0, "#%d: expected less than %d, got interval %+v", i, last, iv.Ivl)
|
||||||
t.Errorf("#%d: expected less than %d, got interval %+v", i, last, iv.Ivl)
|
|
||||||
}
|
|
||||||
last = iv.Ivl.Begin
|
last = iv.Ivl.Begin
|
||||||
count++
|
count++
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
ivt.Visit(tt.visitRange, chk)
|
ivt.Visit(tt.visitRange, chk)
|
||||||
if count != len(tt.ivls) {
|
assert.Lenf(t, tt.ivls, count, "#%d: did not cover all intervals. expected %d, got %d", i, len(tt.ivls), count)
|
||||||
t.Errorf("#%d: did not cover all intervals. expected %d, got %d", i, len(tt.ivls), count)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,9 +433,7 @@ func TestIntervalTreeVisitExit(t *testing.T) {
|
|||||||
count++
|
count++
|
||||||
return tt.f(n)
|
return tt.f(n)
|
||||||
})
|
})
|
||||||
if count != tt.wcount {
|
assert.Equalf(t, count, tt.wcount, "#%d: expected count %d, got %d", i, tt.wcount, count)
|
||||||
t.Errorf("#%d: expected count %d, got %d", i, tt.wcount, count)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,8 +493,7 @@ func TestIntervalTreeContains(t *testing.T) {
|
|||||||
for _, ivl := range tt.ivls {
|
for _, ivl := range tt.ivls {
|
||||||
ivt.Insert(ivl, struct{}{})
|
ivt.Insert(ivl, struct{}{})
|
||||||
}
|
}
|
||||||
if v := ivt.Contains(tt.chkIvl); v != tt.wContains {
|
v := ivt.Contains(tt.chkIvl)
|
||||||
t.Errorf("#%d: ivt.Contains got %v, expected %v", i, v, tt.wContains)
|
assert.Equalf(t, v, tt.wContains, "#%d: ivt.Contains got %v, expected %v", i, v, tt.wContains)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,38 +22,22 @@ func TestHash32(t *testing.T) {
|
|||||||
// create a new hash with stdhash.Sum32() as initial crc
|
// create a new hash with stdhash.Sum32() as initial crc
|
||||||
hash := New(stdhash.Sum32(), crc32.IEEETable)
|
hash := New(stdhash.Sum32(), crc32.IEEETable)
|
||||||
|
|
||||||
wsize := stdhash.Size()
|
assert.Equalf(t, hash.Size(), stdhash.Size(), "size")
|
||||||
if g := hash.Size(); g != wsize {
|
assert.Equalf(t, hash.BlockSize(), stdhash.BlockSize(), "block size")
|
||||||
t.Errorf("size = %d, want %d", g, wsize)
|
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32")
|
||||||
}
|
|
||||||
wbsize := stdhash.BlockSize()
|
|
||||||
if g := hash.BlockSize(); g != wbsize {
|
|
||||||
t.Errorf("block size = %d, want %d", g, wbsize)
|
|
||||||
}
|
|
||||||
wsum32 := stdhash.Sum32()
|
|
||||||
if g := hash.Sum32(); g != wsum32 {
|
|
||||||
t.Errorf("Sum32 = %d, want %d", g, wsum32)
|
|
||||||
}
|
|
||||||
wsum := stdhash.Sum(make([]byte, 32))
|
wsum := stdhash.Sum(make([]byte, 32))
|
||||||
if g := hash.Sum(make([]byte, 32)); !reflect.DeepEqual(g, wsum) {
|
g := hash.Sum(make([]byte, 32))
|
||||||
t.Errorf("sum = %v, want %v", g, wsum)
|
assert.Truef(t, reflect.DeepEqual(g, wsum), "sum")
|
||||||
}
|
|
||||||
|
|
||||||
// write something
|
// write something
|
||||||
_, err = stdhash.Write([]byte("test data"))
|
_, err = stdhash.Write([]byte("test data"))
|
||||||
require.NoErrorf(t, err, "unexpected write error: %v", err)
|
require.NoErrorf(t, err, "unexpected write error: %v", err)
|
||||||
_, err = hash.Write([]byte("test data"))
|
_, err = hash.Write([]byte("test data"))
|
||||||
require.NoErrorf(t, err, "unexpected write error: %v", err)
|
require.NoErrorf(t, err, "unexpected write error: %v", err)
|
||||||
wsum32 = stdhash.Sum32()
|
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32 after write")
|
||||||
if g := hash.Sum32(); g != wsum32 {
|
|
||||||
t.Errorf("Sum32 after write = %d, want %d", g, wsum32)
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset
|
// reset
|
||||||
stdhash.Reset()
|
stdhash.Reset()
|
||||||
hash.Reset()
|
hash.Reset()
|
||||||
wsum32 = stdhash.Sum32()
|
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32 after reset")
|
||||||
if g := hash.Sum32(); g != wsum32 {
|
|
||||||
t.Errorf("Sum32 after reset = %d, want %d", g, wsum32)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
"go.uber.org/zap/zaptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -213,16 +214,13 @@ func TestFeatureGateFlag(t *testing.T) {
|
|||||||
|
|
||||||
err := fs.Parse([]string{fmt.Sprintf("--%s=%s", defaultFlagName, test.arg)})
|
err := fs.Parse([]string{fmt.Sprintf("--%s=%s", defaultFlagName, test.arg)})
|
||||||
if test.parseError != "" {
|
if test.parseError != "" {
|
||||||
if !strings.Contains(err.Error(), test.parseError) {
|
assert.Containsf(t, err.Error(), test.parseError, "%d: Parse() Expected %v, Got %v", i, test.parseError, err)
|
||||||
t.Errorf("%d: Parse() Expected %v, Got %v", i, test.parseError, err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
t.Errorf("%d: Parse() Expected nil, Got %v", i, err)
|
t.Errorf("%d: Parse() Expected nil, Got %v", i, err)
|
||||||
}
|
}
|
||||||
for k, v := range test.expect {
|
for k, v := range test.expect {
|
||||||
if actual := f.enabled.Load().(map[Feature]bool)[k]; actual != v {
|
actual := f.enabled.Load().(map[Feature]bool)[k]
|
||||||
t.Errorf("%d: expected %s=%v, Got %v", i, k, v, actual)
|
assert.Equalf(t, actual, v, "%d: expected %s=%v, Got %v", i, k, v, actual)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -240,20 +238,12 @@ func TestFeatureGateOverride(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
f.Set("TestAlpha=true,TestBeta=true")
|
f.Set("TestAlpha=true,TestBeta=true")
|
||||||
if f.Enabled(testAlphaGate) != true {
|
assert.Truef(t, f.Enabled(testAlphaGate), "Expected true")
|
||||||
t.Errorf("Expected true")
|
assert.Truef(t, f.Enabled(testBetaGate), "Expected true")
|
||||||
}
|
|
||||||
if f.Enabled(testBetaGate) != true {
|
|
||||||
t.Errorf("Expected true")
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Set("TestAlpha=false")
|
f.Set("TestAlpha=false")
|
||||||
if f.Enabled(testAlphaGate) != false {
|
assert.Falsef(t, f.Enabled(testAlphaGate), "Expected false")
|
||||||
t.Errorf("Expected false")
|
assert.Truef(t, f.Enabled(testBetaGate), "Expected true")
|
||||||
}
|
|
||||||
if f.Enabled(testBetaGate) != true {
|
|
||||||
t.Errorf("Expected true")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFeatureGateFlagDefaults(t *testing.T) {
|
func TestFeatureGateFlagDefaults(t *testing.T) {
|
||||||
@ -268,12 +258,8 @@ func TestFeatureGateFlagDefaults(t *testing.T) {
|
|||||||
testBetaGate: {Default: true, PreRelease: Beta},
|
testBetaGate: {Default: true, PreRelease: Beta},
|
||||||
})
|
})
|
||||||
|
|
||||||
if f.Enabled(testAlphaGate) != false {
|
assert.Falsef(t, f.Enabled(testAlphaGate), "Expected false")
|
||||||
t.Errorf("Expected false")
|
assert.Truef(t, f.Enabled(testBetaGate), "Expected true")
|
||||||
}
|
|
||||||
if f.Enabled(testBetaGate) != true {
|
|
||||||
t.Errorf("Expected true")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFeatureGateKnownFeatures(t *testing.T) {
|
func TestFeatureGateKnownFeatures(t *testing.T) {
|
||||||
@ -411,9 +397,8 @@ func TestFeatureGateSetFromMap(t *testing.T) {
|
|||||||
t.Errorf("%d: SetFromMap(%#v) Expected success, Got err:%v", i, test.setmap, err)
|
t.Errorf("%d: SetFromMap(%#v) Expected success, Got err:%v", i, test.setmap, err)
|
||||||
}
|
}
|
||||||
for k, v := range test.expect {
|
for k, v := range test.expect {
|
||||||
if actual := f.Enabled(k); actual != v {
|
actual := f.Enabled(k)
|
||||||
t.Errorf("%d: SetFromMap(%#v) Expected %s=%v, Got %s=%v", i, test.setmap, k, v, k, actual)
|
assert.Equalf(t, actual, v, "%d: SetFromMap(%#v) Expected %s=%v, Got %s=%v", i, test.setmap, k, v, k, actual)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -467,9 +452,7 @@ func TestFeatureGateString(t *testing.T) {
|
|||||||
f.Add(featuremap)
|
f.Add(featuremap)
|
||||||
f.SetFromMap(test.setmap)
|
f.SetFromMap(test.setmap)
|
||||||
result := f.String()
|
result := f.String()
|
||||||
if result != test.expect {
|
assert.Equalf(t, result, test.expect, "%d: SetFromMap(%#v) Expected %s, Got %s", i, test.setmap, test.expect, result)
|
||||||
t.Errorf("%d: SetFromMap(%#v) Expected %s, Got %s", i, test.setmap, test.expect, result)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -477,128 +460,90 @@ func TestFeatureGateString(t *testing.T) {
|
|||||||
func TestFeatureGateOverrideDefault(t *testing.T) {
|
func TestFeatureGateOverrideDefault(t *testing.T) {
|
||||||
t.Run("overrides take effect", func(t *testing.T) {
|
t.Run("overrides take effect", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{
|
err := f.Add(map[Feature]FeatureSpec{
|
||||||
"TestFeature1": {Default: true},
|
"TestFeature1": {Default: true},
|
||||||
"TestFeature2": {Default: false},
|
"TestFeature2": {Default: false},
|
||||||
}); err != nil {
|
})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, f.OverrideDefault("TestFeature1", false))
|
||||||
if err := f.OverrideDefault("TestFeature1", false); err != nil {
|
require.NoError(t, f.OverrideDefault("TestFeature2", true))
|
||||||
t.Fatal(err)
|
assert.Falsef(t, f.Enabled("TestFeature1"), "expected TestFeature1 to have effective default of false")
|
||||||
}
|
assert.Truef(t, f.Enabled("TestFeature2"), "expected TestFeature2 to have effective default of true")
|
||||||
if err := f.OverrideDefault("TestFeature2", true); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if f.Enabled("TestFeature1") {
|
|
||||||
t.Error("expected TestFeature1 to have effective default of false")
|
|
||||||
}
|
|
||||||
if !f.Enabled("TestFeature2") {
|
|
||||||
t.Error("expected TestFeature2 to have effective default of true")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("overrides are preserved across deep copies", func(t *testing.T) {
|
t.Run("overrides are preserved across deep copies", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: false}}); err != nil {
|
err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: false}})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, f.OverrideDefault("TestFeature", true))
|
||||||
if err := f.OverrideDefault("TestFeature", true); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
fcopy := f.DeepCopy()
|
fcopy := f.DeepCopy()
|
||||||
if !fcopy.Enabled("TestFeature") {
|
assert.Truef(t, fcopy.Enabled("TestFeature"), "default override was not preserved by deep copy")
|
||||||
t.Error("default override was not preserved by deep copy")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("reflected in known features", func(t *testing.T) {
|
t.Run("reflected in known features", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {
|
err := f.Add(map[Feature]FeatureSpec{"TestFeature": {
|
||||||
Default: false,
|
Default: false,
|
||||||
PreRelease: Alpha,
|
PreRelease: Alpha,
|
||||||
}}); err != nil {
|
}})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, f.OverrideDefault("TestFeature", true))
|
||||||
if err := f.OverrideDefault("TestFeature", true); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var found bool
|
var found bool
|
||||||
for _, s := range f.KnownFeatures() {
|
for _, s := range f.KnownFeatures() {
|
||||||
if !strings.Contains(s, "TestFeature") {
|
if !strings.Contains(s, "TestFeature") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
found = true
|
found = true
|
||||||
if !strings.Contains(s, "default=true") {
|
assert.Containsf(t, s, "default=true", "expected override of default to be reflected in known feature description %q", s)
|
||||||
t.Errorf("expected override of default to be reflected in known feature description %q", s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Error("found no entry for TestFeature in known features")
|
|
||||||
}
|
}
|
||||||
|
assert.Truef(t, found, "found no entry for TestFeature in known features")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("may not change default for specs with locked defaults", func(t *testing.T) {
|
t.Run("may not change default for specs with locked defaults", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{
|
err := f.Add(map[Feature]FeatureSpec{
|
||||||
"LockedFeature": {
|
"LockedFeature": {
|
||||||
Default: true,
|
Default: true,
|
||||||
LockToDefault: true,
|
LockToDefault: true,
|
||||||
},
|
},
|
||||||
}); err != nil {
|
})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.Errorf(t, f.OverrideDefault("LockedFeature", false), "expected error when attempting to override the default for a feature with a locked default")
|
||||||
if f.OverrideDefault("LockedFeature", false) == nil {
|
assert.Errorf(t, f.OverrideDefault("LockedFeature", true), "expected error when attempting to override the default for a feature with a locked default")
|
||||||
t.Error("expected error when attempting to override the default for a feature with a locked default")
|
|
||||||
}
|
|
||||||
if f.OverrideDefault("LockedFeature", true) == nil {
|
|
||||||
t.Error("expected error when attempting to override the default for a feature with a locked default")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("does not supersede explicitly-set value", func(t *testing.T) {
|
t.Run("does not supersede explicitly-set value", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: true}}); err != nil {
|
err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: true}})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, f.OverrideDefault("TestFeature", false))
|
||||||
if err := f.OverrideDefault("TestFeature", false); err != nil {
|
require.NoError(t, f.SetFromMap(map[string]bool{"TestFeature": true}))
|
||||||
t.Fatal(err)
|
assert.Truef(t, f.Enabled("TestFeature"), "expected feature to be effectively enabled despite default override")
|
||||||
}
|
|
||||||
if err := f.SetFromMap(map[string]bool{"TestFeature": true}); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !f.Enabled("TestFeature") {
|
|
||||||
t.Error("expected feature to be effectively enabled despite default override")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("prevents re-registration of feature spec after overriding default", func(t *testing.T) {
|
t.Run("prevents re-registration of feature spec after overriding default", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.Add(map[Feature]FeatureSpec{
|
err := f.Add(map[Feature]FeatureSpec{
|
||||||
"TestFeature": {
|
"TestFeature": {
|
||||||
Default: true,
|
Default: true,
|
||||||
PreRelease: Alpha,
|
PreRelease: Alpha,
|
||||||
},
|
},
|
||||||
}); err != nil {
|
})
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, f.OverrideDefault("TestFeature", false))
|
||||||
if err := f.OverrideDefault("TestFeature", false); err != nil {
|
err = f.Add(map[Feature]FeatureSpec{
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := f.Add(map[Feature]FeatureSpec{
|
|
||||||
"TestFeature": {
|
"TestFeature": {
|
||||||
Default: true,
|
Default: true,
|
||||||
PreRelease: Alpha,
|
PreRelease: Alpha,
|
||||||
},
|
},
|
||||||
}); err == nil {
|
})
|
||||||
t.Error("expected re-registration to return a non-nil error after overriding its default")
|
assert.Errorf(t, err, "expected re-registration to return a non-nil error after overriding its default")
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("does not allow override for an unknown feature", func(t *testing.T) {
|
t.Run("does not allow override for an unknown feature", func(t *testing.T) {
|
||||||
f := New("test", zaptest.NewLogger(t))
|
f := New("test", zaptest.NewLogger(t))
|
||||||
if err := f.OverrideDefault("TestFeature", true); err == nil {
|
err := f.OverrideDefault("TestFeature", true)
|
||||||
t.Error("expected an error to be returned in attempt to override default for unregistered feature")
|
assert.Errorf(t, err, "expected an error to be returned in attempt to override default for unregistered feature")
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("returns error if already added to flag set", func(t *testing.T) {
|
t.Run("returns error if already added to flag set", func(t *testing.T) {
|
||||||
@ -606,8 +551,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
|
|||||||
fs := flag.NewFlagSet("test", flag.ContinueOnError)
|
fs := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||||
f.AddFlag(fs, defaultFlagName)
|
f.AddFlag(fs, defaultFlagName)
|
||||||
|
|
||||||
if err := f.OverrideDefault("TestFeature", true); err == nil {
|
err := f.OverrideDefault("TestFeature", true)
|
||||||
t.Error("expected a non-nil error to be returned")
|
assert.Errorf(t, err, "expected a non-nil error to be returned")
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
"go.uber.org/zap/zaptest"
|
||||||
)
|
)
|
||||||
@ -33,9 +34,7 @@ func TestSetFlagsFromEnv(t *testing.T) {
|
|||||||
// flags should be settable using env vars
|
// flags should be settable using env vars
|
||||||
t.Setenv("ETCD_A", "foo")
|
t.Setenv("ETCD_A", "foo")
|
||||||
// and command-line flags
|
// and command-line flags
|
||||||
if err := fs.Set("b", "bar"); err != nil {
|
require.NoError(t, fs.Set("b", "bar"))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// first verify that flags are as expected before reading the env
|
// first verify that flags are as expected before reading the env
|
||||||
for f, want := range map[string]string{
|
for f, want := range map[string]string{
|
||||||
@ -47,17 +46,13 @@ func TestSetFlagsFromEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// now read the env and verify flags were updated as expected
|
// now read the env and verify flags were updated as expected
|
||||||
err := SetFlagsFromEnv(zaptest.NewLogger(t), "ETCD", fs)
|
require.NoError(t, SetFlagsFromEnv(zaptest.NewLogger(t), "ETCD", fs))
|
||||||
if err != nil {
|
|
||||||
t.Errorf("err=%v, want nil", err)
|
|
||||||
}
|
|
||||||
for f, want := range map[string]string{
|
for f, want := range map[string]string{
|
||||||
"a": "foo",
|
"a": "foo",
|
||||||
"b": "bar",
|
"b": "bar",
|
||||||
} {
|
} {
|
||||||
if got := fs.Lookup(f).Value.String(); got != want {
|
got := fs.Lookup(f).Value.String()
|
||||||
t.Errorf("flag %q=%q, want %q", f, got, want)
|
assert.Equalf(t, want, got, "flag %q=%q, want %q", f, got, want)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,9 +61,7 @@ func TestSetFlagsFromEnvBad(t *testing.T) {
|
|||||||
fs := flag.NewFlagSet("testing", flag.ExitOnError)
|
fs := flag.NewFlagSet("testing", flag.ExitOnError)
|
||||||
fs.Int("x", 0, "")
|
fs.Int("x", 0, "")
|
||||||
t.Setenv("ETCD_X", "not_a_number")
|
t.Setenv("ETCD_X", "not_a_number")
|
||||||
if err := SetFlagsFromEnv(zaptest.NewLogger(t), "ETCD", fs); err == nil {
|
assert.Error(t, SetFlagsFromEnv(zaptest.NewLogger(t), "ETCD", fs))
|
||||||
t.Errorf("err=nil, want != nil")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetFlagsFromEnvParsingError(t *testing.T) {
|
func TestSetFlagsFromEnvParsingError(t *testing.T) {
|
||||||
|
@ -16,6 +16,8 @@ package flags
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSelectiveStringValue(t *testing.T) {
|
func TestSelectiveStringValue(t *testing.T) {
|
||||||
@ -35,13 +37,9 @@ func TestSelectiveStringValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
sf := NewSelectiveStringValue(tt.vals...)
|
sf := NewSelectiveStringValue(tt.vals...)
|
||||||
if sf.v != tt.vals[0] {
|
assert.Equalf(t, sf.v, tt.vals[0], "#%d: want default val=%v,but got %v", i, tt.vals[0], sf.v)
|
||||||
t.Errorf("#%d: want default val=%v,but got %v", i, tt.vals[0], sf.v)
|
|
||||||
}
|
|
||||||
err := sf.Set(tt.val)
|
err := sf.Set(tt.val)
|
||||||
if tt.pass != (err == nil) {
|
assert.Equalf(t, tt.pass, (err == nil), "#%d: want pass=%t, but got err=%v", i, tt.pass, err)
|
||||||
t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +61,6 @@ func TestSelectiveStringsValue(t *testing.T) {
|
|||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
sf := NewSelectiveStringsValue(tt.vals...)
|
sf := NewSelectiveStringsValue(tt.vals...)
|
||||||
err := sf.Set(tt.val)
|
err := sf.Set(tt.val)
|
||||||
if tt.pass != (err == nil) {
|
assert.Equalf(t, tt.pass, (err == nil), "#%d: want pass=%t, but got err=%v", i, tt.pass, err)
|
||||||
t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,13 +56,9 @@ func TestUint32Value(t *testing.T) {
|
|||||||
err := val.Set(tc.s)
|
err := val.Set(tc.s)
|
||||||
|
|
||||||
if tc.expectError {
|
if tc.expectError {
|
||||||
if err == nil {
|
assert.Errorf(t, err, "Expected failure on parsing uint32 value from %s", tc.s)
|
||||||
t.Errorf("Expected failure on parsing uint32 value from %s", tc.s)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if err != nil {
|
require.NoErrorf(t, err, "Unexpected error when parsing %s: %v", tc.s, err)
|
||||||
t.Errorf("Unexpected error when parsing %s: %v", tc.s, err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, tc.expectedVal, uint32(val))
|
assert.Equal(t, tc.expectedVal, uint32(val))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -104,7 +104,7 @@ func TestUniqueURLsFromFlag(t *testing.T) {
|
|||||||
fs.Var(u, name, "usage")
|
fs.Var(u, name, "usage")
|
||||||
uss := UniqueURLsFromFlag(fs, name)
|
uss := UniqueURLsFromFlag(fs, name)
|
||||||
|
|
||||||
require.Equal(t, len(u.Values), len(uss))
|
require.Len(t, uss, len(u.Values))
|
||||||
|
|
||||||
um := make(map[string]struct{})
|
um := make(map[string]struct{})
|
||||||
for _, x := range uss {
|
for _, x := range uss {
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -40,9 +41,7 @@ func TestValidateURLsValueBad(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, in := range tests {
|
for i, in := range tests {
|
||||||
u := URLsValue{}
|
u := URLsValue{}
|
||||||
if err := u.Set(in); err == nil {
|
assert.Errorf(t, u.Set(in), `#%d: unexpected nil error for in=%q`, i, in)
|
||||||
t.Errorf(`#%d: unexpected nil error for in=%q`, i, in)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@ package httputil
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetHostname(t *testing.T) {
|
func TestGetHostname(t *testing.T) {
|
||||||
@ -43,8 +45,6 @@ func TestGetHostname(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i := range tt {
|
for i := range tt {
|
||||||
hv := GetHostname(tt[i].req)
|
hv := GetHostname(tt[i].req)
|
||||||
if hv != tt[i].host {
|
assert.Equalf(t, hv, tt[i].host, "#%d: %q expected host %q, got '%v'", i, tt[i].req.Host, tt[i].host, hv)
|
||||||
t.Errorf("#%d: %q expected host %q, got '%v'", i, tt[i].req.Host, tt[i].host, hv)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,30 +17,24 @@ package idutil
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewGenerator(t *testing.T) {
|
func TestNewGenerator(t *testing.T) {
|
||||||
g := NewGenerator(0x12, time.Unix(0, 0).Add(0x3456*time.Millisecond))
|
g := NewGenerator(0x12, time.Unix(0, 0).Add(0x3456*time.Millisecond))
|
||||||
id := g.Next()
|
id := g.Next()
|
||||||
wid := uint64(0x12000000345601)
|
wid := uint64(0x12000000345601)
|
||||||
if id != wid {
|
assert.Equalf(t, id, wid, "id = %x, want %x", id, wid)
|
||||||
t.Errorf("id = %x, want %x", id, wid)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewGeneratorUnique(t *testing.T) {
|
func TestNewGeneratorUnique(t *testing.T) {
|
||||||
g := NewGenerator(0, time.Time{})
|
g := NewGenerator(0, time.Time{})
|
||||||
id := g.Next()
|
id := g.Next()
|
||||||
// different server generates different ID
|
// different server generates different ID
|
||||||
g1 := NewGenerator(1, time.Time{})
|
assert.NotEqualf(t, id, NewGenerator(1, time.Time{}).Next(), "generate the same id %x using different server ID", id)
|
||||||
if gid := g1.Next(); id == gid {
|
|
||||||
t.Errorf("generate the same id %x using different server ID", id)
|
|
||||||
}
|
|
||||||
// restarted server generates different ID
|
// restarted server generates different ID
|
||||||
g2 := NewGenerator(0, time.Now())
|
assert.NotEqualf(t, id, NewGenerator(0, time.Now()).Next(), "generate the same id %x after restart", id)
|
||||||
if gid := g2.Next(); id == gid {
|
|
||||||
t.Errorf("generate the same id %x after restart", id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNext(t *testing.T) {
|
func TestNext(t *testing.T) {
|
||||||
@ -48,9 +42,7 @@ func TestNext(t *testing.T) {
|
|||||||
wid := uint64(0x12000000345601)
|
wid := uint64(0x12000000345601)
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
id := g.Next()
|
id := g.Next()
|
||||||
if id != wid+uint64(i) {
|
assert.Equalf(t, id, wid+uint64(i), "id = %x, want %x", id, wid+uint64(i))
|
||||||
t.Errorf("id = %x, want %x", id, wid+uint64(i))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,9 +32,7 @@ func TestPageWriterRandom(t *testing.T) {
|
|||||||
n := 0
|
n := 0
|
||||||
for i := 0; i < 4096; i++ {
|
for i := 0; i < 4096; i++ {
|
||||||
c, err := w.Write(buf[:rand.Intn(len(buf))])
|
c, err := w.Write(buf[:rand.Intn(len(buf))])
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
n += c
|
n += c
|
||||||
}
|
}
|
||||||
require.LessOrEqualf(t, cw.writeBytes, n, "wrote %d bytes to io.Writer, but only wrote %d bytes", cw.writeBytes, n)
|
require.LessOrEqualf(t, cw.writeBytes, n, "wrote %d bytes to io.Writer, but only wrote %d bytes", cw.writeBytes, n)
|
||||||
@ -53,26 +51,20 @@ func TestPageWriterPartialSlack(t *testing.T) {
|
|||||||
cw := &checkPageWriter{pageBytes: 64, t: t}
|
cw := &checkPageWriter{pageBytes: 64, t: t}
|
||||||
w := NewPageWriter(cw, pageBytes, 0)
|
w := NewPageWriter(cw, pageBytes, 0)
|
||||||
// put writer in non-zero page offset
|
// put writer in non-zero page offset
|
||||||
if _, err := w.Write(buf[:64]); err != nil {
|
_, err := w.Write(buf[:64])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, w.Flush())
|
||||||
if err := w.Flush(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes)
|
require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes)
|
||||||
// nearly fill buffer
|
// nearly fill buffer
|
||||||
if _, err := w.Write(buf[:1022]); err != nil {
|
_, err = w.Write(buf[:1022])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
// overflow buffer, but without enough to write as aligned
|
// overflow buffer, but without enough to write as aligned
|
||||||
if _, err := w.Write(buf[:8]); err != nil {
|
_, err = w.Write(buf[:8])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes)
|
require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes)
|
||||||
// finish writing slack space
|
// finish writing slack space
|
||||||
if _, err := w.Write(buf[:128]); err != nil {
|
_, err = w.Write(buf[:128])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
require.Equalf(t, 2, cw.writes, "got %d writes, expected 2", cw.writes)
|
require.Equalf(t, 2, cw.writes, "got %d writes, expected 2", cw.writes)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,21 +75,15 @@ func TestPageWriterOffset(t *testing.T) {
|
|||||||
buf := make([]byte, defaultBufferBytes)
|
buf := make([]byte, defaultBufferBytes)
|
||||||
cw := &checkPageWriter{pageBytes: 64, t: t}
|
cw := &checkPageWriter{pageBytes: 64, t: t}
|
||||||
w := NewPageWriter(cw, pageBytes, 0)
|
w := NewPageWriter(cw, pageBytes, 0)
|
||||||
if _, err := w.Write(buf[:64]); err != nil {
|
_, err := w.Write(buf[:64])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, w.Flush())
|
||||||
if err := w.Flush(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
require.Equalf(t, 64, w.pageOffset, "w.pageOffset expected 64, got %d", w.pageOffset)
|
require.Equalf(t, 64, w.pageOffset, "w.pageOffset expected 64, got %d", w.pageOffset)
|
||||||
|
|
||||||
w = NewPageWriter(cw, w.pageOffset, pageBytes)
|
w = NewPageWriter(cw, w.pageOffset, pageBytes)
|
||||||
if _, err := w.Write(buf[:64]); err != nil {
|
_, err = w.Write(buf[:64])
|
||||||
t.Fatal(err)
|
require.NoError(t, err)
|
||||||
}
|
require.NoError(t, w.Flush())
|
||||||
if err := w.Flush(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
require.Equalf(t, 0, w.pageOffset, "w.pageOffset expected 0, got %d", w.pageOffset)
|
require.Equalf(t, 0, w.pageOffset, "w.pageOffset expected 0, got %d", w.pageOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,9 @@ package ioutil
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLimitedBufferReaderRead(t *testing.T) {
|
func TestLimitedBufferReaderRead(t *testing.T) {
|
||||||
@ -24,10 +27,6 @@ func TestLimitedBufferReaderRead(t *testing.T) {
|
|||||||
ln := 1
|
ln := 1
|
||||||
lr := NewLimitedBufferReader(buf, ln)
|
lr := NewLimitedBufferReader(buf, ln)
|
||||||
n, err := lr.Read(make([]byte, 10))
|
n, err := lr.Read(make([]byte, 10))
|
||||||
if err != nil {
|
require.NoErrorf(t, err, "unexpected read error: %v", err)
|
||||||
t.Fatalf("unexpected read error: %v", err)
|
assert.Equalf(t, n, ln, "len(data read) = %d, want %d", n, ln)
|
||||||
}
|
|
||||||
if n != ln {
|
|
||||||
t.Errorf("len(data read) = %d, want %d", n, ln)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
"go.uber.org/zap/zaptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -136,14 +138,10 @@ func TestResolveTCPAddrs(t *testing.T) {
|
|||||||
urls, err := resolveTCPAddrs(ctx, zaptest.NewLogger(t), tt.urls)
|
urls, err := resolveTCPAddrs(ctx, zaptest.NewLogger(t), tt.urls)
|
||||||
cancel()
|
cancel()
|
||||||
if tt.hasError {
|
if tt.hasError {
|
||||||
if err == nil {
|
require.Errorf(t, err, "expected error")
|
||||||
t.Errorf("expected error")
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(urls, tt.expected) {
|
assert.Truef(t, reflect.DeepEqual(urls, tt.expected), "expected: %v, got %v", tt.expected, urls)
|
||||||
t.Errorf("expected: %v, got %v", tt.expected, urls)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,9 +306,7 @@ func TestURLsEqual(t *testing.T) {
|
|||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
result, err := urlsEqual(context.TODO(), zaptest.NewLogger(t), test.a, test.b)
|
result, err := urlsEqual(context.TODO(), zaptest.NewLogger(t), test.a, test.b)
|
||||||
if result != test.expect {
|
assert.Equalf(t, result, test.expect, "idx=%d #%d: a:%v b:%v, expected %v but %v", i, test.n, test.a, test.b, test.expect, result)
|
||||||
t.Errorf("idx=%d #%d: a:%v b:%v, expected %v but %v", i, test.n, test.a, test.b, test.expect, result)
|
|
||||||
}
|
|
||||||
if test.err != nil {
|
if test.err != nil {
|
||||||
if err.Error() != test.err.Error() {
|
if err.Error() != test.err.Error() {
|
||||||
t.Errorf("idx=%d #%d: err expected %v but %v", i, test.n, test.err, err)
|
t.Errorf("idx=%d #%d: err expected %v but %v", i, test.n, test.err, err)
|
||||||
@ -347,11 +343,7 @@ func TestURLStringsEqual(t *testing.T) {
|
|||||||
t.Logf("TestURLStringsEqual, case #%d", idx)
|
t.Logf("TestURLStringsEqual, case #%d", idx)
|
||||||
resolveTCPAddr = c.resolver
|
resolveTCPAddr = c.resolver
|
||||||
result, err := URLStringsEqual(context.TODO(), zaptest.NewLogger(t), c.urlsA, c.urlsB)
|
result, err := URLStringsEqual(context.TODO(), zaptest.NewLogger(t), c.urlsA, c.urlsB)
|
||||||
if !result {
|
assert.Truef(t, result, "unexpected result %v", result)
|
||||||
t.Errorf("unexpected result %v", result)
|
assert.NoErrorf(t, err, "unexpected error %v", err)
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,20 +16,20 @@
|
|||||||
|
|
||||||
package netutil
|
package netutil
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
func TestGetDefaultInterface(t *testing.T) {
|
func TestGetDefaultInterface(t *testing.T) {
|
||||||
ifc, err := GetDefaultInterfaces()
|
ifc, err := GetDefaultInterfaces()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Logf("default network interfaces: %+v\n", ifc)
|
t.Logf("default network interfaces: %+v\n", ifc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetDefaultHost(t *testing.T) {
|
func TestGetDefaultHost(t *testing.T) {
|
||||||
ip, err := GetDefaultHost()
|
ip, err := GetDefaultHost()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Logf("default ip: %v", ip)
|
t.Logf("default ip: %v", ip)
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
"go.uber.org/zap/zaptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,9 +30,7 @@ func init() { setDflSignal = func(syscall.Signal) {} }
|
|||||||
func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
|
func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
|
||||||
select {
|
select {
|
||||||
case s := <-c:
|
case s := <-c:
|
||||||
if s != sig {
|
require.Equalf(t, s, sig, "signal was %v, want %v", s, sig)
|
||||||
t.Fatalf("signal was %v, want %v", s, sig)
|
|
||||||
}
|
|
||||||
case <-time.After(1 * time.Second):
|
case <-time.After(1 * time.Second):
|
||||||
t.Fatalf("timeout waiting for %v", sig)
|
t.Fatalf("timeout waiting for %v", sig)
|
||||||
}
|
}
|
||||||
@ -54,12 +53,8 @@ func TestHandleInterrupts(t *testing.T) {
|
|||||||
waitSig(t, c, sig)
|
waitSig(t, c, sig)
|
||||||
waitSig(t, c, sig)
|
waitSig(t, c, sig)
|
||||||
|
|
||||||
if n == 3 {
|
require.NotEqualf(t, 3, n, "interrupt handlers were called in wrong order")
|
||||||
t.Fatalf("interrupt handlers were called in wrong order")
|
require.Equalf(t, 4, n, "interrupt handlers were not called properly")
|
||||||
}
|
|
||||||
if n != 4 {
|
|
||||||
t.Fatalf("interrupt handlers were not called properly")
|
|
||||||
}
|
|
||||||
// reset interrupt handlers
|
// reset interrupt handlers
|
||||||
interruptHandlers = interruptHandlers[:0]
|
interruptHandlers = interruptHandlers[:0]
|
||||||
interruptExitMu.Unlock()
|
interruptExitMu.Unlock()
|
||||||
|
@ -18,21 +18,20 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMarshaler(t *testing.T) {
|
func TestMarshaler(t *testing.T) {
|
||||||
data := []byte("test data")
|
data := []byte("test data")
|
||||||
m := &fakeMarshaler{data: data}
|
m := &fakeMarshaler{data: data}
|
||||||
if g := MustMarshal(m); !reflect.DeepEqual(g, data) {
|
g := MustMarshal(m)
|
||||||
t.Errorf("data = %s, want %s", g, m.data)
|
assert.Truef(t, reflect.DeepEqual(g, data), "data = %s, want %s", g, m.data)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshalerPanic(t *testing.T) {
|
func TestMarshalerPanic(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r == nil {
|
assert.NotNilf(t, recover(), "recover = nil, want error")
|
||||||
t.Errorf("recover = nil, want error")
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
m := &fakeMarshaler{err: errors.New("blah")}
|
m := &fakeMarshaler{err: errors.New("blah")}
|
||||||
MustMarshal(m)
|
MustMarshal(m)
|
||||||
@ -42,16 +41,12 @@ func TestUnmarshaler(t *testing.T) {
|
|||||||
data := []byte("test data")
|
data := []byte("test data")
|
||||||
m := &fakeUnmarshaler{}
|
m := &fakeUnmarshaler{}
|
||||||
MustUnmarshal(m, data)
|
MustUnmarshal(m, data)
|
||||||
if !reflect.DeepEqual(m.data, data) {
|
assert.Truef(t, reflect.DeepEqual(m.data, data), "data = %s, want %s", m.data, data)
|
||||||
t.Errorf("data = %s, want %s", m.data, data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalerPanic(t *testing.T) {
|
func TestUnmarshalerPanic(t *testing.T) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r == nil {
|
assert.NotNilf(t, recover(), "recover = nil, want error")
|
||||||
t.Errorf("recover = nil, want error")
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
m := &fakeUnmarshaler{err: errors.New("blah")}
|
m := &fakeUnmarshaler{err: errors.New("blah")}
|
||||||
MustUnmarshal(m, nil)
|
MustUnmarshal(m, nil)
|
||||||
@ -69,12 +64,8 @@ func TestGetBool(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
b, set := GetBool(tt.b)
|
b, set := GetBool(tt.b)
|
||||||
if b != tt.wb {
|
assert.Equalf(t, b, tt.wb, "#%d: value = %v, want %v", i, b, tt.wb)
|
||||||
t.Errorf("#%d: value = %v, want %v", i, b, tt.wb)
|
assert.Equalf(t, set, tt.wset, "#%d: set = %v, want %v", i, set, tt.wset)
|
||||||
}
|
|
||||||
if set != tt.wset {
|
|
||||||
t.Errorf("#%d: set = %v, want %v", i, set, tt.wset)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,14 +84,14 @@ func testServer(t *testing.T, scheme string, secure bool, delayTx bool) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer close(donec)
|
defer close(donec)
|
||||||
for data := range writec {
|
for data := range writec {
|
||||||
send(t, data, scheme, srcAddr, tlsInfo)
|
send(t, data, scheme, srcAddr, tlsInfo) //nolint:testifylint //FIXME
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
recvc := make(chan []byte, 1)
|
recvc := make(chan []byte, 1)
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
recvc <- receive(t, ln)
|
recvc <- receive(t, ln) //nolint:testifylint //FIXME
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -146,9 +146,7 @@ func testServer(t *testing.T, scheme string, secure bool, delayTx bool) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.Close(); err != nil {
|
require.NoError(t, p.Close())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-p.Done():
|
case <-p.Done():
|
||||||
@ -207,31 +205,25 @@ func testServerDelayAccept(t *testing.T, secure bool) {
|
|||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
send(t, data, scheme, srcAddr, tlsInfo)
|
send(t, data, scheme, srcAddr, tlsInfo)
|
||||||
if d := receive(t, ln); !bytes.Equal(data, d) {
|
d := receive(t, ln)
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
require.Truef(t, bytes.Equal(data, d), "expected %q, got %q", string(data), string(d))
|
||||||
}
|
|
||||||
took1 := time.Since(now)
|
took1 := time.Since(now)
|
||||||
t.Logf("took %v with no latency", took1)
|
t.Logf("took %v with no latency", took1)
|
||||||
|
|
||||||
lat, rv := 700*time.Millisecond, 10*time.Millisecond
|
lat, rv := 700*time.Millisecond, 10*time.Millisecond
|
||||||
p.DelayAccept(lat, rv)
|
p.DelayAccept(lat, rv)
|
||||||
defer p.UndelayAccept()
|
defer p.UndelayAccept()
|
||||||
if err := p.ResetListener(); err != nil {
|
require.NoError(t, p.ResetListener())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
send(t, data, scheme, srcAddr, tlsInfo)
|
send(t, data, scheme, srcAddr, tlsInfo)
|
||||||
if d := receive(t, ln); !bytes.Equal(data, d) {
|
d = receive(t, ln)
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
require.Truef(t, bytes.Equal(data, d), "expected %q, got %q", string(data), string(d))
|
||||||
}
|
|
||||||
took2 := time.Since(now)
|
took2 := time.Since(now)
|
||||||
t.Logf("took %v with latency %v±%v", took2, lat, rv)
|
t.Logf("took %v with latency %v±%v", took2, lat, rv)
|
||||||
|
|
||||||
if took1 >= took2 {
|
require.Lessf(t, took1, took2, "expected took1 %v < took2 %v", took1, took2)
|
||||||
t.Fatalf("expected took1 %v < took2 %v", took1, took2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_PauseTx(t *testing.T) {
|
func TestServer_PauseTx(t *testing.T) {
|
||||||
@ -262,7 +254,7 @@ func TestServer_PauseTx(t *testing.T) {
|
|||||||
|
|
||||||
recvc := make(chan []byte, 1)
|
recvc := make(chan []byte, 1)
|
||||||
go func() {
|
go func() {
|
||||||
recvc <- receive(t, ln)
|
recvc <- receive(t, ln) //nolint:testifylint //FIXME
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -275,9 +267,7 @@ func TestServer_PauseTx(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case d := <-recvc:
|
case d := <-recvc:
|
||||||
if !bytes.Equal(data, d) {
|
require.Truef(t, bytes.Equal(data, d), "expected %q, got %q", string(data), string(d))
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
|
||||||
}
|
|
||||||
case <-time.After(2 * time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
t.Fatal("took too long to receive after unpause")
|
t.Fatal("took too long to receive after unpause")
|
||||||
}
|
}
|
||||||
@ -310,15 +300,13 @@ func TestServer_ModifyTx_corrupt(t *testing.T) {
|
|||||||
})
|
})
|
||||||
data := []byte("Hello World!")
|
data := []byte("Hello World!")
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); bytes.Equal(d, data) {
|
d := receive(t, ln)
|
||||||
t.Fatalf("expected corrupted data, got %q", string(d))
|
require.Falsef(t, bytes.Equal(d, data), "expected corrupted data, got %q", string(d))
|
||||||
}
|
|
||||||
|
|
||||||
p.UnmodifyTx()
|
p.UnmodifyTx()
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); !bytes.Equal(d, data) {
|
d = receive(t, ln)
|
||||||
t.Fatalf("expected uncorrupted data, got %q", string(d))
|
require.Truef(t, bytes.Equal(d, data), "expected uncorrupted data, got %q", string(d))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_ModifyTx_packet_loss(t *testing.T) {
|
func TestServer_ModifyTx_packet_loss(t *testing.T) {
|
||||||
@ -349,15 +337,13 @@ func TestServer_ModifyTx_packet_loss(t *testing.T) {
|
|||||||
})
|
})
|
||||||
data := []byte("Hello World!")
|
data := []byte("Hello World!")
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); bytes.Equal(d, data) {
|
d := receive(t, ln)
|
||||||
t.Fatalf("expected corrupted data, got %q", string(d))
|
require.Falsef(t, bytes.Equal(d, data), "expected corrupted data, got %q", string(d))
|
||||||
}
|
|
||||||
|
|
||||||
p.UnmodifyTx()
|
p.UnmodifyTx()
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); !bytes.Equal(d, data) {
|
d = receive(t, ln)
|
||||||
t.Fatalf("expected uncorrupted data, got %q", string(d))
|
require.Truef(t, bytes.Equal(d, data), "expected uncorrupted data, got %q", string(d))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_BlackholeTx(t *testing.T) {
|
func TestServer_BlackholeTx(t *testing.T) {
|
||||||
@ -388,7 +374,7 @@ func TestServer_BlackholeTx(t *testing.T) {
|
|||||||
|
|
||||||
recvc := make(chan []byte, 1)
|
recvc := make(chan []byte, 1)
|
||||||
go func() {
|
go func() {
|
||||||
recvc <- receive(t, ln)
|
recvc <- receive(t, ln) //nolint:testifylint //FIXME
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -405,9 +391,7 @@ func TestServer_BlackholeTx(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case d := <-recvc:
|
case d := <-recvc:
|
||||||
if !bytes.Equal(data, d) {
|
require.Truef(t, bytes.Equal(data, d), "expected %q, got %q", string(data), string(d))
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
|
||||||
}
|
|
||||||
case <-time.After(2 * time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
t.Fatal("took too long to receive after unblackhole")
|
t.Fatal("took too long to receive after unblackhole")
|
||||||
}
|
}
|
||||||
@ -440,9 +424,8 @@ func TestServer_Shutdown(t *testing.T) {
|
|||||||
|
|
||||||
data := []byte("Hello World!")
|
data := []byte("Hello World!")
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); !bytes.Equal(d, data) {
|
d := receive(t, ln)
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
require.Truef(t, bytes.Equal(d, data), "expected %q, got %q", string(data), string(d))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_ShutdownListener(t *testing.T) {
|
func TestServer_ShutdownListener(t *testing.T) {
|
||||||
@ -476,9 +459,8 @@ func TestServer_ShutdownListener(t *testing.T) {
|
|||||||
|
|
||||||
data := []byte("Hello World!")
|
data := []byte("Hello World!")
|
||||||
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
send(t, data, scheme, srcAddr, transport.TLSInfo{})
|
||||||
if d := receive(t, ln); !bytes.Equal(d, data) {
|
d := receive(t, ln)
|
||||||
t.Fatalf("expected %q, got %q", string(data), string(d))
|
require.Truef(t, bytes.Equal(d, data), "expected %q, got %q", string(data), string(d))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServerHTTP_Insecure_DelayTx(t *testing.T) { testServerHTTP(t, false, true) }
|
func TestServerHTTP_Insecure_DelayTx(t *testing.T) { testServerHTTP(t, false, true) }
|
||||||
@ -497,20 +479,15 @@ func testServerHTTP(t *testing.T, secure, delayTx bool) {
|
|||||||
mux.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
|
mux.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
|
||||||
d, err := io.ReadAll(req.Body)
|
d, err := io.ReadAll(req.Body)
|
||||||
req.Body.Close()
|
req.Body.Close()
|
||||||
if err != nil {
|
require.NoError(t, err) //nolint:testifylint //FIXME
|
||||||
t.Fatal(err)
|
_, err = w.Write([]byte(fmt.Sprintf("%q(confirmed)", string(d))))
|
||||||
}
|
require.NoError(t, err) //nolint:testifylint //FIXME
|
||||||
if _, err = w.Write([]byte(fmt.Sprintf("%q(confirmed)", string(d)))); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
tlsInfo := createTLSInfo(lg, secure)
|
tlsInfo := createTLSInfo(lg, secure)
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if secure {
|
if secure {
|
||||||
_, err := tlsInfo.ServerConfig()
|
_, err := tlsInfo.ServerConfig()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: dstAddr,
|
Addr: dstAddr,
|
||||||
@ -570,18 +547,14 @@ func testServerHTTP(t *testing.T, secure, delayTx bool) {
|
|||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
d, err := io.ReadAll(resp.Body)
|
d, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
took1 := time.Since(now)
|
took1 := time.Since(now)
|
||||||
t.Logf("took %v with no latency", took1)
|
t.Logf("took %v with no latency", took1)
|
||||||
|
|
||||||
rs1 := string(d)
|
rs1 := string(d)
|
||||||
exp := fmt.Sprintf("%q(confirmed)", data)
|
exp := fmt.Sprintf("%q(confirmed)", data)
|
||||||
if rs1 != exp {
|
require.Equalf(t, exp, rs1, "got %q, expected %q", rs1, exp)
|
||||||
t.Fatalf("got %q, expected %q", rs1, exp)
|
|
||||||
}
|
|
||||||
|
|
||||||
lat, rv := 100*time.Millisecond, 10*time.Millisecond
|
lat, rv := 100*time.Millisecond, 10*time.Millisecond
|
||||||
if delayTx {
|
if delayTx {
|
||||||
@ -595,9 +568,7 @@ func testServerHTTP(t *testing.T, secure, delayTx bool) {
|
|||||||
now = time.Now()
|
now = time.Now()
|
||||||
if secure {
|
if secure {
|
||||||
tp, terr := transport.NewTransport(tlsInfo, 3*time.Second)
|
tp, terr := transport.NewTransport(tlsInfo, 3*time.Second)
|
||||||
if terr != nil {
|
require.NoError(t, terr)
|
||||||
t.Fatal(terr)
|
|
||||||
}
|
|
||||||
cli := &http.Client{Transport: tp}
|
cli := &http.Client{Transport: tp}
|
||||||
resp, err = cli.Post("https://"+srcAddr+"/hello", "", strings.NewReader(data))
|
resp, err = cli.Post("https://"+srcAddr+"/hello", "", strings.NewReader(data))
|
||||||
defer cli.CloseIdleConnections()
|
defer cli.CloseIdleConnections()
|
||||||
@ -606,24 +577,16 @@ func testServerHTTP(t *testing.T, secure, delayTx bool) {
|
|||||||
resp, err = http.Post("http://"+srcAddr+"/hello", "", strings.NewReader(data))
|
resp, err = http.Post("http://"+srcAddr+"/hello", "", strings.NewReader(data))
|
||||||
defer http.DefaultClient.CloseIdleConnections()
|
defer http.DefaultClient.CloseIdleConnections()
|
||||||
}
|
}
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
d, err = io.ReadAll(resp.Body)
|
d, err = io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
took2 := time.Since(now)
|
took2 := time.Since(now)
|
||||||
t.Logf("took %v with latency %v±%v", took2, lat, rv)
|
t.Logf("took %v with latency %v±%v", took2, lat, rv)
|
||||||
|
|
||||||
rs2 := string(d)
|
rs2 := string(d)
|
||||||
if rs2 != exp {
|
require.Equalf(t, exp, rs2, "got %q, expected %q", rs2, exp)
|
||||||
t.Fatalf("got %q, expected %q", rs2, exp)
|
require.LessOrEqualf(t, took1, took2, "expected took1 %v < took2 %v", took1, took2)
|
||||||
}
|
|
||||||
if took1 > took2 {
|
|
||||||
t.Fatalf("expected took1 %v < took2 %v", took1, took2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUnixAddr() string {
|
func newUnixAddr() string {
|
||||||
@ -640,9 +603,7 @@ func listen(t *testing.T, scheme, addr string, tlsInfo transport.TLSInfo) (ln ne
|
|||||||
} else {
|
} else {
|
||||||
ln, err = net.Listen(scheme, addr)
|
ln, err = net.Listen(scheme, addr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return ln
|
return ln
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -651,36 +612,25 @@ func send(t *testing.T, data []byte, scheme, addr string, tlsInfo transport.TLSI
|
|||||||
var err error
|
var err error
|
||||||
if !tlsInfo.Empty() {
|
if !tlsInfo.Empty() {
|
||||||
tp, terr := transport.NewTransport(tlsInfo, 3*time.Second)
|
tp, terr := transport.NewTransport(tlsInfo, 3*time.Second)
|
||||||
if terr != nil {
|
require.NoError(t, terr)
|
||||||
t.Fatal(terr)
|
|
||||||
}
|
|
||||||
out, err = tp.DialContext(context.Background(), scheme, addr)
|
out, err = tp.DialContext(context.Background(), scheme, addr)
|
||||||
} else {
|
} else {
|
||||||
out, err = net.Dial(scheme, addr)
|
out, err = net.Dial(scheme, addr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
_, err = out.Write(data)
|
||||||
}
|
require.NoError(t, err)
|
||||||
if _, err = out.Write(data); err != nil {
|
require.NoError(t, out.Close())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err = out.Close(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive(t *testing.T, ln net.Listener) (data []byte) {
|
func receive(t *testing.T, ln net.Listener) (data []byte) {
|
||||||
buf := bytes.NewBuffer(make([]byte, 0, 1024))
|
buf := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
for {
|
for {
|
||||||
in, err := ln.Accept()
|
in, err := ln.Accept()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
var n int64
|
var n int64
|
||||||
n, err = buf.ReadFrom(in)
|
n, err = buf.ReadFrom(in)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,10 @@ package report
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,16 +28,12 @@ func TestPercentiles(t *testing.T) {
|
|||||||
nums := make([]float64, 100)
|
nums := make([]float64, 100)
|
||||||
nums[99] = 1 // 99-percentile (1 out of 100)
|
nums[99] = 1 // 99-percentile (1 out of 100)
|
||||||
data := percentiles(nums)
|
data := percentiles(nums)
|
||||||
if data[len(pctls)-2] != 1 {
|
require.InDeltaf(t, 1, data[len(pctls)-2], 0.0, "99-percentile expected 1, got %f", data[len(pctls)-2])
|
||||||
t.Fatalf("99-percentile expected 1, got %f", data[len(pctls)-2])
|
|
||||||
}
|
|
||||||
|
|
||||||
nums = make([]float64, 1000)
|
nums = make([]float64, 1000)
|
||||||
nums[999] = 1 // 99.9-percentile (1 out of 1000)
|
nums[999] = 1 // 99.9-percentile (1 out of 1000)
|
||||||
data = percentiles(nums)
|
data = percentiles(nums)
|
||||||
if data[len(pctls)-1] != 1 {
|
require.InDeltaf(t, 1, data[len(pctls)-1], 0.0, "99.9-percentile expected 1, got %f", data[len(pctls)-1])
|
||||||
t.Fatalf("99.9-percentile expected 1, got %f", data[len(pctls)-1])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReport(t *testing.T) {
|
func TestReport(t *testing.T) {
|
||||||
@ -76,9 +72,7 @@ func TestReport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
ss := <-r.Run()
|
ss := <-r.Run()
|
||||||
for i, ws := range wstrs {
|
for i, ws := range wstrs {
|
||||||
if !strings.Contains(ss, ws) {
|
assert.Containsf(t, ss, ws, "#%d: stats string missing %s", i, ws)
|
||||||
t.Errorf("#%d: stats string missing %s", i, ws)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
"go.uber.org/zap/zaptest"
|
||||||
)
|
)
|
||||||
@ -53,7 +54,5 @@ func TestFIFOSchedule(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.WaitFinish(100)
|
s.WaitFinish(100)
|
||||||
if s.Finished() != 100 {
|
assert.Equalf(t, 100, s.Finished(), "finished = %d, want %d", s.Finished(), 100)
|
||||||
t.Errorf("finished = %d, want %d", s.Finished(), 100)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"go.etcd.io/etcd/client/pkg/v3/logutil"
|
"go.etcd.io/etcd/client/pkg/v3/logutil"
|
||||||
@ -50,9 +51,7 @@ func TestGet(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
trace := Get(tt.inputCtx)
|
trace := Get(tt.inputCtx)
|
||||||
if trace == nil {
|
assert.NotNilf(t, trace, "Expected %v; Got nil", tt.outputTrace)
|
||||||
t.Errorf("Expected %v; Got nil", tt.outputTrace)
|
|
||||||
}
|
|
||||||
if tt.outputTrace == nil || trace.operation != tt.outputTrace.operation {
|
if tt.outputTrace == nil || trace.operation != tt.outputTrace.operation {
|
||||||
t.Errorf("Expected %v; Got %v", tt.outputTrace, trace)
|
t.Errorf("Expected %v; Got %v", tt.outputTrace, trace)
|
||||||
}
|
}
|
||||||
@ -75,16 +74,10 @@ func TestCreate(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
trace := New(op, nil, fields[0], fields[1])
|
trace := New(op, nil, fields[0], fields[1])
|
||||||
if trace.operation != op {
|
assert.Equalf(t, trace.operation, op, "Expected %v; Got %v", op, trace.operation)
|
||||||
t.Errorf("Expected %v; Got %v", op, trace.operation)
|
|
||||||
}
|
|
||||||
for i, f := range trace.fields {
|
for i, f := range trace.fields {
|
||||||
if f.Key != fields[i].Key {
|
assert.Equalf(t, f.Key, fields[i].Key, "Expected %v; Got %v", fields[i].Key, f.Key)
|
||||||
t.Errorf("Expected %v; Got %v", fields[i].Key, f.Key)
|
assert.Equalf(t, f.Value, fields[i].Value, "Expected %v; Got %v", fields[i].Value, f.Value)
|
||||||
}
|
|
||||||
if f.Value != fields[i].Value {
|
|
||||||
t.Errorf("Expected %v; Got %v", fields[i].Value, f.Value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range steps {
|
for i, v := range steps {
|
||||||
@ -92,15 +85,9 @@ func TestCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range trace.steps {
|
for i, v := range trace.steps {
|
||||||
if steps[i] != v.msg {
|
assert.Equalf(t, steps[i], v.msg, "Expected %v; Got %v", steps[i], v.msg)
|
||||||
t.Errorf("Expected %v; Got %v", steps[i], v.msg)
|
assert.Equalf(t, stepFields[i].Key, v.fields[0].Key, "Expected %v; Got %v", stepFields[i].Key, v.fields[0].Key)
|
||||||
}
|
assert.Equalf(t, stepFields[i].Value, v.fields[0].Value, "Expected %v; Got %v", stepFields[i].Value, v.fields[0].Value)
|
||||||
if stepFields[i].Key != v.fields[0].Key {
|
|
||||||
t.Errorf("Expected %v; Got %v", stepFields[i].Key, v.fields[0].Key)
|
|
||||||
}
|
|
||||||
if stepFields[i].Value != v.fields[0].Value {
|
|
||||||
t.Errorf("Expected %v; Got %v", stepFields[i].Value, v.fields[0].Value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,9 +207,7 @@ func TestLog(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, msg := range tt.expectedMsg {
|
for _, msg := range tt.expectedMsg {
|
||||||
if !bytes.Contains(data, []byte(msg)) {
|
assert.Truef(t, bytes.Contains(data, []byte(msg)), "Expected to find %v in log", msg)
|
||||||
t.Errorf("Expected to find %v in log", msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -295,9 +280,7 @@ func TestLogIfLong(t *testing.T) {
|
|||||||
data, err := os.ReadFile(logPath)
|
data, err := os.ReadFile(logPath)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
for _, msg := range tt.expectedMsg {
|
for _, msg := range tt.expectedMsg {
|
||||||
if !bytes.Contains(data, []byte(msg)) {
|
assert.Truef(t, bytes.Contains(data, []byte(msg)), "Expected to find %v in log", msg)
|
||||||
t.Errorf("Expected to find %v in log", msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWait(t *testing.T) {
|
func TestWait(t *testing.T) {
|
||||||
@ -26,9 +28,8 @@ func TestWait(t *testing.T) {
|
|||||||
ch := wt.Register(eid)
|
ch := wt.Register(eid)
|
||||||
wt.Trigger(eid, "foo")
|
wt.Trigger(eid, "foo")
|
||||||
v := <-ch
|
v := <-ch
|
||||||
if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
|
g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"
|
||||||
t.Errorf("<-ch = %v, want %v", g, w)
|
assert.Equalf(t, g, w, "<-ch = %v, want %v", g, w)
|
||||||
}
|
|
||||||
|
|
||||||
if g := <-ch; g != nil {
|
if g := <-ch; g != nil {
|
||||||
t.Errorf("unexpected non-nil value: %v (%T)", g, g)
|
t.Errorf("unexpected non-nil value: %v (%T)", g, g)
|
||||||
@ -69,9 +70,8 @@ func TestTriggerDupSuppression(t *testing.T) {
|
|||||||
wt.Trigger(eid, "bar")
|
wt.Trigger(eid, "bar")
|
||||||
|
|
||||||
v := <-ch
|
v := <-ch
|
||||||
if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
|
g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"
|
||||||
t.Errorf("<-ch = %v, want %v", g, w)
|
assert.Equalf(t, g, w, "<-ch = %v, want %v", g, w)
|
||||||
}
|
|
||||||
|
|
||||||
if g := <-ch; g != nil {
|
if g := <-ch; g != nil {
|
||||||
t.Errorf("unexpected non-nil value: %v (%T)", g, g)
|
t.Errorf("unexpected non-nil value: %v (%T)", g, g)
|
||||||
@ -86,17 +86,11 @@ func TestIsRegistered(t *testing.T) {
|
|||||||
wt.Register(2)
|
wt.Register(2)
|
||||||
|
|
||||||
for i := uint64(0); i < 3; i++ {
|
for i := uint64(0); i < 3; i++ {
|
||||||
if !wt.IsRegistered(i) {
|
assert.Truef(t, wt.IsRegistered(i), "event ID %d isn't registered", i)
|
||||||
t.Errorf("event ID %d isn't registered", i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if wt.IsRegistered(4) {
|
assert.Falsef(t, wt.IsRegistered(4), "event ID 4 shouldn't be registered")
|
||||||
t.Errorf("event ID 4 shouldn't be registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
wt.Trigger(0, "foo")
|
wt.Trigger(0, "foo")
|
||||||
if wt.IsRegistered(0) {
|
assert.Falsef(t, wt.IsRegistered(0), "event ID 0 is already triggered, shouldn't be registered")
|
||||||
t.Errorf("event ID 0 is already triggered, shouldn't be registered")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user