Skip to content

Commit

Permalink
Add test for tailable cursor with non-capped collection (FerretDB#3677)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisersup authored and yonarw committed Nov 16, 2023
1 parent de4e904 commit 6b8d150
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
18 changes: 18 additions & 0 deletions integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,3 +1203,21 @@ func TestQueryShowRecordIDErrors(t *testing.T) {
})
}
}

func TestQueryTailableCursors(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t)
_, err := collection.InsertOne(ctx, bson.D{{"v", int32(42)}})
require.NoError(t, err)

expectedErr := mongo.CommandError{
Code: 2,
Message: "error processing query: ns=TestQueryTailableCursors.TestQueryTailableCursorsTree:" +
" $eq null\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection",
Name: "BadValue",
}

_, err = collection.Find(ctx, bson.D{{}}, options.Find().SetCursorType(options.Tailable))
AssertEqualCommandError(t, expectedErr, err)
}
2 changes: 1 addition & 1 deletion internal/handlers/common/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type FindParams struct {
SingleBatch bool `ferretdb:"singleBatch,opt"`
Comment string `ferretdb:"comment,opt"`
MaxTimeMS int64 `ferretdb:"maxTimeMS,opt,wholePositiveNumber"`
Tailable bool `ferretdb:"tailable,opt"`

Collation *types.Document `ferretdb:"collation,unimplemented"`
Let *types.Document `ferretdb:"let,unimplemented"`
Expand All @@ -52,7 +53,6 @@ type FindParams struct {

ReturnKey bool `ferretdb:"returnKey,unimplemented-non-default"`
ShowRecordId bool `ferretdb:"showRecordId,opt"`
Tailable bool `ferretdb:"tailable,unimplemented-non-default"`
OplogReplay bool `ferretdb:"oplogReplay,unimplemented-non-default"`
NoCursorTimeout bool `ferretdb:"noCursorTimeout,unimplemented-non-default"`
AwaitData bool `ferretdb:"awaitData,unimplemented-non-default"`
Expand Down
32 changes: 32 additions & 0 deletions internal/handlers/sqlite/msg_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
return nil, lazyerrors.Error(err)
}

if params.Tailable {
var res *backends.ListCollectionsResult
var collInfo *backends.CollectionInfo

res, err = db.ListCollections(ctx, nil)
if err != nil {
return nil, err
}

// TODO https://github.com/FerretDB/FerretDB/issues/3601
for _, coll := range res.Collections {
if coll.Name == params.Collection {
collInfo = &coll
}
}

if collInfo != nil && !collInfo.Capped() {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrBadValue,
fmt.Sprintf(
"error processing query: ns=%s.%sTree:"+
" $eq null\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection",
params.DB,
params.Collection,
),
"find",
)
}

return nil, common.Unimplemented(document, "tailable")
}

qp := &backends.QueryParams{
Comment: params.Comment,
}
Expand Down

0 comments on commit 6b8d150

Please sign in to comment.