1
+ /* Copyright 2010-present MongoDB Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System . Collections . Generic ;
17
+ using MongoDB . Bson ;
18
+ using MongoDB . Bson . Serialization . Attributes ;
19
+ using Xunit ;
20
+
21
+ namespace MongoDB . Driver . Tests ;
22
+
23
+ public class AtClusterTimeTests
24
+ {
25
+ [ Fact ]
26
+ public void AtClusterTime_should_work ( )
27
+ {
28
+ const string collectionName = "atClusterTimeTests" ;
29
+ const string databaseName = "testDb" ;
30
+
31
+ using var client = DriverTestConfiguration . Client ;
32
+ var database = client . GetDatabase ( databaseName ) ;
33
+ database . DropCollection ( collectionName ) ;
34
+ var collection = database . GetCollection < TestObject > ( collectionName ) ;
35
+
36
+ var obj1 = new TestObject { Name = "obj1" } ;
37
+ collection . InsertOne ( obj1 ) ;
38
+
39
+ BsonTimestamp clusterTime1 ;
40
+
41
+ var filterDefinition = Builders < TestObject > . Filter . Empty ;
42
+ var sortDefinition = Builders < TestObject > . Sort . Ascending ( o => o . Name ) ;
43
+
44
+ var sessionOptions1 = new ClientSessionOptions
45
+ {
46
+ Snapshot = true
47
+ } ;
48
+
49
+ using ( var session1 = client . StartSession ( sessionOptions1 ) )
50
+ {
51
+ var results = collection . Find ( session1 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
52
+ AssertOneObj ( results ) ;
53
+
54
+ clusterTime1 = session1 . SnapshotTime ;
55
+ Assert . NotEqual ( null , clusterTime1 ) ;
56
+ }
57
+
58
+ var obj2 = new TestObject { Name = "obj2" } ;
59
+ collection . InsertOne ( obj2 ) ;
60
+
61
+ var sessionOptions2 = new ClientSessionOptions
62
+ {
63
+ Snapshot = true ,
64
+ SnapshotTime = clusterTime1
65
+ } ;
66
+
67
+ //Snapshot read session at clusterTime1 should not see obj2
68
+ using ( var session2 = client . StartSession ( sessionOptions2 ) )
69
+ {
70
+ var results = collection . Find ( session2 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
71
+ AssertOneObj ( results ) ;
72
+
73
+ var clusterTime2 = session2 . SnapshotTime ;
74
+ Assert . Equal ( clusterTime2 , clusterTime1 ) ;
75
+ }
76
+
77
+ var sessionOptions3 = new ClientSessionOptions
78
+ {
79
+ Snapshot = true ,
80
+ } ;
81
+
82
+ //Snapshot read session without cluster time should see obj2
83
+ using ( var session3 = client . StartSession ( sessionOptions3 ) )
84
+ {
85
+ var results = collection . Find ( session3 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
86
+ AssertTwoObjs ( results ) ;
87
+
88
+ var clusterTime3 = session3 . WrappedCoreSession . SnapshotTime ;
89
+ Assert . NotEqual ( clusterTime3 , clusterTime1 ) ;
90
+ }
91
+
92
+ void AssertOneObj ( List < TestObject > objs )
93
+ {
94
+ Assert . Equal ( 1 , objs . Count ) ;
95
+ Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
96
+ }
97
+
98
+ void AssertTwoObjs ( List < TestObject > objs )
99
+ {
100
+ Assert . Equal ( 2 , objs . Count ) ;
101
+ Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
102
+ Assert . Equal ( "obj2" , objs [ 1 ] . Name ) ;
103
+ }
104
+ }
105
+
106
+ private class TestObject
107
+ {
108
+ [ BsonId ]
109
+ public ObjectId Id { get ; set ; }
110
+ public string Name { get ; set ; }
111
+ }
112
+ }
0 commit comments