Skip to content

Commit c0ea843

Browse files
committed
CSHARP-2119: Test that killCursors really succeeds
1 parent b30e4ff commit c0ea843

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

tests/MongoDB.Driver.Core.Tests/Core/Operations/CursorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System.Threading;
1716
using MongoDB.Bson;
1817
using MongoDB.Bson.Serialization.Serializers;
1918
using MongoDB.Driver.Core.Bindings;

tests/MongoDB.Driver.Tests/AsyncCursorTests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using FluentAssertions;
18+
using FluentAssertions.Common;
1719
using MongoDB.Bson;
20+
using MongoDB.Bson.TestHelpers;
1821
using MongoDB.Bson.TestHelpers.XunitExtensions;
22+
using MongoDB.Driver.Core;
23+
using MongoDB.Driver.Core.Events;
1924
using MongoDB.Driver.Core.Misc;
2025
using MongoDB.Driver.Core.Operations;
2126
using 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

Comments
 (0)