Skip to content

Commit 3a5907f

Browse files
author
Andrew Mason
authored
New for loops and some assert/require (#15194)
Signed-off-by: Andrew Mason <andrew@planetscale.com>
1 parent c0b303d commit 3a5907f

File tree

10 files changed

+79
-197
lines changed

10 files changed

+79
-197
lines changed

examples/compose/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353

5454
// Insert some messages on random pages.
5555
fmt.Println("Inserting into primary...")
56-
for i := 0; i < 3; i++ {
56+
for range 3 {
5757
tx, err := db.Begin()
5858
if err != nil {
5959
fmt.Printf("begin failed: %v\n", err)

examples/compose/vtcompose/vtcompose.go

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,6 @@ func main() {
218218
writeFile(dockerComposeFile, "docker-compose.yml")
219219
}
220220

221-
func applyFilePatch(dockerYaml []byte, patchFile string) []byte {
222-
yamlPatch, err := os.ReadFile(patchFile)
223-
if err != nil {
224-
log.Fatalf("reading yaml patch file %s: %s", patchFile, err)
225-
}
226-
227-
patch, err := yamlpatch.DecodePatch(yamlPatch)
228-
if err != nil {
229-
log.Fatalf("decoding patch failed: %s", err)
230-
}
231-
232-
bs, err := patch.Apply(dockerYaml)
233-
if err != nil {
234-
log.Fatalf("applying patch failed: %s", err)
235-
}
236-
return bs
237-
}
238-
239221
func applyJsonInMemoryPatch(vSchemaFile []byte, patchString string) []byte {
240222
patch, err := jsonpatch.DecodePatch([]byte(patchString))
241223
if err != nil {
@@ -446,7 +428,7 @@ func applyKeyspaceDependentPatches(
446428
dockerComposeFile = applyShardPatches(dockerComposeFile, tabAlias, shard, keyspaceData, externalDbInfoMap, opts)
447429
} else {
448430
// Determine shard range
449-
for i := 0; i < keyspaceData.shards; i++ {
431+
for i := range keyspaceData.shards {
450432
if i == 0 {
451433
shard = fmt.Sprintf("-%x", interval)
452434
} else if i == (keyspaceData.shards - 1) {
@@ -517,28 +499,6 @@ func applyShardPatches(
517499
return dockerComposeFile
518500
}
519501

520-
func generateDefaultShard(tabAlias int, shard string, keyspaceData keyspaceInfo, opts vtOptions) string {
521-
aliases := []int{tabAlias + 1} // primary alias, e.g. 201
522-
for i := 0; i < keyspaceData.replicaTablets; i++ {
523-
aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ...
524-
}
525-
tabletDepends := make([]string, len(aliases))
526-
for i, tabletId := range aliases {
527-
tabletDepends[i] = fmt.Sprintf("vttablet%d: {condition : service_healthy}", tabletId)
528-
}
529-
// Wait on all shard tablets to be healthy
530-
dependsOn := "depends_on: {" + strings.Join(tabletDepends, ", ") + "}"
531-
532-
return fmt.Sprintf(`
533-
- op: add
534-
path: /services/init_shard_primary%[2]d
535-
value:
536-
image: vitess/lite:${VITESS_TAG:-latest}
537-
command: ["sh", "-c", "/vt/bin/vtctldclient %[5]s InitShardPrimary --force %[4]s/%[3]s %[6]s-%[2]d "]
538-
%[1]s
539-
`, dependsOn, aliases[0], shard, keyspaceData.keyspace, opts.topologyFlags, opts.cell)
540-
}
541-
542502
func generateExternalPrimary(
543503
tabAlias int,
544504
shard string,
@@ -548,7 +508,7 @@ func generateExternalPrimary(
548508
) string {
549509

550510
aliases := []int{tabAlias + 1} // primary alias, e.g. 201
551-
for i := 0; i < keyspaceData.replicaTablets; i++ {
511+
for i := range keyspaceData.replicaTablets {
552512
aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ...
553513
}
554514

@@ -611,7 +571,7 @@ func applyTabletPatches(
611571
dbInfo = val
612572
}
613573
dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+1, shard, "primary", keyspaceData.keyspace, dbInfo, opts))
614-
for i := 0; i < keyspaceData.replicaTablets; i++ {
574+
for i := range keyspaceData.replicaTablets {
615575
dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+2+i, shard, "replica", keyspaceData.keyspace, dbInfo, opts))
616576
}
617577
return dockerComposeFile

go/bucketpool/bucketpool_test.go

Lines changed: 37 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,166 +19,105 @@ package bucketpool
1919
import (
2020
"math/rand"
2121
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2225
)
2326

2427
func TestPool(t *testing.T) {
2528
maxSize := 16384
2629
pool := New(1024, maxSize)
27-
if pool.maxSize != maxSize {
28-
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
29-
}
30-
if len(pool.pools) != 5 {
31-
t.Fatalf("Invalid number of pools: %d, expected %d", len(pool.pools), 5)
32-
}
30+
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
31+
require.Len(t, pool.pools, 5, "Invalid number of pools")
3332

3433
buf := pool.Get(64)
35-
if len(*buf) != 64 {
36-
t.Fatalf("unexpected buf length: %d", len(*buf))
37-
}
38-
if cap(*buf) != 1024 {
39-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
40-
}
34+
require.Len(t, *buf, 64, "unexpected buf length")
35+
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
4136

4237
// get from same pool, check that length is right
4338
buf = pool.Get(128)
44-
if len(*buf) != 128 {
45-
t.Fatalf("unexpected buf length: %d", len(*buf))
46-
}
47-
if cap(*buf) != 1024 {
48-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
49-
}
39+
require.Len(t, *buf, 128, "unexpected buf length")
40+
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
5041
pool.Put(buf)
5142

5243
// get boundary size
5344
buf = pool.Get(1024)
54-
if len(*buf) != 1024 {
55-
t.Fatalf("unexpected buf length: %d", len(*buf))
56-
}
57-
if cap(*buf) != 1024 {
58-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
59-
}
45+
require.Len(t, *buf, 1024, "unexpected buf length")
46+
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
6047
pool.Put(buf)
6148

6249
// get from the middle
6350
buf = pool.Get(5000)
64-
if len(*buf) != 5000 {
65-
t.Fatalf("unexpected buf length: %d", len(*buf))
66-
}
67-
if cap(*buf) != 8192 {
68-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
69-
}
51+
require.Len(t, *buf, 5000, "unexpected buf length")
52+
require.Equal(t, 8192, cap(*buf), "unexpected buf cap")
7053
pool.Put(buf)
7154

7255
// check last pool
7356
buf = pool.Get(16383)
74-
if len(*buf) != 16383 {
75-
t.Fatalf("unexpected buf length: %d", len(*buf))
76-
}
77-
if cap(*buf) != 16384 {
78-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
79-
}
57+
require.Len(t, *buf, 16383, "unexpected buf length")
58+
require.Equal(t, 16384, cap(*buf), "unexpected buf cap")
8059
pool.Put(buf)
8160

8261
// get big buffer
8362
buf = pool.Get(16385)
84-
if len(*buf) != 16385 {
85-
t.Fatalf("unexpected buf length: %d", len(*buf))
86-
}
87-
if cap(*buf) != 16385 {
88-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
89-
}
63+
require.Len(t, *buf, 16385, "unexpected buf length")
64+
require.Equal(t, 16385, cap(*buf), "unexpected buf cap")
9065
pool.Put(buf)
9166
}
9267

9368
func TestPoolOneSize(t *testing.T) {
9469
maxSize := 1024
9570
pool := New(1024, maxSize)
96-
if pool.maxSize != maxSize {
97-
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
98-
}
71+
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
9972
buf := pool.Get(64)
100-
if len(*buf) != 64 {
101-
t.Fatalf("unexpected buf length: %d", len(*buf))
102-
}
103-
if cap(*buf) != 1024 {
104-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
105-
}
73+
require.Len(t, *buf, 64, "unexpected buf length")
74+
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
10675
pool.Put(buf)
10776

10877
buf = pool.Get(1025)
109-
if len(*buf) != 1025 {
110-
t.Fatalf("unexpected buf length: %d", len(*buf))
111-
}
112-
if cap(*buf) != 1025 {
113-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
114-
}
78+
require.Len(t, *buf, 1025, "unexpected buf length")
79+
require.Equal(t, 1025, cap(*buf), "unexpected buf cap")
11580
pool.Put(buf)
11681
}
11782

11883
func TestPoolTwoSizeNotMultiplier(t *testing.T) {
11984
maxSize := 2000
12085
pool := New(1024, maxSize)
121-
if pool.maxSize != maxSize {
122-
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
123-
}
86+
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
12487
buf := pool.Get(64)
125-
if len(*buf) != 64 {
126-
t.Fatalf("unexpected buf length: %d", len(*buf))
127-
}
128-
if cap(*buf) != 1024 {
129-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
130-
}
88+
require.Len(t, *buf, 64, "unexpected buf length")
89+
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
13190
pool.Put(buf)
13291

13392
buf = pool.Get(2001)
134-
if len(*buf) != 2001 {
135-
t.Fatalf("unexpected buf length: %d", len(*buf))
136-
}
137-
if cap(*buf) != 2001 {
138-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
139-
}
93+
require.Len(t, *buf, 2001, "unexpected buf length")
94+
require.Equal(t, 2001, cap(*buf), "unexpected buf cap")
14095
pool.Put(buf)
14196
}
14297

14398
func TestPoolMaxSizeLessThanMinSize(t *testing.T) {
144-
defer func() {
145-
if r := recover(); r == nil {
146-
t.Errorf("Expected the code to panic")
147-
}
148-
}()
149-
150-
New(15000, 1024)
99+
assert.Panics(t, func() { New(15000, 1024) })
151100
}
152101

153102
func TestPoolWeirdMaxSize(t *testing.T) {
154103
maxSize := 15000
155104
pool := New(1024, maxSize)
156-
if pool.maxSize != maxSize {
157-
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
158-
}
105+
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
159106

160107
buf := pool.Get(14000)
161-
if len(*buf) != 14000 {
162-
t.Fatalf("unexpected buf length: %d", len(*buf))
163-
}
164-
if cap(*buf) != 15000 {
165-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
166-
}
108+
require.Len(t, *buf, 14000, "unexpected buf length")
109+
require.Equal(t, 15000, cap(*buf), "unexpected buf cap")
167110
pool.Put(buf)
168111

169112
buf = pool.Get(16383)
170-
if len(*buf) != 16383 {
171-
t.Fatalf("unexpected buf length: %d", len(*buf))
172-
}
173-
if cap(*buf) != 16383 {
174-
t.Fatalf("unexpected buf cap: %d", cap(*buf))
175-
}
113+
require.Len(t, *buf, 16383, "unexpected buf length")
114+
require.Equal(t, 16383, cap(*buf), "unexpected buf cap")
176115
pool.Put(buf)
177116
}
178117

179118
func TestFuzz(t *testing.T) {
180119
maxTestSize := 16384
181-
for i := 0; i < 20000; i++ {
120+
for range 20000 {
182121
minSize := rand.Intn(maxTestSize)
183122
if minSize == 0 {
184123
minSize = 1
@@ -187,18 +126,12 @@ func TestFuzz(t *testing.T) {
187126
p := New(minSize, maxSize)
188127
bufSize := rand.Intn(maxTestSize)
189128
buf := p.Get(bufSize)
190-
if len(*buf) != bufSize {
191-
t.Fatalf("Invalid length %d, expected %d", len(*buf), bufSize)
192-
}
129+
require.Len(t, *buf, bufSize, "unexpected buf length")
193130
sPool := p.findPool(bufSize)
194131
if sPool == nil {
195-
if cap(*buf) != len(*buf) {
196-
t.Fatalf("Invalid cap %d, expected %d", cap(*buf), len(*buf))
197-
}
132+
require.Equal(t, len(*buf), cap(*buf), "unexpected buf cap")
198133
} else {
199-
if cap(*buf) != sPool.size {
200-
t.Fatalf("Invalid cap %d, expected %d", cap(*buf), sPool.size)
201-
}
134+
require.Equal(t, sPool.size, cap(*buf), "unexpected buf cap")
202135
}
203136
p.Put(buf)
204137
}

go/cache/theine/bf/bf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (d *Bloomfilter) EnsureCapacity(capacity int) {
5454
func (d *Bloomfilter) Exist(h uint64) bool {
5555
h1, h2 := uint32(h), uint32(h>>32)
5656
var o uint = 1
57-
for i := uint32(0); i < d.K; i++ {
57+
for i := range d.K {
5858
o &= d.Filter.get((h1 + (i * h2)) & (d.M - 1))
5959
}
6060
return o == 1
@@ -65,7 +65,7 @@ func (d *Bloomfilter) Exist(h uint64) bool {
6565
func (d *Bloomfilter) Insert(h uint64) bool {
6666
h1, h2 := uint32(h), uint32(h>>32)
6767
var o uint = 1
68-
for i := uint32(0); i < d.K; i++ {
68+
for i := range d.K {
6969
o &= d.Filter.getset((h1 + (i * h2)) & (d.M - 1))
7070
}
7171
return o == 1

go/cache/theine/list_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestList(t *testing.T) {
2828
l := NewList[StringKey, string](5, LIST_PROBATION)
2929
require.Equal(t, uint(5), l.capacity)
3030
require.Equal(t, LIST_PROBATION, l.listType)
31-
for i := 0; i < 5; i++ {
31+
for i := range 5 {
3232
evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1))
3333
require.Nil(t, evicted)
3434
}
@@ -42,15 +42,15 @@ func TestList(t *testing.T) {
4242
require.Equal(t, "5/4/3/2/1", l.display())
4343
require.Equal(t, "1/2/3/4/5", l.displayReverse())
4444

45-
for i := 0; i < 5; i++ {
45+
for i := range 5 {
4646
entry := l.PopTail()
4747
require.Equal(t, StringKey(fmt.Sprintf("%d", i+1)), entry.key)
4848
}
4949
entry := l.PopTail()
5050
require.Nil(t, entry)
5151

5252
var entries []*Entry[StringKey, string]
53-
for i := 0; i < 5; i++ {
53+
for i := range 5 {
5454
new := NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1)
5555
evicted := l.PushFront(new)
5656
entries = append(entries, new)
@@ -76,13 +76,13 @@ func TestListCountCost(t *testing.T) {
7676
l := NewList[StringKey, string](100, LIST_PROBATION)
7777
require.Equal(t, uint(100), l.capacity)
7878
require.Equal(t, LIST_PROBATION, l.listType)
79-
for i := 0; i < 5; i++ {
79+
for i := range 5 {
8080
evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 20))
8181
require.Nil(t, evicted)
8282
}
8383
require.Equal(t, 100, l.len)
8484
require.Equal(t, 5, l.count)
85-
for i := 0; i < 3; i++ {
85+
for range 3 {
8686
entry := l.PopTail()
8787
require.NotNil(t, entry)
8888
}

0 commit comments

Comments
 (0)