diff --git a/examples/compose/client.go b/examples/compose/client.go index 8beaef683cd..95d6b4f4815 100644 --- a/examples/compose/client.go +++ b/examples/compose/client.go @@ -53,7 +53,7 @@ func main() { // Insert some messages on random pages. fmt.Println("Inserting into primary...") - for i := 0; i < 3; i++ { + for range 3 { tx, err := db.Begin() if err != nil { fmt.Printf("begin failed: %v\n", err) diff --git a/examples/compose/vtcompose/vtcompose.go b/examples/compose/vtcompose/vtcompose.go index c6df1d72e48..3bcfd8315e5 100644 --- a/examples/compose/vtcompose/vtcompose.go +++ b/examples/compose/vtcompose/vtcompose.go @@ -218,24 +218,6 @@ func main() { writeFile(dockerComposeFile, "docker-compose.yml") } -func applyFilePatch(dockerYaml []byte, patchFile string) []byte { - yamlPatch, err := os.ReadFile(patchFile) - if err != nil { - log.Fatalf("reading yaml patch file %s: %s", patchFile, err) - } - - patch, err := yamlpatch.DecodePatch(yamlPatch) - if err != nil { - log.Fatalf("decoding patch failed: %s", err) - } - - bs, err := patch.Apply(dockerYaml) - if err != nil { - log.Fatalf("applying patch failed: %s", err) - } - return bs -} - func applyJsonInMemoryPatch(vSchemaFile []byte, patchString string) []byte { patch, err := jsonpatch.DecodePatch([]byte(patchString)) if err != nil { @@ -446,7 +428,7 @@ func applyKeyspaceDependentPatches( dockerComposeFile = applyShardPatches(dockerComposeFile, tabAlias, shard, keyspaceData, externalDbInfoMap, opts) } else { // Determine shard range - for i := 0; i < keyspaceData.shards; i++ { + for i := range keyspaceData.shards { if i == 0 { shard = fmt.Sprintf("-%x", interval) } else if i == (keyspaceData.shards - 1) { @@ -517,28 +499,6 @@ func applyShardPatches( return dockerComposeFile } -func generateDefaultShard(tabAlias int, shard string, keyspaceData keyspaceInfo, opts vtOptions) string { - aliases := []int{tabAlias + 1} // primary alias, e.g. 201 - for i := 0; i < keyspaceData.replicaTablets; i++ { - aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ... - } - tabletDepends := make([]string, len(aliases)) - for i, tabletId := range aliases { - tabletDepends[i] = fmt.Sprintf("vttablet%d: {condition : service_healthy}", tabletId) - } - // Wait on all shard tablets to be healthy - dependsOn := "depends_on: {" + strings.Join(tabletDepends, ", ") + "}" - - return fmt.Sprintf(` -- op: add - path: /services/init_shard_primary%[2]d - value: - image: vitess/lite:${VITESS_TAG:-latest} - command: ["sh", "-c", "/vt/bin/vtctldclient %[5]s InitShardPrimary --force %[4]s/%[3]s %[6]s-%[2]d "] - %[1]s -`, dependsOn, aliases[0], shard, keyspaceData.keyspace, opts.topologyFlags, opts.cell) -} - func generateExternalPrimary( tabAlias int, shard string, @@ -548,7 +508,7 @@ func generateExternalPrimary( ) string { aliases := []int{tabAlias + 1} // primary alias, e.g. 201 - for i := 0; i < keyspaceData.replicaTablets; i++ { + for i := range keyspaceData.replicaTablets { aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ... } @@ -611,7 +571,7 @@ func applyTabletPatches( dbInfo = val } dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+1, shard, "primary", keyspaceData.keyspace, dbInfo, opts)) - for i := 0; i < keyspaceData.replicaTablets; i++ { + for i := range keyspaceData.replicaTablets { dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+2+i, shard, "replica", keyspaceData.keyspace, dbInfo, opts)) } return dockerComposeFile diff --git a/go/bucketpool/bucketpool_test.go b/go/bucketpool/bucketpool_test.go index cc8bcff0eec..af9693a523f 100644 --- a/go/bucketpool/bucketpool_test.go +++ b/go/bucketpool/bucketpool_test.go @@ -19,166 +19,105 @@ package bucketpool import ( "math/rand" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPool(t *testing.T) { maxSize := 16384 pool := New(1024, maxSize) - if pool.maxSize != maxSize { - t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) - } - if len(pool.pools) != 5 { - t.Fatalf("Invalid number of pools: %d, expected %d", len(pool.pools), 5) - } + require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size") + require.Len(t, pool.pools, 5, "Invalid number of pools") buf := pool.Get(64) - if len(*buf) != 64 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1024 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 64, "unexpected buf length") + require.Equal(t, 1024, cap(*buf), "unexpected buf cap") // get from same pool, check that length is right buf = pool.Get(128) - if len(*buf) != 128 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1024 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 128, "unexpected buf length") + require.Equal(t, 1024, cap(*buf), "unexpected buf cap") pool.Put(buf) // get boundary size buf = pool.Get(1024) - if len(*buf) != 1024 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1024 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 1024, "unexpected buf length") + require.Equal(t, 1024, cap(*buf), "unexpected buf cap") pool.Put(buf) // get from the middle buf = pool.Get(5000) - if len(*buf) != 5000 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 8192 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 5000, "unexpected buf length") + require.Equal(t, 8192, cap(*buf), "unexpected buf cap") pool.Put(buf) // check last pool buf = pool.Get(16383) - if len(*buf) != 16383 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 16384 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 16383, "unexpected buf length") + require.Equal(t, 16384, cap(*buf), "unexpected buf cap") pool.Put(buf) // get big buffer buf = pool.Get(16385) - if len(*buf) != 16385 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 16385 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 16385, "unexpected buf length") + require.Equal(t, 16385, cap(*buf), "unexpected buf cap") pool.Put(buf) } func TestPoolOneSize(t *testing.T) { maxSize := 1024 pool := New(1024, maxSize) - if pool.maxSize != maxSize { - t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) - } + require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size") buf := pool.Get(64) - if len(*buf) != 64 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1024 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 64, "unexpected buf length") + require.Equal(t, 1024, cap(*buf), "unexpected buf cap") pool.Put(buf) buf = pool.Get(1025) - if len(*buf) != 1025 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1025 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 1025, "unexpected buf length") + require.Equal(t, 1025, cap(*buf), "unexpected buf cap") pool.Put(buf) } func TestPoolTwoSizeNotMultiplier(t *testing.T) { maxSize := 2000 pool := New(1024, maxSize) - if pool.maxSize != maxSize { - t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) - } + require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size") buf := pool.Get(64) - if len(*buf) != 64 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 1024 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 64, "unexpected buf length") + require.Equal(t, 1024, cap(*buf), "unexpected buf cap") pool.Put(buf) buf = pool.Get(2001) - if len(*buf) != 2001 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 2001 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 2001, "unexpected buf length") + require.Equal(t, 2001, cap(*buf), "unexpected buf cap") pool.Put(buf) } func TestPoolMaxSizeLessThanMinSize(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("Expected the code to panic") - } - }() - - New(15000, 1024) + assert.Panics(t, func() { New(15000, 1024) }) } func TestPoolWeirdMaxSize(t *testing.T) { maxSize := 15000 pool := New(1024, maxSize) - if pool.maxSize != maxSize { - t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) - } + require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size") buf := pool.Get(14000) - if len(*buf) != 14000 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 15000 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 14000, "unexpected buf length") + require.Equal(t, 15000, cap(*buf), "unexpected buf cap") pool.Put(buf) buf = pool.Get(16383) - if len(*buf) != 16383 { - t.Fatalf("unexpected buf length: %d", len(*buf)) - } - if cap(*buf) != 16383 { - t.Fatalf("unexpected buf cap: %d", cap(*buf)) - } + require.Len(t, *buf, 16383, "unexpected buf length") + require.Equal(t, 16383, cap(*buf), "unexpected buf cap") pool.Put(buf) } func TestFuzz(t *testing.T) { maxTestSize := 16384 - for i := 0; i < 20000; i++ { + for range 20000 { minSize := rand.Intn(maxTestSize) if minSize == 0 { minSize = 1 @@ -187,18 +126,12 @@ func TestFuzz(t *testing.T) { p := New(minSize, maxSize) bufSize := rand.Intn(maxTestSize) buf := p.Get(bufSize) - if len(*buf) != bufSize { - t.Fatalf("Invalid length %d, expected %d", len(*buf), bufSize) - } + require.Len(t, *buf, bufSize, "unexpected buf length") sPool := p.findPool(bufSize) if sPool == nil { - if cap(*buf) != len(*buf) { - t.Fatalf("Invalid cap %d, expected %d", cap(*buf), len(*buf)) - } + require.Equal(t, len(*buf), cap(*buf), "unexpected buf cap") } else { - if cap(*buf) != sPool.size { - t.Fatalf("Invalid cap %d, expected %d", cap(*buf), sPool.size) - } + require.Equal(t, sPool.size, cap(*buf), "unexpected buf cap") } p.Put(buf) } diff --git a/go/cache/theine/bf/bf.go b/go/cache/theine/bf/bf.go index f68e34d81e3..97b27a5c217 100644 --- a/go/cache/theine/bf/bf.go +++ b/go/cache/theine/bf/bf.go @@ -54,7 +54,7 @@ func (d *Bloomfilter) EnsureCapacity(capacity int) { func (d *Bloomfilter) Exist(h uint64) bool { h1, h2 := uint32(h), uint32(h>>32) var o uint = 1 - for i := uint32(0); i < d.K; i++ { + for i := range d.K { o &= d.Filter.get((h1 + (i * h2)) & (d.M - 1)) } return o == 1 @@ -65,7 +65,7 @@ func (d *Bloomfilter) Exist(h uint64) bool { func (d *Bloomfilter) Insert(h uint64) bool { h1, h2 := uint32(h), uint32(h>>32) var o uint = 1 - for i := uint32(0); i < d.K; i++ { + for i := range d.K { o &= d.Filter.getset((h1 + (i * h2)) & (d.M - 1)) } return o == 1 diff --git a/go/cache/theine/list_test.go b/go/cache/theine/list_test.go index aad68f5c142..a0b607338dd 100644 --- a/go/cache/theine/list_test.go +++ b/go/cache/theine/list_test.go @@ -28,7 +28,7 @@ func TestList(t *testing.T) { l := NewList[StringKey, string](5, LIST_PROBATION) require.Equal(t, uint(5), l.capacity) require.Equal(t, LIST_PROBATION, l.listType) - for i := 0; i < 5; i++ { + for i := range 5 { evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1)) require.Nil(t, evicted) } @@ -42,7 +42,7 @@ func TestList(t *testing.T) { require.Equal(t, "5/4/3/2/1", l.display()) require.Equal(t, "1/2/3/4/5", l.displayReverse()) - for i := 0; i < 5; i++ { + for i := range 5 { entry := l.PopTail() require.Equal(t, StringKey(fmt.Sprintf("%d", i+1)), entry.key) } @@ -50,7 +50,7 @@ func TestList(t *testing.T) { require.Nil(t, entry) var entries []*Entry[StringKey, string] - for i := 0; i < 5; i++ { + for i := range 5 { new := NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1) evicted := l.PushFront(new) entries = append(entries, new) @@ -76,13 +76,13 @@ func TestListCountCost(t *testing.T) { l := NewList[StringKey, string](100, LIST_PROBATION) require.Equal(t, uint(100), l.capacity) require.Equal(t, LIST_PROBATION, l.listType) - for i := 0; i < 5; i++ { + for i := range 5 { evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 20)) require.Nil(t, evicted) } require.Equal(t, 100, l.len) require.Equal(t, 5, l.count) - for i := 0; i < 3; i++ { + for range 3 { entry := l.PopTail() require.NotNil(t, entry) } diff --git a/go/cache/theine/singleflight_test.go b/go/cache/theine/singleflight_test.go index 60b28e69b4e..4ae235f97e2 100644 --- a/go/cache/theine/singleflight_test.go +++ b/go/cache/theine/singleflight_test.go @@ -32,6 +32,9 @@ import ( "sync/atomic" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDo(t *testing.T) { @@ -39,12 +42,9 @@ func TestDo(t *testing.T) { v, err, _ := g.Do("key", func() (string, error) { return "bar", nil }) - if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want { - t.Errorf("Do = %v; want %v", got, want) - } - if err != nil { - t.Errorf("Do error = %v", err) - } + + assert.Equal(t, "bar (string)", fmt.Sprintf("%v (%T)", v, v), "incorrect Do value") + assert.NoError(t, err, "got Do error") } func TestDoErr(t *testing.T) { @@ -53,12 +53,9 @@ func TestDoErr(t *testing.T) { v, err, _ := g.Do("key", func() (string, error) { return "", someErr }) - if err != someErr { - t.Errorf("Do error = %v; want someErr %v", err, someErr) - } - if v != "" { - t.Errorf("unexpected non-nil value %#v", v) - } + + assert.ErrorIs(t, err, someErr, "incorrect Do error") + assert.Empty(t, v, "unexpected non-empty value") } func TestDoDupSuppress(t *testing.T) { @@ -81,20 +78,18 @@ func TestDoDupSuppress(t *testing.T) { const n = 10 wg1.Add(1) - for i := 0; i < n; i++ { + for range n { wg1.Add(1) wg2.Add(1) go func() { defer wg2.Done() wg1.Done() v, err, _ := g.Do("key", fn) - if err != nil { - t.Errorf("Do error: %v", err) + if !assert.NoError(t, err, "unexpected Do error") { return } - if s := v; s != "bar" { - t.Errorf("Do = %T %v; want %q", v, v, "bar") - } + + assert.Equal(t, "bar", v, "unexpected Do value") }() } wg1.Wait() @@ -102,9 +97,8 @@ func TestDoDupSuppress(t *testing.T) { // least reached the line before the Do. c <- "bar" wg2.Wait() - if got := atomic.LoadInt32(&calls); got <= 0 || got >= n { - t.Errorf("number of calls = %d; want over 0 and less than %d", got, n) - } + got := atomic.LoadInt32(&calls) + assert.True(t, got > 0 && got < n, "number of calls not between 0 and %d", n) } // Test singleflight behaves correctly after Do panic. @@ -119,7 +113,7 @@ func TestPanicDo(t *testing.T) { waited := int32(n) panicCount := int32(0) done := make(chan struct{}) - for i := 0; i < n; i++ { + for range n { go func() { defer func() { if err := recover(); err != nil { @@ -137,11 +131,9 @@ func TestPanicDo(t *testing.T) { select { case <-done: - if panicCount != n { - t.Errorf("Expect %d panic, but got %d", n, panicCount) - } + assert.Equal(t, int32(n), panicCount, "unexpected number of panics") case <-time.After(time.Second): - t.Fatalf("Do hangs") + require.Fail(t, "Do hangs") } } @@ -155,13 +147,11 @@ func TestGoexitDo(t *testing.T) { const n = 5 waited := int32(n) done := make(chan struct{}) - for i := 0; i < n; i++ { + for range n { go func() { var err error defer func() { - if err != nil { - t.Errorf("Error should be nil, but got: %v", err) - } + assert.NoError(t, err) if atomic.AddInt32(&waited, -1) == 0 { close(done) } @@ -173,7 +163,7 @@ func TestGoexitDo(t *testing.T) { select { case <-done: case <-time.After(time.Second): - t.Fatalf("Do hangs") + require.Fail(t, "Do hangs") } } @@ -201,10 +191,9 @@ func randKeys(b *testing.B, count, length uint) []string { keys := make([]string, 0, count) key := make([]byte, length) - for i := uint(0); i < count; i++ { - if _, err := io.ReadFull(rand.Reader, key); err != nil { - b.Fatalf("Failed to generate random key %d of %d of length %d: %s", i+1, count, length, err) - } + for i := range uint(count) { + _, err := io.ReadFull(rand.Reader, key) + require.NoError(b, err, "Failed to generate random key %d of %d length %d", i+1, count, length) keys = append(keys, string(key)) } return keys diff --git a/go/cache/theine/sketch_test.go b/go/cache/theine/sketch_test.go index 3437f0cac3c..fb53fa8e5fb 100644 --- a/go/cache/theine/sketch_test.go +++ b/go/cache/theine/sketch_test.go @@ -23,7 +23,7 @@ func TestSketch(t *testing.T) { sketch.SampleSize = 5120 failed := 0 - for i := 0; i < 500; i++ { + for i := range 500 { key := fmt.Sprintf("key:%d", i) keyh := xxhash.Sum64String(key) sketch.Add(keyh) diff --git a/go/cache/theine/store.go b/go/cache/theine/store.go index 21ce084e8a5..cef5a89c8b7 100644 --- a/go/cache/theine/store.go +++ b/go/cache/theine/store.go @@ -208,7 +208,7 @@ func NewStore[K cachekey, V cacheval](maxsize int64, doorkeeper bool) *Store[K, writebufsize: writeBufSize, } s.shards = make([]*Shard[K, V], 0, s.shardCount) - for i := 0; i < int(s.shardCount); i++ { + for range s.shardCount { s.shards = append(s.shards, NewShard[K, V](uint(dequeSize), doorkeeper)) } diff --git a/go/cache/theine/store_test.go b/go/cache/theine/store_test.go index 9f337b818ad..e6a2f9d5679 100644 --- a/go/cache/theine/store_test.go +++ b/go/cache/theine/store_test.go @@ -52,7 +52,7 @@ func TestProcessDeque(t *testing.T) { shard := store.shards[index] shard.qsize = 10 - for i := keyint(0); i < 5; i++ { + for i := range keyint(5) { entry := &Entry[keyint, cachedint]{key: i} entry.cost.Store(1) store.shards[index].deque.PushFront(entry) @@ -75,7 +75,7 @@ func TestDoorKeeperDynamicSize(t *testing.T) { store := NewStore[keyint, cachedint](200000, true) shard := store.shards[0] require.True(t, shard.doorkeeper.Capacity == 512) - for i := keyint(0); i < 5000; i++ { + for i := range keyint(5000) { shard.set(i, &Entry[keyint, cachedint]{}) } require.True(t, shard.doorkeeper.Capacity > 100000) diff --git a/go/cache/theine/tlfu_test.go b/go/cache/theine/tlfu_test.go index ac6ddaabdb6..f798f89549f 100644 --- a/go/cache/theine/tlfu_test.go +++ b/go/cache/theine/tlfu_test.go @@ -33,7 +33,7 @@ func TestTlfu(t *testing.T) { require.Equal(t, 0, tlfu.slru.protected.len) var entries []*Entry[StringKey, string] - for i := 0; i < 200; i++ { + for i := range 200 { e := NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1) evicted := tlfu.Set(e) entries = append(entries, e) @@ -78,7 +78,7 @@ func TestTlfu(t *testing.T) { require.Equal(t, 998, tlfu.slru.probation.len) var entries2 []*Entry[StringKey, string] - for i := 0; i < 1000; i++ { + for i := range 1000 { e := NewEntry(StringKey(fmt.Sprintf("%d*", i)), "", 1) tlfu.Set(e) entries2 = append(entries2, e) @@ -103,7 +103,7 @@ func TestEvictEntries(t *testing.T) { require.Equal(t, 0, tlfu.slru.probation.len) require.Equal(t, 0, tlfu.slru.protected.len) - for i := 0; i < 500; i++ { + for i := range 500 { tlfu.Set(NewEntry(StringKey(fmt.Sprintf("%d:1", i)), "", 1)) } require.Equal(t, 500, tlfu.slru.probation.len)