Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an integration test for cursor not found #3768

Closed
wants to merge 19 commits into from
Prev Previous commit
Next Next commit
not a true test
  • Loading branch information
b1ron committed Nov 30, 2023
commit c0b8efcfe6ce49c8032db032e65808749001d085
89 changes: 18 additions & 71 deletions integration/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,97 +15,44 @@
package integration

import (
"context"
"sync"
"testing"

"github.com/AlekSi/pointer"
"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func TestCursorStress(t *testing.T) {
ctx := context.Background()
func TestCursor(t *testing.T) {
t.Parallel()

client, err := mongo.Connect(ctx, nil)
if err != nil {
t.Error(err)
}

defaultBatchSize := 101

// use large documents
provider := shareddata.BenchmarkSettingsDocuments
ctx, collection := setup.Setup(t, shareddata.Scalars)

iter := provider.NewIterator()

for {
docs, err := iterator.ConsumeValuesN(iter, defaultBatchSize)
require.NoError(t, err)

if docs == nil {
break
}

insertDocs := make([]any, len(docs))
for i := range insertDocs {
insertDocs[i] = docs[i]
}

_, err = client.Database("test").Collection("foo").InsertMany(ctx, insertDocs)
require.NoError(t, err)
opts := &options.FindOptions{
BatchSize: pointer.ToInt32(1),
}

t.Cleanup(func() { require.NoError(t, client.Database("test").Drop(ctx)) })
cur, err := collection.Find(ctx, bson.D{}, opts)
require.NoError(t, err)

var wg sync.WaitGroup

N := 10
wg.Add(1)

for i := 0; i < N; i++ {
wg.Add(1)
go func() {
defer wg.Done()

go func() {
defer wg.Done()

// create N clients
client, err := mongo.Connect(ctx, nil)
if err != nil {
t.Error(err)
}

coll := client.Database("test").Collection("foo")

opts := &options.FindOptions{
// set a small batch size to increase the frequency of getMores
BatchSize: pointer.ToInt32(20),
Sort: bson.D{{"_id", 1}},
}

cur, err := coll.Find(ctx, bson.D{}, opts)
require.NoError(t, err)

// iterate the cursor until it is exhausted or there is an error getting the next document
for {
if cur.TryNext(ctx) {
assert.True(t, cur.Next(ctx))
}

if err := cur.Err(); err != nil {
t.Error(err)
}
res := []bson.D{}
err := cur.All(ctx, &res)
require.NoError(t, err)
}()

if cur.ID() == 0 {
break
}
}
}()
}
res := []bson.D{}
err = cur.All(ctx, &res)
require.NoError(t, err)

wg.Wait()
}
Loading