Skip to content

Commit d1c92a8

Browse files
authored
[Improvement] Use bulks in tests (#263)
1 parent c4be366 commit d1c92a8

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

test/backup_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -869,18 +869,12 @@ func TestBackupRestoreWithViews(t *testing.T) {
869869
go func(i int) {
870870
defer wg.Done()
871871

872-
for j := 0; j < numDocs; j++ {
873-
874-
book := BookWithAuthor{
872+
sendBulks(t, col, ctx, func(t *testing.T, j int) interface{} {
873+
return BookWithAuthor{
875874
Title: fmt.Sprintf("Hello World - %d", j),
876875
Author: fmt.Sprintf("Author - %d", i),
877876
}
878-
879-
_, err := col.CreateDocument(ctx, book)
880-
if err != nil {
881-
t.Fatalf("Failed to create document %s", describe(err))
882-
}
883-
}
877+
}, numDocs)
884878
}(k)
885879
}
886880
wg.Wait()

test/clean.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ type remove interface {
3434
}
3535

3636
func clean(t *testing.T, ctx context.Context, col remove) {
37+
if col == nil {
38+
return
39+
}
40+
3741
require.NoError(t, col.Remove(ctx))
3842
}

test/cluster_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ func TestClusterMoveShard(t *testing.T) {
264264
col, err := db.CreateCollection(ctx, "test_move_shard", &driver.CreateCollectionOptions{
265265
NumberOfShards: 12,
266266
})
267-
defer clean(t, ctx, col)
268267
if err != nil {
269268
t.Fatalf("CreateCollection failed: %s", describe(err))
270269
}
270+
defer clean(t, ctx, col)
271271
h, err := cl.Health(ctx)
272272
if err != nil {
273273
t.Fatalf("Health failed: %s", describe(err))
@@ -365,10 +365,10 @@ func TestClusterResignLeadership(t *testing.T) {
365365
NumberOfShards: 12,
366366
ReplicationFactor: 2,
367367
})
368-
defer clean(t, ctx, col)
369368
if err != nil {
370369
t.Fatalf("CreateCollection failed: %s", describe(err))
371370
}
371+
defer clean(t, ctx, col)
372372
inv, err := cl.DatabaseInventory(ctx, db)
373373
if err != nil {
374374
t.Fatalf("DatabaseInventory failed: %s", describe(err))
@@ -453,21 +453,21 @@ func TestClusterMoveShardWithViews(t *testing.T) {
453453
col, err := db.CreateCollection(ctx, "test_move_shard_with_view", &driver.CreateCollectionOptions{
454454
NumberOfShards: 12,
455455
})
456-
clean(t, ctx, col)
457456
if err != nil {
458457
t.Fatalf("CreateCollection failed: %s", describe(err))
459458
}
459+
defer clean(t, ctx, col)
460460
opts := &driver.ArangoSearchViewProperties{
461461
Links: driver.ArangoSearchLinks{
462462
"test_move_shard_with_view": driver.ArangoSearchElementProperties{},
463463
},
464464
}
465465
viewName := "test_move_shard_view"
466466
view, err := db.CreateArangoSearchView(ctx, viewName, opts)
467-
clean(t, ctx, view)
468467
if err != nil {
469468
t.Fatalf("Failed to create view '%s': %s", viewName, describe(err))
470469
}
470+
defer clean(t, ctx, view)
471471
h, err := cl.Health(ctx)
472472
if err != nil {
473473
t.Fatalf("Health failed: %s", describe(err))

test/cursor_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,10 @@ func TestCreateStreamCursor(t *testing.T) {
348348
}
349349

350350
// This might take a few seconds
351-
for i := 0; i < 10000; i++ {
352-
user := UserDoc{Name: "John", Age: i}
353-
if _, err := col.CreateDocument(ctx, user); err != nil {
354-
t.Fatalf("Expected success, got %s", describe(err))
355-
}
356-
}
351+
docs := 10000
352+
sendBulks(t, col, ctx, func(t *testing.T, i int) interface{} {
353+
return UserDoc{Name: "John", Age: i}
354+
}, docs)
357355
t.Log("Completed inserting 10k docs")
358356

359357
const expectedResults int = 10 * 10000

test/server_mode_auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestServerModeAndGrants(t *testing.T) {
6262
db := ensureDatabase(ctx, c, "_system", nil, t)
6363
colName := "server_mode_and_grants_test1"
6464
col := ensureCollection(ctx, db, colName, nil, t)
65-
clean(t, ctx, col)
65+
defer clean(t, ctx, col)
6666

6767
// Get database & collection access
6868
defaultDBAccess, err := u.GetDatabaseAccess(ctx, db)

test/util.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package test
2424

2525
import (
26+
"context"
2627
"encoding/hex"
2728
"encoding/json"
2829
"fmt"
@@ -32,6 +33,8 @@ import (
3233
"testing"
3334
"time"
3435

36+
"github.com/stretchr/testify/require"
37+
3538
driver "github.com/arangodb/go-driver"
3639
)
3740

@@ -147,3 +150,41 @@ func retry(interval, timeout time.Duration, f func() error) error {
147150
}
148151
}
149152
}
153+
154+
const bulkSize = 1000
155+
156+
func sendBulks(t *testing.T, col driver.Collection, ctx context.Context, creator func(t *testing.T, i int) interface{}, size int) {
157+
current := 0
158+
t.Logf("Creating %d documents", size)
159+
160+
for {
161+
t.Logf("Created %d/%d documents", current, size)
162+
stepSize := min(bulkSize, size-current)
163+
if stepSize == 0 {
164+
return
165+
}
166+
167+
objs := make([]interface{}, min(bulkSize, stepSize))
168+
for i := 0; i < stepSize; i++ {
169+
objs[i] = creator(t, current+i)
170+
}
171+
172+
_, _, err := col.CreateDocuments(ctx, objs)
173+
t.Logf("Creating %d documents", len(objs))
174+
require.NoError(t, err)
175+
176+
current += stepSize
177+
}
178+
}
179+
180+
func min(max int, ints ...int) int {
181+
z := max
182+
183+
for _, i := range ints {
184+
if z > i {
185+
z = i
186+
}
187+
}
188+
189+
return z
190+
}

0 commit comments

Comments
 (0)