@@ -165,6 +165,58 @@ func TestCursor(t *testing.T) {
165
165
assertCursorBatchLength (mt , cursor , len (getMoreBatch )- 1 )
166
166
})
167
167
})
168
+ mt .RunOpts ("all" , noClientOpts , func (mt * mtest.T ) {
169
+ failpointOpts := mtest .NewOptions ().Topologies (mtest .ReplicaSet ).MinServerVersion ("4.0" )
170
+ mt .RunOpts ("getMore error" , failpointOpts , func (mt * mtest.T ) {
171
+ failpointData := mtest.FailPointData {
172
+ FailCommands : []string {"getMore" },
173
+ ErrorCode : 100 ,
174
+ }
175
+ mt .SetFailPoint (mtest.FailPoint {
176
+ ConfigureFailPoint : "failCommand" ,
177
+ Mode : "alwaysOn" ,
178
+ Data : failpointData ,
179
+ })
180
+ initCollection (mt , mt .Coll )
181
+ cursor , err := mt .Coll .Find (mtest .Background , bson.D {}, options .Find ().SetBatchSize (2 ))
182
+ assert .Nil (mt , err , "Find error: %v" , err )
183
+ defer cursor .Close (mtest .Background )
184
+
185
+ var docs []bson.D
186
+ err = cursor .All (context .Background (), & docs )
187
+ assert .NotNil (mt , err , "expected change stream error, got nil" )
188
+
189
+ // make sure that a mongo.CommandError is returned instead of a driver.Error
190
+ mongoErr , ok := err .(mongo.CommandError )
191
+ assert .True (mt , ok , "expected mongo.CommandError, got: %T" , err )
192
+ assert .Equal (mt , failpointData .ErrorCode , mongoErr .Code , "expected code %v, got: %v" , failpointData .ErrorCode , mongoErr .Code )
193
+ })
194
+ })
195
+ mt .RunOpts ("close" , noClientOpts , func (mt * mtest.T ) {
196
+ failpointOpts := mtest .NewOptions ().Topologies (mtest .ReplicaSet ).MinServerVersion ("4.0" )
197
+ mt .RunOpts ("killCursors error" , failpointOpts , func (mt * mtest.T ) {
198
+ failpointData := mtest.FailPointData {
199
+ FailCommands : []string {"killCursors" },
200
+ ErrorCode : 100 ,
201
+ }
202
+ mt .SetFailPoint (mtest.FailPoint {
203
+ ConfigureFailPoint : "failCommand" ,
204
+ Mode : "alwaysOn" ,
205
+ Data : failpointData ,
206
+ })
207
+ initCollection (mt , mt .Coll )
208
+ cursor , err := mt .Coll .Find (mtest .Background , bson.D {}, options .Find ().SetBatchSize (2 ))
209
+ assert .Nil (mt , err , "Find error: %v" , err )
210
+
211
+ err = cursor .Close (mtest .Background )
212
+ assert .NotNil (mt , err , "expected change stream error, got nil" )
213
+
214
+ // make sure that a mongo.CommandError is returned instead of a driver.Error
215
+ mongoErr , ok := err .(mongo.CommandError )
216
+ assert .True (mt , ok , "expected mongo.CommandError, got: %T" , err )
217
+ assert .Equal (mt , failpointData .ErrorCode , mongoErr .Code , "expected code %v, got: %v" , failpointData .ErrorCode , mongoErr .Code )
218
+ })
219
+ })
168
220
}
169
221
170
222
type tryNextCursor interface {
@@ -198,12 +250,13 @@ func verifyOneGetmoreSent(mt *mtest.T, cursor tryNextCursor) {
198
250
199
251
// should be called in a test run with a mock deployment
200
252
func tryNextGetmoreError (mt * mtest.T , cursor tryNextCursor ) {
201
- getMoreRes := mtest . CreateCommandErrorResponse ( mtest.CommandError {
253
+ testErr := mtest.CommandError {
202
254
Code : 100 ,
203
255
Message : "getMore error" ,
204
256
Name : "CursorError" ,
205
257
Labels : []string {"NonResumableChangeStreamError" },
206
- })
258
+ }
259
+ getMoreRes := mtest .CreateCommandErrorResponse (testErr )
207
260
mt .AddMockResponses (getMoreRes )
208
261
209
262
// first call to TryNext should return false because first batch was empty so batch cursor returns false
@@ -215,6 +268,14 @@ func tryNextGetmoreError(mt *mtest.T, cursor tryNextCursor) {
215
268
216
269
err := cursor .Err ()
217
270
assert .NotNil (mt , err , "expected change stream error, got nil" )
271
+
272
+ // make sure that a mongo.CommandError is returned instead of a driver.Error
273
+ mongoErr , ok := err .(mongo.CommandError )
274
+ assert .True (mt , ok , "expected mongo.CommandError, got: %T" , err )
275
+ assert .Equal (mt , testErr .Code , mongoErr .Code , "expected code %v, got: %v" , testErr .Code , mongoErr .Code )
276
+ assert .Equal (mt , testErr .Message , mongoErr .Message , "expected message %v, got: %v" , testErr .Message , mongoErr .Message )
277
+ assert .Equal (mt , testErr .Name , mongoErr .Name , "expected name %v, got: %v" , testErr .Name , mongoErr .Name )
278
+ assert .Equal (mt , testErr .Labels , mongoErr .Labels , "expected labels %v, got: %v" , testErr .Labels , mongoErr .Labels )
218
279
}
219
280
220
281
func assertCursorBatchLength (mt * mtest.T , cursor * mongo.Cursor , expected int ) {
0 commit comments