Skip to content

Commit ee85c62

Browse files
committed
Added test infrastructure
1 parent 7d32031 commit ee85c62

File tree

5 files changed

+263
-2
lines changed

5 files changed

+263
-2
lines changed

specifications/sessions/tests/snapshot-sessions.json

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,147 @@
988988
}
989989
}
990990
]
991+
},
992+
{
993+
"description": "Find operation with snapshot and snapshot time",
994+
"operations": [
995+
{
996+
"name": "find",
997+
"object": "collection0",
998+
"arguments": {
999+
"session": "session0",
1000+
"filter": {}
1001+
},
1002+
"expectResult": [
1003+
{
1004+
"_id": 1,
1005+
"x": 11
1006+
},
1007+
{
1008+
"_id": 2,
1009+
"x": 11
1010+
}
1011+
]
1012+
},
1013+
{
1014+
"name": "getSnapshotTime",
1015+
"object": "session0",
1016+
"saveResultAsEntity": "savedSnapshotTime"
1017+
},
1018+
{
1019+
"name": "insertOne",
1020+
"object": "collection0",
1021+
"arguments": {
1022+
"document": {
1023+
"_id": 3,
1024+
"x": 33
1025+
}
1026+
}
1027+
},
1028+
{
1029+
"name": "createEntities",
1030+
"object": "testRunner",
1031+
"arguments": {
1032+
"entities": [
1033+
{
1034+
"session": {
1035+
"id": "session2",
1036+
"client": "client0",
1037+
"sessionOptions": {
1038+
"snapshot": true,
1039+
"snapshotTime": "savedSnapshotTime"
1040+
}
1041+
}
1042+
}
1043+
]
1044+
}
1045+
},
1046+
{
1047+
"name": "find",
1048+
"object": "collection0",
1049+
"arguments": {
1050+
"session": "session2",
1051+
"filter": {}
1052+
},
1053+
"expectResult": [
1054+
{
1055+
"_id": 1,
1056+
"x": 11
1057+
},
1058+
{
1059+
"_id": 2,
1060+
"x": 11
1061+
}
1062+
]
1063+
},
1064+
{
1065+
"name": "find",
1066+
"object": "collection0",
1067+
"arguments": {
1068+
"filter": {}
1069+
},
1070+
"expectResult": [
1071+
{
1072+
"_id": 1,
1073+
"x": 11
1074+
},
1075+
{
1076+
"_id": 2,
1077+
"x": 11
1078+
},
1079+
{
1080+
"_id": 3,
1081+
"x": 33
1082+
}
1083+
]
1084+
}
1085+
],
1086+
"expectEvents": [
1087+
{
1088+
"client": "client0",
1089+
"events": [
1090+
{
1091+
"commandStartedEvent": {
1092+
"command": {
1093+
"find": "collection0",
1094+
"readConcern": {
1095+
"level": "snapshot",
1096+
"atClusterTime": {
1097+
"$$exists": false
1098+
}
1099+
}
1100+
},
1101+
"databaseName": "database0"
1102+
}
1103+
},
1104+
{
1105+
"commandStartedEvent": {
1106+
"command": {
1107+
"find": "collection0",
1108+
"readConcern": {
1109+
"level": "snapshot",
1110+
"atClusterTime": {
1111+
"$$matchesEntity": "savedSnapshotTime"
1112+
}
1113+
}
1114+
},
1115+
"databaseName": "database0"
1116+
}
1117+
},
1118+
{
1119+
"commandStartedEvent": {
1120+
"command": {
1121+
"find": "collection0",
1122+
"readConcern": {
1123+
"$$exists": false
1124+
}
1125+
},
1126+
"databaseName": "database0"
1127+
}
1128+
}
1129+
]
1130+
}
1131+
]
9911132
}
9921133
]
9931134
}

specifications/sessions/tests/snapshot-sessions.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,71 @@ tests:
480480
isError: true
481481
isClientError: true
482482
errorContains: Transactions are not supported in snapshot sessions
483+
484+
- description: Find operation with snapshot and snapshot time
485+
operations:
486+
- name: find
487+
object: collection0
488+
arguments:
489+
session: session0
490+
filter: {}
491+
expectResult:
492+
- { _id: 1, x: 11 }
493+
- { _id: 2, x: 11 }
494+
- name: getSnapshotTime
495+
object: session0
496+
saveResultAsEntity: &savedSnapshotTime savedSnapshotTime
497+
- name: insertOne
498+
object: collection0
499+
arguments:
500+
document: { _id: 3, x: 33 }
501+
- name: createEntities
502+
object: testRunner
503+
arguments:
504+
entities:
505+
- session:
506+
id: session2
507+
client: client0
508+
sessionOptions:
509+
snapshot: true
510+
snapshotTime: *savedSnapshotTime
511+
- name: find
512+
object: collection0
513+
arguments:
514+
session: session2
515+
filter: {}
516+
expectResult:
517+
- { _id: 1, x: 11 }
518+
- { _id: 2, x: 11 }
519+
- name: find
520+
object: collection0
521+
arguments:
522+
filter: {}
523+
expectResult:
524+
- { _id: 1, x: 11 }
525+
- { _id: 2, x: 11 }
526+
- { _id: 3, x: 33 }
527+
expectEvents:
528+
- client: client0
529+
events:
530+
- commandStartedEvent:
531+
command:
532+
find: collection0
533+
readConcern:
534+
level: snapshot
535+
atClusterTime:
536+
"$$exists": false
537+
databaseName: database0
538+
- commandStartedEvent:
539+
command:
540+
find: collection0
541+
readConcern:
542+
level: snapshot
543+
atClusterTime: { $$matchesEntity: savedSnapshotTime }
544+
databaseName: database0
545+
- commandStartedEvent:
546+
command:
547+
find: collection0
548+
readConcern:
549+
"$$exists": false
550+
databaseName: database0

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedEntityMap.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,9 @@ private IClientSessionHandle CreateSession(BsonDocument entity, Dictionary<strin
10561056
case "snapshot":
10571057
options.Snapshot = option.Value.ToBoolean();
10581058
break;
1059+
case "snapshotTime":
1060+
options.SnapshotTime = _results[option.Value.AsString].AsBsonTimestamp;
1061+
break;
10591062
case "causalConsistency":
10601063
options.CausalConsistency = option.Value.ToBoolean();
10611064
break;

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedGetSnapshotOperation.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,57 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using MongoDB.Bson;
20+
1621
namespace MongoDB.Driver.Tests.UnifiedTestOperations;
1722

18-
public class UnifiedGetSnapshotOperation
23+
public class UnifiedGetSnapshotOperation : IUnifiedEntityTestOperation
24+
{
25+
private readonly IClientSessionHandle _session;
26+
27+
public UnifiedGetSnapshotOperation(IClientSessionHandle session)
28+
{
29+
_session = session;
30+
}
31+
32+
public OperationResult Execute(CancellationToken cancellationToken) => GetSnapshotTime();
33+
34+
//TODO Do we necessarily need an async version of this...?
35+
public Task<OperationResult> ExecuteAsync(CancellationToken cancellationToken) => Task.FromResult(GetSnapshotTime());
36+
37+
private OperationResult GetSnapshotTime()
38+
{
39+
try
40+
{
41+
return OperationResult.FromResult(_session.GetSnapshotTime());
42+
}
43+
catch (Exception exception)
44+
{
45+
return OperationResult.FromException(exception);
46+
}
47+
}
48+
}
49+
50+
public class UnifiedGetSnapshotOperationBuilder
1951
{
20-
52+
private readonly UnifiedEntityMap _entityMap;
53+
54+
public UnifiedGetSnapshotOperationBuilder(UnifiedEntityMap entityMap)
55+
{
56+
_entityMap = entityMap;
57+
}
58+
59+
public UnifiedGetSnapshotOperation Build(string targetSessionId, BsonDocument arguments)
60+
{
61+
if (arguments != null)
62+
{
63+
throw new FormatException("GetSnapshotTime is not expected to contain arguments.");
64+
}
65+
66+
var session = _entityMap.Sessions[targetSessionId];
67+
return new UnifiedGetSnapshotOperation(session);
68+
}
2169
}

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedTestOperationFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public IUnifiedTestOperation CreateOperation(string operationName, string target
133133
"abortTransaction" => new UnifiedAbortTransactionOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
134134
"commitTransaction" => new UnifiedCommitTransactionOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
135135
"endSession" => new UnifiedEndSessionOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
136+
"getSnapshotTime" => new UnifiedGetSnapshotOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
136137
"startTransaction" => new UnifiedStartTransactionOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
137138
"withTransaction" => new UnifiedWithTransactionOperationBuilder(_entityMap).Build(targetEntityId, operationArguments),
138139
_ => throw new FormatException($"Invalid method name: '{operationName}'."),

0 commit comments

Comments
 (0)