Skip to content
This repository was archived by the owner on Apr 20, 2019. It is now read-only.

Commit eedc633

Browse files
committed
CSHARP-2312: ChangeStream JSON tests should be run using the async API also.
1 parent 10f3540 commit eedc633

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

tests/MongoDB.Driver.Tests/Specifications/change-streams/ChangeStreamTestRunner.cs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Run(JsonDrivenTestCase testCase)
5151
private void Run(BsonDocument shared, BsonDocument test)
5252
{
5353
JsonDrivenHelper.EnsureAllFieldsAreValid(shared, "_path", "database_name", "database2_name", "collection_name", "collection2_name", "tests");
54-
JsonDrivenHelper.EnsureAllFieldsAreValid(test, "description", "minServerVersion", "topology", "target", "changeStreamPipeline", "changeStreamOptions", "operations", "expectations", "result");
54+
JsonDrivenHelper.EnsureAllFieldsAreValid(test, "description", "minServerVersion", "topology", "target", "changeStreamPipeline", "changeStreamOptions", "operations", "expectations", "result", "async");
5555

5656
if (test.Contains("minServerVersion"))
5757
{
@@ -81,12 +81,13 @@ private void Run(BsonDocument shared, BsonDocument test)
8181
{
8282
try
8383
{
84-
using (var cursor = Watch(client, test))
84+
var async = test["async"].AsBoolean;
85+
using (var cursor = Watch(client, test, async))
8586
{
8687
var globalClient = DriverTestConfiguration.Client;
8788
ExecuteOperations(globalClient, test["operations"].AsBsonArray);
8889

89-
actualResult = ReadChangeStreamDocuments(cursor, test);
90+
actualResult = ReadChangeStreamDocuments(cursor, test, async);
9091
actualEvents = GetEvents(eventCapturer);
9192
}
9293
}
@@ -173,7 +174,7 @@ private DisposableMongoClient CreateDisposableClient(EventCapturer eventCapturer
173174
});
174175
}
175176

176-
private IAsyncCursor<ChangeStreamDocument<BsonDocument>> Watch(IMongoClient client, BsonDocument test)
177+
private IAsyncCursor<ChangeStreamDocument<BsonDocument>> Watch(IMongoClient client, BsonDocument test, bool async)
177178
{
178179
var target = test["target"].AsString;
179180
var stages = test["changeStreamPipeline"].AsBsonArray.Cast<BsonDocument>();
@@ -182,19 +183,40 @@ private IAsyncCursor<ChangeStreamDocument<BsonDocument>> Watch(IMongoClient clie
182183

183184
if (target == "client")
184185
{
185-
return client.Watch(pipeline, options);
186+
if (async)
187+
{
188+
return client.WatchAsync(pipeline, options).GetAwaiter().GetResult();
189+
}
190+
else
191+
{
192+
return client.Watch(pipeline, options);
193+
}
186194
}
187195

188196
var database = client.GetDatabase(_databaseName);
189197
if (target == "database")
190198
{
191-
return database.Watch(pipeline, options);
199+
if (async)
200+
{
201+
return database.WatchAsync(pipeline, options).GetAwaiter().GetResult();
202+
}
203+
else
204+
{
205+
return database.Watch(pipeline, options);
206+
}
192207
}
193208

194209
var collection = database.GetCollection<BsonDocument>(_collectionName);
195210
if (target == "collection")
196211
{
197-
return collection.Watch(pipeline, options);
212+
if (async)
213+
{
214+
return collection.WatchAsync(pipeline, options).GetAwaiter().GetResult();
215+
}
216+
else
217+
{
218+
return collection.Watch(pipeline, options);
219+
}
198220
}
199221

200222
throw new FormatException($"Invalid target: \"{target}\".");
@@ -237,12 +259,12 @@ private List<CommandStartedEvent> GetEvents(EventCapturer eventCapturer)
237259
return events;
238260
}
239261

240-
private List<ChangeStreamDocument<BsonDocument>> ReadChangeStreamDocuments(IAsyncCursor<ChangeStreamDocument<BsonDocument>> cursor, BsonDocument test)
262+
private List<ChangeStreamDocument<BsonDocument>> ReadChangeStreamDocuments(IAsyncCursor<ChangeStreamDocument<BsonDocument>> cursor, BsonDocument test, bool async)
241263
{
242264
var result = new List<ChangeStreamDocument<BsonDocument>>();
243265
var expectedNumberOfDocuments = test["result"]["success"].AsBsonArray.Count;
244266

245-
while (cursor.MoveNext())
267+
while (async ? cursor.MoveNextAsync().GetAwaiter().GetResult() : cursor.MoveNext())
246268
{
247269
result.AddRange(cursor.Current);
248270

@@ -405,6 +427,20 @@ protected override string PathPrefix
405427
}
406428
}
407429

430+
// protected methods
431+
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
432+
{
433+
foreach (var testCase in base.CreateTestCases(document))
434+
{
435+
foreach (var async in new[] { false, true })
436+
{
437+
var name = $"{testCase.Name}:async={async}";
438+
var test = testCase.Test.DeepClone().AsBsonDocument.Add("async", async);
439+
yield return new JsonDrivenTestCase(name, testCase.Shared, test);
440+
}
441+
}
442+
}
443+
408444
protected override bool ShouldReadJsonDocument(string path)
409445
{
410446
return base.ShouldReadJsonDocument(path); // && path.EndsWith("change-streams.json");

0 commit comments

Comments
 (0)