18
18
using MongoDB . Bson . Serialization . Attributes ;
19
19
using MongoDB . Driver . Core . Clusters ;
20
20
using MongoDB . Driver . Core . Misc ;
21
- using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
21
+ using MongoDB . Driver . TestHelpers ;
22
22
using Xunit ;
23
23
24
24
namespace MongoDB . Driver . Tests ;
25
25
26
- [ Trait ( "Category" , "Integration" ) ]
27
- public class AtClusterTimeTests
26
+ public class AtClusterTimeTests : IntegrationTest < AtClusterTimeTests . ClassFixture >
28
27
{
29
- [ Fact ]
30
- public void AtClusterTime_should_work ( )
28
+ public AtClusterTimeTests ( ClassFixture fixture )
29
+ : base ( fixture , server => server . Supports ( Feature . SnapshotReads ) . ClusterType ( ClusterType . ReplicaSet ) )
31
30
{
32
- RequireServer . Check ( ) . ClusterType ( ClusterType . ReplicaSet ) . Supports ( Feature . SnapshotReads ) ;
33
- const string collectionName = "atClusterTimeTests" ;
34
- const string databaseName = "testDb" ;
35
-
36
- using var client = DriverTestConfiguration . Client ;
37
- var database = client . GetDatabase ( databaseName ) ;
38
- database . DropCollection ( collectionName ) ;
39
- var collection = database . GetCollection < TestObject > ( collectionName ) ;
31
+ }
40
32
41
- var obj1 = new TestObject { Name = "obj1" } ;
42
- collection . InsertOne ( obj1 ) ;
33
+ [ Fact ]
34
+ public void MainTest ( )
35
+ {
36
+ var client = Fixture . Client ;
37
+ var collection = Fixture . Collection ;
43
38
44
39
BsonTimestamp clusterTime1 ;
45
40
46
- var filterDefinition = Builders < TestObject > . Filter . Empty ;
47
- var sortDefinition = Builders < TestObject > . Sort . Ascending ( o => o . Name ) ;
48
-
49
41
var sessionOptions1 = new ClientSessionOptions
50
42
{
51
43
Snapshot = true
52
44
} ;
53
45
54
46
using ( var session1 = client . StartSession ( sessionOptions1 ) )
55
47
{
56
- var results = collection . Find ( session1 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
48
+ var results = GetTestObjects ( collection , session1 ) ;
57
49
AssertOneObj ( results ) ;
58
50
59
51
clusterTime1 = session1 . GetSnapshotTime ( ) ;
@@ -72,7 +64,7 @@ public void AtClusterTime_should_work()
72
64
//Snapshot read session at clusterTime1 should not see obj2
73
65
using ( var session2 = client . StartSession ( sessionOptions2 ) )
74
66
{
75
- var results = collection . Find ( session2 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
67
+ var results = GetTestObjects ( collection , session2 ) ;
76
68
AssertOneObj ( results ) ;
77
69
78
70
var clusterTime2 = session2 . GetSnapshotTime ( ) ;
@@ -87,28 +79,127 @@ public void AtClusterTime_should_work()
87
79
//Snapshot read session without cluster time should see obj2
88
80
using ( var session3 = client . StartSession ( sessionOptions3 ) )
89
81
{
90
- var results = collection . Find ( session3 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
82
+ var results = GetTestObjects ( collection , session3 ) ;
91
83
AssertTwoObjs ( results ) ;
92
84
93
- var clusterTime3 = session3 . WrappedCoreSession . SnapshotTime ;
85
+ var clusterTime3 = session3 . GetSnapshotTime ( ) ;
94
86
Assert . NotEqual ( clusterTime3 , clusterTime1 ) ;
95
87
}
88
+ }
96
89
97
- void AssertOneObj ( List < TestObject > objs )
90
+ [ Fact ]
91
+ public void IncreasedTimestamp ( )
92
+ {
93
+ var client = Fixture . Client ;
94
+ var collection = Fixture . Collection ;
95
+
96
+ BsonTimestamp clusterTime1 ;
97
+
98
+ var sessionOptions1 = new ClientSessionOptions
99
+ {
100
+ Snapshot = true
101
+ } ;
102
+
103
+ using ( var session1 = client . StartSession ( sessionOptions1 ) )
98
104
{
99
- Assert . Equal ( 1 , objs . Count ) ;
100
- Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
105
+ var results = GetTestObjects ( collection , session1 ) ;
106
+ AssertOneObj ( results ) ;
107
+
108
+ clusterTime1 = session1 . GetSnapshotTime ( ) ;
109
+ Assert . NotEqual ( null , clusterTime1 ) ;
101
110
}
102
111
103
- void AssertTwoObjs ( List < TestObject > objs )
112
+ var obj2 = new TestObject { Name = "obj2" } ;
113
+ collection . InsertOne ( obj2 ) ;
114
+
115
+ var modifiedClusterTime = new BsonTimestamp ( clusterTime1 . Value + 1 ) ;
116
+ var sessionOptions2 = new ClientSessionOptions
104
117
{
105
- Assert . Equal ( 2 , objs . Count ) ;
106
- Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
107
- Assert . Equal ( "obj2" , objs [ 1 ] . Name ) ;
118
+ Snapshot = true ,
119
+ SnapshotTime = modifiedClusterTime
120
+ } ;
121
+
122
+ //Snapshot read session at clusterTime1+1 should see obj2
123
+ using ( var session2 = client . StartSession ( sessionOptions2 ) )
124
+ {
125
+ var results = GetTestObjects ( collection , session2 ) ;
126
+ AssertTwoObjs ( results ) ;
127
+
128
+ var clusterTime2 = session2 . GetSnapshotTime ( ) ;
129
+ Assert . Equal ( modifiedClusterTime , clusterTime2 ) ;
108
130
}
109
131
}
110
132
111
- private class TestObject
133
+ [ Fact ]
134
+ public void DecreasedTimestamp ( )
135
+ {
136
+ var client = Fixture . Client ;
137
+ var collection = Fixture . Collection ;
138
+
139
+ BsonTimestamp clusterTime1 ;
140
+
141
+ var sessionOptions1 = new ClientSessionOptions
142
+ {
143
+ Snapshot = true
144
+ } ;
145
+
146
+ using ( var session1 = client . StartSession ( sessionOptions1 ) )
147
+ {
148
+ var results = GetTestObjects ( collection , session1 ) ;
149
+ AssertOneObj ( results ) ;
150
+
151
+ clusterTime1 = session1 . GetSnapshotTime ( ) ;
152
+ Assert . NotEqual ( null , clusterTime1 ) ;
153
+ }
154
+
155
+ var obj2 = new TestObject { Name = "obj2" } ;
156
+ collection . InsertOne ( obj2 ) ;
157
+
158
+ var modifiedClusterTime = new BsonTimestamp ( clusterTime1 . Value - 1 ) ;
159
+ var sessionOptions2 = new ClientSessionOptions
160
+ {
161
+ Snapshot = true ,
162
+ SnapshotTime = modifiedClusterTime
163
+ } ;
164
+
165
+ //Snapshot read session at clusterTime1-1 should not see obj2
166
+ using ( var session2 = client . StartSession ( sessionOptions2 ) )
167
+ {
168
+ var results = GetTestObjects ( collection , session2 ) ;
169
+ Assert . Equal ( 0 , results . Count ) ;
170
+
171
+ var clusterTime2 = session2 . GetSnapshotTime ( ) ;
172
+ Assert . Equal ( modifiedClusterTime , clusterTime2 ) ;
173
+ }
174
+ }
175
+
176
+ List < TestObject > GetTestObjects ( IMongoCollection < TestObject > collection , IClientSessionHandle session )
177
+ {
178
+ var filterDefinition = Builders < TestObject > . Filter . Empty ;
179
+ var sortDefinition = Builders < TestObject > . Sort . Ascending ( o => o . Name ) ;
180
+ return collection . Find ( session , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
181
+ }
182
+
183
+ void AssertOneObj ( List < TestObject > objs )
184
+ {
185
+ Assert . Equal ( 1 , objs . Count ) ;
186
+ Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
187
+ }
188
+
189
+ void AssertTwoObjs ( List < TestObject > objs )
190
+ {
191
+ Assert . Equal ( 2 , objs . Count ) ;
192
+ Assert . Equal ( "obj1" , objs [ 0 ] . Name ) ;
193
+ Assert . Equal ( "obj2" , objs [ 1 ] . Name ) ;
194
+ }
195
+
196
+ public class ClassFixture : MongoCollectionFixture < TestObject >
197
+ {
198
+ public override bool InitializeDataBeforeEachTestCase => true ;
199
+ protected override IEnumerable < TestObject > InitialData => [ new ( ) { Name = "obj1" } ] ;
200
+ }
201
+
202
+ public class TestObject
112
203
{
113
204
[ BsonId ]
114
205
public ObjectId Id { get ; set ; }
0 commit comments