1313* limitations under the License.
1414*/
1515
16+ using System . Collections . Generic ;
1617using FluentAssertions ;
18+ using FluentAssertions . Common ;
1719using MongoDB . Bson ;
20+ using MongoDB . Bson . TestHelpers ;
1821using MongoDB . Bson . TestHelpers . XunitExtensions ;
22+ using MongoDB . Driver . Core ;
23+ using MongoDB . Driver . Core . Events ;
1924using MongoDB . Driver . Core . Misc ;
2025using MongoDB . Driver . Core . Operations ;
2126using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
@@ -53,6 +58,37 @@ public void Cursor_should_not_throw_exception_after_double_close([Values(false,
5358 }
5459 }
5560
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+
5692 //private methods
5793 private IMongoClient CreateClient( )
5894 {
@@ -65,4 +101,10 @@ private void DropCollection(IMongoClient client, string databaseName, string col
65101 client. GetDatabase ( databaseName ) . DropCollection ( collectionName ) ;
66102 }
67103 }
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