13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System . Collections . Generic ;
16
17
using FluentAssertions ;
18
+ using FluentAssertions . Common ;
17
19
using MongoDB . Bson ;
20
+ using MongoDB . Bson . TestHelpers ;
18
21
using MongoDB . Bson . TestHelpers . XunitExtensions ;
22
+ using MongoDB . Driver . Core ;
23
+ using MongoDB . Driver . Core . Events ;
19
24
using MongoDB . Driver . Core . Misc ;
20
25
using MongoDB . Driver . Core . Operations ;
21
26
using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
@@ -53,6 +58,37 @@ public void Cursor_should_not_throw_exception_after_double_close([Values(false,
53
58
}
54
59
}
55
60
61
+ [ SkippableFact ]
62
+ public void KillCursor_should_actually_work ( )
63
+ {
64
+ RequireServer . Check ( ) . Supports ( Feature . KillCursorsCommand ) ;
65
+ var eventCapturer = new EventCapturer ( ) . Capture < CommandSucceededEvent > ( x => x . CommandName . Equals ( "killCursors" ) ) ;
66
+ using ( var client = DriverTestConfiguration. CreateDisposableClient ( eventCapturer ) )
67
+ {
68
+ IAsyncCursor < BsonDocument > cursor ;
69
+ var database = client . GetDatabase ( "test ") ;
70
+ var collection = database. GetCollection< BsonDocument> ( GetType( ) . Name) ;
71
+ var documents = new List < BsonDocument > ( ) ;
72
+ for ( int i = 0 ; i < 1000 ; i++ )
73
+ {
74
+ documents . Add ( new BsonDocument ( "x", i) ) ;
75
+ }
76
+
77
+ collection. InsertMany( documents) ;
78
+ cursor = collection . FindSync ( "{ } ") ;
79
+ cursor . MoveNext ( ) ;
80
+
81
+ var cursorId = ( ( AsyncCursor < BsonDocument > ) cursor) . _cursorId( ) ;
82
+ cursorId . Should ( ) . NotBe ( 0 ) ;
83
+ cursor . Dispose ( ) ;
84
+
85
+ var desiredResult = BsonDocument . Parse ( $"{ { \" cursorsKilled \" : [ { cursorId} ] , \" cursorsNotFound \" : [ ] , " +
86
+ $"\" cursorsAlive \" : [ ] , \" cursorsUnknown \" : [ ] , \" ok \" : 1.0 } } ") ;
87
+ var result = ( ( CommandSucceededEvent ) eventCapturer . Events [ 0 ] ) . Reply ;
88
+ result. IsSameOrEqualTo ( desiredResult ) ;
89
+ }
90
+ }
91
+
56
92
//private methods
57
93
private IMongoClient CreateClient( )
58
94
{
@@ -65,4 +101,10 @@ private void DropCollection(IMongoClient client, string databaseName, string col
65
101
client. GetDatabase ( databaseName ) . DropCollection ( collectionName ) ;
66
102
}
67
103
}
68
- }
104
+
105
+ public static class AsyncCursorReflector
106
+ {
107
+ public static long _cursorId( this AsyncCursor< BsonDocument > obj ) =>
108
+ ( long ) Reflector . GetFieldValue ( obj , nameof ( _cursorId ) ) ;
109
+ }
110
+ }
0 commit comments