Skip to content

Commit

Permalink
Simplify tailable cursor tests (#3854)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Dec 15, 2023
1 parent 9a15f1a commit 39aee11
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 69 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ linters-settings:
common:
files:
- "**/internal/backends/**/*.go"
- "**/internal/clientconn/cursor/*.go"
deny:
- pkg: github.com/FerretDB/FerretDB/internal/handler/common
handlererrors:
Expand Down
116 changes: 48 additions & 68 deletions integration/cursors/tailable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"github.com/FerretDB/FerretDB/integration/shareddata"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/testutil/testfail"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

func TestCursorsTailableErrors(t *testing.T) {
Expand Down Expand Up @@ -57,11 +55,7 @@ func TestCursorsTailableErrors(t *testing.T) {
t.Run("GetMoreDifferentCollection", func(tt *testing.T) {
tt.Parallel()

var t testtb.TB = tt

if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

s := setup.SetupWithOpts(t, nil)

Expand All @@ -76,7 +70,7 @@ func TestCursorsTailableErrors(t *testing.T) {
bsonArr, arr := integration.GenerateDocuments(0, 3)

_, err = collection.InsertMany(ctx, bsonArr)
require.NoError(tt, err)
require.NoError(t, err)

var cursorID any

Expand Down Expand Up @@ -152,10 +146,7 @@ func TestCursorsTailable(t *testing.T) {
var cursorID any

t.Run("FirstBatch", func(tt *testing.T) {
var t testtb.TB = tt
if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

cmd := bson.D{
{"find", collection.Name()},
Expand All @@ -182,10 +173,8 @@ func TestCursorsTailable(t *testing.T) {
}

t.Run("GetMore", func(tt *testing.T) {
var t testtb.TB = tt
if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

for i := 0; i < 2; i++ {
var res bson.D
err = collection.Database().RunCommand(ctx, getMoreCmd).Decode(&res)
Expand All @@ -202,10 +191,8 @@ func TestCursorsTailable(t *testing.T) {
})

t.Run("GetMoreEmpty", func(tt *testing.T) {
var t testtb.TB = tt
if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

var res bson.D
err = collection.Database().RunCommand(ctx, getMoreCmd).Decode(&res)
require.NoError(t, err)
Expand All @@ -216,10 +203,8 @@ func TestCursorsTailable(t *testing.T) {
})

t.Run("GetMoreNewDoc", func(tt *testing.T) {
var t testtb.TB = tt
if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

newDoc := bson.D{{"_id", "new"}}
_, err = collection.InsertOne(ctx, newDoc)
require.NoError(t, err)
Expand All @@ -233,14 +218,12 @@ func TestCursorsTailable(t *testing.T) {
assert.Equal(t, cursorID, nextID)

require.Equal(t, 1, nextBatch.Len())
require.Equal(t, integration.ConvertDocument(tt, newDoc), must.NotFail(nextBatch.Get(0)))
require.Equal(t, integration.ConvertDocument(t, newDoc), must.NotFail(nextBatch.Get(0)))
})

t.Run("GetMoreEmptyAfterInsertion", func(tt *testing.T) {
var t testtb.TB = tt
if !setup.IsMongoDB(tt) {
t = testfail.Expected(tt, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

var res bson.D
err = collection.Database().RunCommand(ctx, getMoreCmd).Decode(&res)
require.NoError(t, err)
Expand All @@ -251,28 +234,25 @@ func TestCursorsTailable(t *testing.T) {
})
}

func TestCursorsTailableTwoCursorsSameCollection(t *testing.T) {
t.Parallel()
func TestCursorsTailableTwoCursorsSameCollection(tt *testing.T) {
tt.Parallel()

var tt testtb.TB = t
if !setup.IsMongoDB(tt) {
tt = testfail.Expected(t, "https://github.com/FerretDB/FerretDB/issues/2283")
}
t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/2283")

s := setup.SetupWithOpts(tt, nil)
s := setup.SetupWithOpts(t, nil)

db, ctx := s.Collection.Database(), s.Ctx

opts := options.CreateCollection().SetCapped(true).SetSizeInBytes(10000)
err := db.CreateCollection(s.Ctx, tt.Name(), opts)
require.NoError(tt, err)
err := db.CreateCollection(s.Ctx, t.Name(), opts)
require.NoError(t, err)

collection := db.Collection(tt.Name())
collection := db.Collection(t.Name())

bsonArr, arr := integration.GenerateDocuments(0, 50)

_, err = collection.InsertMany(ctx, bsonArr)
require.NoError(tt, err)
require.NoError(t, err)

var cursorID1, cursorID2 any

Expand All @@ -285,24 +265,24 @@ func TestCursorsTailableTwoCursorsSameCollection(t *testing.T) {
var res bson.D

err = collection.Database().RunCommand(ctx, cmd).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

var firstBatch1 *types.Array
firstBatch1, cursorID1 = getFirstBatch(tt, res)
firstBatch1, cursorID1 = getFirstBatch(t, res)

err = collection.Database().RunCommand(ctx, cmd).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

var firstBatch2 *types.Array
firstBatch2, cursorID2 = getFirstBatch(tt, res)
firstBatch2, cursorID2 = getFirstBatch(t, res)

expectedFirstBatch := integration.ConvertDocuments(tt, arr[:1])
expectedFirstBatch := integration.ConvertDocuments(t, arr[:1])

require.Equal(tt, len(expectedFirstBatch), firstBatch1.Len())
require.Equal(tt, expectedFirstBatch[0], must.NotFail(firstBatch1.Get(0)))
require.Equal(t, len(expectedFirstBatch), firstBatch1.Len())
require.Equal(t, expectedFirstBatch[0], must.NotFail(firstBatch1.Get(0)))

require.Equal(tt, len(expectedFirstBatch), firstBatch2.Len())
require.Equal(tt, expectedFirstBatch[0], must.NotFail(firstBatch2.Get(0)))
require.Equal(t, len(expectedFirstBatch), firstBatch2.Len())
require.Equal(t, expectedFirstBatch[0], must.NotFail(firstBatch2.Get(0)))

getMoreCmd1 := bson.D{
{"getMore", cursorID1},
Expand All @@ -318,39 +298,39 @@ func TestCursorsTailableTwoCursorsSameCollection(t *testing.T) {

for i := 0; i < 49; i++ {
err = collection.Database().RunCommand(ctx, getMoreCmd1).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

nextBatch1, nextID1 := getNextBatch(tt, res)
nextBatch1, nextID1 := getNextBatch(t, res)

err = collection.Database().RunCommand(ctx, getMoreCmd2).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

nextBatch2, nextID2 := getNextBatch(tt, res)
nextBatch2, nextID2 := getNextBatch(t, res)

expectedNextBatch := integration.ConvertDocuments(tt, arr[i+1:i+2])
expectedNextBatch := integration.ConvertDocuments(t, arr[i+1:i+2])

assert.Equal(tt, cursorID1, nextID1)
require.Equal(tt, len(expectedNextBatch), nextBatch1.Len())
require.Equal(tt, expectedNextBatch[0], must.NotFail(nextBatch1.Get(0)))
assert.Equal(t, cursorID1, nextID1)
require.Equal(t, len(expectedNextBatch), nextBatch1.Len())
require.Equal(t, expectedNextBatch[0], must.NotFail(nextBatch1.Get(0)))

assert.Equal(tt, cursorID2, nextID2)
require.Equal(tt, len(expectedNextBatch), nextBatch2.Len())
require.Equal(tt, expectedNextBatch[0], must.NotFail(nextBatch2.Get(0)))
assert.Equal(t, cursorID2, nextID2)
require.Equal(t, len(expectedNextBatch), nextBatch2.Len())
require.Equal(t, expectedNextBatch[0], must.NotFail(nextBatch2.Get(0)))
}

err = collection.Database().RunCommand(ctx, getMoreCmd1).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

nextBatch1, nextID1 := getNextBatch(tt, res)
nextBatch1, nextID1 := getNextBatch(t, res)

err = collection.Database().RunCommand(ctx, getMoreCmd2).Decode(&res)
require.NoError(tt, err)
require.NoError(t, err)

nextBatch2, nextID2 := getNextBatch(tt, res)
nextBatch2, nextID2 := getNextBatch(t, res)

require.Equal(tt, 0, nextBatch1.Len())
assert.Equal(tt, cursorID1, nextID1)
require.Equal(t, 0, nextBatch1.Len())
assert.Equal(t, cursorID1, nextID1)

require.Equal(tt, 0, nextBatch2.Len())
assert.Equal(tt, cursorID2, nextID2)
require.Equal(t, 0, nextBatch2.Len())
assert.Equal(t, cursorID2, nextID2)
}
3 changes: 2 additions & 1 deletion internal/util/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package debug
import (
"bytes"
"context"
"errors"
_ "expvar" // for metrics
"fmt"
"net"
Expand Down Expand Up @@ -108,7 +109,7 @@ func RunHandler(ctx context.Context, addr string, r prometheus.Registerer, l *za
l.Sugar().Infof("%s%s - %s", root, path, handlers[path])
}

if err := s.Serve(lis); err != http.ErrServerClosed {
if err := s.Serve(lis); !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
Expand Down

0 comments on commit 39aee11

Please sign in to comment.