1818using MongoDB . Bson . Serialization . Attributes ;
1919using MongoDB . Driver . Core . Clusters ;
2020using MongoDB . Driver . Core . Misc ;
21- using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
21+ using MongoDB . Driver . TestHelpers ;
2222using Xunit ;
2323
2424namespace MongoDB . Driver . Tests ;
2525
26- [ Trait ( "Category" , "Integration" ) ]
27- public class AtClusterTimeTests
26+ public class AtClusterTimeTests : IntegrationTest < AtClusterTimeTests . ClassFixture >
2827{
29- [ Fact ]
30- public void AtClusterTime_should_work ( )
28+ public AtClusterTimeTests ( ClassFixture fixture )
29+ : base ( fixture , server => server . Supports ( Feature . SnapshotReads ) . ClusterType ( ClusterType . ReplicaSet ) )
3130 {
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+ }
4032
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 ;
4338
4439 BsonTimestamp clusterTime1 ;
4540
46- var filterDefinition = Builders < TestObject > . Filter . Empty ;
47- var sortDefinition = Builders < TestObject > . Sort . Ascending ( o => o . Name ) ;
48-
4941 var sessionOptions1 = new ClientSessionOptions
5042 {
5143 Snapshot = true
5244 } ;
5345
5446 using ( var session1 = client . StartSession ( sessionOptions1 ) )
5547 {
56- var results = collection . Find ( session1 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
48+ var results = GetTestObjects ( collection , session1 ) ;
5749 AssertOneObj ( results ) ;
5850
5951 clusterTime1 = session1 . GetSnapshotTime ( ) ;
@@ -72,7 +64,7 @@ public void AtClusterTime_should_work()
7264 //Snapshot read session at clusterTime1 should not see obj2
7365 using ( var session2 = client . StartSession ( sessionOptions2 ) )
7466 {
75- var results = collection . Find ( session2 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
67+ var results = GetTestObjects ( collection , session2 ) ;
7668 AssertOneObj ( results ) ;
7769
7870 var clusterTime2 = session2 . GetSnapshotTime ( ) ;
@@ -87,28 +79,127 @@ public void AtClusterTime_should_work()
8779 //Snapshot read session without cluster time should see obj2
8880 using ( var session3 = client . StartSession ( sessionOptions3 ) )
8981 {
90- var results = collection . Find ( session3 , filterDefinition ) . Sort ( sortDefinition ) . ToList ( ) ;
82+ var results = GetTestObjects ( collection , session3 ) ;
9183 AssertTwoObjs ( results ) ;
9284
93- var clusterTime3 = session3 . WrappedCoreSession . SnapshotTime ;
85+ var clusterTime3 = session3 . GetSnapshotTime ( ) ;
9486 Assert . NotEqual ( clusterTime3 , clusterTime1 ) ;
9587 }
88+ }
9689
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 ) )
98104 {
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 ) ;
101110 }
102111
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
104117 {
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 ) ;
108130 }
109131 }
110132
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
112203 {
113204 [ BsonId ]
114205 public ObjectId Id { get ; set ; }
0 commit comments