diff --git a/test/backup_test.go b/test/backup_test.go index 15f0db2e..dc717835 100644 --- a/test/backup_test.go +++ b/test/backup_test.go @@ -869,18 +869,12 @@ func TestBackupRestoreWithViews(t *testing.T) { go func(i int) { defer wg.Done() - for j := 0; j < numDocs; j++ { - - book := BookWithAuthor{ + sendBulks(t, col, ctx, func(t *testing.T, j int) interface{} { + return BookWithAuthor{ Title: fmt.Sprintf("Hello World - %d", j), Author: fmt.Sprintf("Author - %d", i), } - - _, err := col.CreateDocument(ctx, book) - if err != nil { - t.Fatalf("Failed to create document %s", describe(err)) - } - } + }, numDocs) }(k) } wg.Wait() diff --git a/test/clean.go b/test/clean.go index 9be9d862..59139e0b 100644 --- a/test/clean.go +++ b/test/clean.go @@ -34,5 +34,9 @@ type remove interface { } func clean(t *testing.T, ctx context.Context, col remove) { + if col == nil { + return + } + require.NoError(t, col.Remove(ctx)) } diff --git a/test/cluster_test.go b/test/cluster_test.go index 6a88a14f..38117833 100644 --- a/test/cluster_test.go +++ b/test/cluster_test.go @@ -264,10 +264,10 @@ func TestClusterMoveShard(t *testing.T) { col, err := db.CreateCollection(ctx, "test_move_shard", &driver.CreateCollectionOptions{ NumberOfShards: 12, }) - defer clean(t, ctx, col) if err != nil { t.Fatalf("CreateCollection failed: %s", describe(err)) } + defer clean(t, ctx, col) h, err := cl.Health(ctx) if err != nil { t.Fatalf("Health failed: %s", describe(err)) @@ -365,10 +365,10 @@ func TestClusterResignLeadership(t *testing.T) { NumberOfShards: 12, ReplicationFactor: 2, }) - defer clean(t, ctx, col) if err != nil { t.Fatalf("CreateCollection failed: %s", describe(err)) } + defer clean(t, ctx, col) inv, err := cl.DatabaseInventory(ctx, db) if err != nil { t.Fatalf("DatabaseInventory failed: %s", describe(err)) @@ -453,10 +453,10 @@ func TestClusterMoveShardWithViews(t *testing.T) { col, err := db.CreateCollection(ctx, "test_move_shard_with_view", &driver.CreateCollectionOptions{ NumberOfShards: 12, }) - clean(t, ctx, col) if err != nil { t.Fatalf("CreateCollection failed: %s", describe(err)) } + defer clean(t, ctx, col) opts := &driver.ArangoSearchViewProperties{ Links: driver.ArangoSearchLinks{ "test_move_shard_with_view": driver.ArangoSearchElementProperties{}, @@ -464,10 +464,10 @@ func TestClusterMoveShardWithViews(t *testing.T) { } viewName := "test_move_shard_view" view, err := db.CreateArangoSearchView(ctx, viewName, opts) - clean(t, ctx, view) if err != nil { t.Fatalf("Failed to create view '%s': %s", viewName, describe(err)) } + defer clean(t, ctx, view) h, err := cl.Health(ctx) if err != nil { t.Fatalf("Health failed: %s", describe(err)) diff --git a/test/cursor_test.go b/test/cursor_test.go index ba80cda4..de1e7358 100644 --- a/test/cursor_test.go +++ b/test/cursor_test.go @@ -348,12 +348,10 @@ func TestCreateStreamCursor(t *testing.T) { } // This might take a few seconds - for i := 0; i < 10000; i++ { - user := UserDoc{Name: "John", Age: i} - if _, err := col.CreateDocument(ctx, user); err != nil { - t.Fatalf("Expected success, got %s", describe(err)) - } - } + docs := 10000 + sendBulks(t, col, ctx, func(t *testing.T, i int) interface{} { + return UserDoc{Name: "John", Age: i} + }, docs) t.Log("Completed inserting 10k docs") const expectedResults int = 10 * 10000 diff --git a/test/server_mode_auth_test.go b/test/server_mode_auth_test.go index cd988060..b1d4eb8e 100644 --- a/test/server_mode_auth_test.go +++ b/test/server_mode_auth_test.go @@ -62,7 +62,7 @@ func TestServerModeAndGrants(t *testing.T) { db := ensureDatabase(ctx, c, "_system", nil, t) colName := "server_mode_and_grants_test1" col := ensureCollection(ctx, db, colName, nil, t) - clean(t, ctx, col) + defer clean(t, ctx, col) // Get database & collection access defaultDBAccess, err := u.GetDatabaseAccess(ctx, db) diff --git a/test/util.go b/test/util.go index 2fa40a91..892864fd 100644 --- a/test/util.go +++ b/test/util.go @@ -23,6 +23,7 @@ package test import ( + "context" "encoding/hex" "encoding/json" "fmt" @@ -32,6 +33,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + driver "github.com/arangodb/go-driver" ) @@ -147,3 +150,41 @@ func retry(interval, timeout time.Duration, f func() error) error { } } } + +const bulkSize = 1000 + +func sendBulks(t *testing.T, col driver.Collection, ctx context.Context, creator func(t *testing.T, i int) interface{}, size int) { + current := 0 + t.Logf("Creating %d documents", size) + + for { + t.Logf("Created %d/%d documents", current, size) + stepSize := min(bulkSize, size-current) + if stepSize == 0 { + return + } + + objs := make([]interface{}, min(bulkSize, stepSize)) + for i := 0; i < stepSize; i++ { + objs[i] = creator(t, current+i) + } + + _, _, err := col.CreateDocuments(ctx, objs) + t.Logf("Creating %d documents", len(objs)) + require.NoError(t, err) + + current += stepSize + } +} + +func min(max int, ints ...int) int { + z := max + + for _, i := range ints { + if z > i { + z = i + } + } + + return z +}