Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion store/mongo/mongo_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (store *mongoStore) IterateMessages(beginSeqNum, endSeqNum int, cb func([]b
return err
}
}
return nil
return cursor.Err()
}

func (store *mongoStore) GetMessages(beginSeqNum, endSeqNum int) ([][]byte, error) {
Expand Down
57 changes: 57 additions & 0 deletions store/mongo/mongo_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/quickfixgo/quickfix/internal/testsuite"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)

// MongoStoreTestSuite runs all tests in the message.StoreTestSuite against the MongoStore implementation.
Expand Down Expand Up @@ -73,3 +75,58 @@ func (suite *MongoStoreTestSuite) TearDownTest() {
func TestMongoStoreTestSuite(t *testing.T) {
suite.Run(t, new(MongoStoreTestSuite))
}

func TestIterateMessagesCursorError(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
for _, count := range []int{0, 2} {
mt.Run(fmt.Sprintf("after_%d_messages", count), func(mt *mtest.T) {
store := &mongoStore{
db: mt.Client,
mongoDatabase: mt.DB.Name(),
messagesCollection: mt.Coll.Name(),
}
batch := make([]bson.D, count)
for i := range batch {
batch[i] = bson.D{{Key: "msgseq", Value: 7 + i}, {Key: "message", Value: []byte("stored message")}}
}
mt.AddMockResponses(
mtest.CreateCursorResponse(123, mt.DB.Name()+"."+mt.Coll.Name(), mtest.FirstBatch, batch...),
mtest.CreateCommandErrorResponse(mtest.CommandError{Code: 43, Name: "CursorNotFound", Message: "injected cursor failure"}),
mtest.CreateSuccessResponse(),
)

visited := 0
err := store.IterateMessages(7, 10, func([]byte) error {
visited++
return nil
})
require.Equal(mt, count, visited)
require.ErrorContains(mt, err, "injected cursor failure")
})
}
}

func TestIterateMessagesCursorExhausted(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
mt.Run("success", func(mt *mtest.T) {
store := &mongoStore{
db: mt.Client,
mongoDatabase: mt.DB.Name(),
messagesCollection: mt.Coll.Name(),
}
mt.AddMockResponses(mtest.CreateCursorResponse(
0,
mt.DB.Name()+"."+mt.Coll.Name(),
mtest.FirstBatch,
bson.D{{Key: "msgseq", Value: 7}, {Key: "message", Value: []byte("stored message")}},
))

visited := 0
err := store.IterateMessages(7, 10, func([]byte) error {
visited++
return nil
})
require.NoError(mt, err)
require.Equal(mt, 1, visited)
})
}
Loading