@@ -5,9 +5,20 @@ import (
5
5
6
6
"github.com/mongodb/mongo-go-driver/bson"
7
7
"github.com/mongodb/mongo-go-driver/core/uuid"
8
+ "github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
8
9
"github.com/stretchr/testify/require"
9
10
)
10
11
12
+ func compareOperationTimes (t * testing.T , expected * bson.Timestamp , actual * bson.Timestamp ) {
13
+ if expected .T != actual .T {
14
+ t .Fatalf ("T value mismatch; expected %d got %d" , expected .T , actual .T )
15
+ }
16
+
17
+ if expected .I != actual .I {
18
+ t .Fatalf ("I value mismatch; expected %d got %d" , expected .I , actual .I )
19
+ }
20
+ }
21
+
11
22
func TestClientSession (t * testing.T ) {
12
23
var clusterTime1 = bson .NewDocument (bson .EC .SubDocument ("$clusterTime" ,
13
24
bson .NewDocument (bson .EC .Timestamp ("clusterTime" , 10 , 5 ))))
@@ -30,7 +41,7 @@ func TestClientSession(t *testing.T) {
30
41
31
42
t .Run ("TestAdvanceClusterTime" , func (t * testing.T ) {
32
43
id , _ := uuid .New ()
33
- sess , err := NewClientSession (& Pool {}, id , Explicit )
44
+ sess , err := NewClientSession (& Pool {}, id , Explicit , OptCausalConsistency ( true ) )
34
45
require .Nil (t , err , "Unexpected error" )
35
46
err = sess .AdvanceClusterTime (clusterTime2 )
36
47
require .Nil (t , err , "Unexpected error" )
@@ -52,10 +63,48 @@ func TestClientSession(t *testing.T) {
52
63
53
64
t .Run ("TestEndSession" , func (t * testing.T ) {
54
65
id , _ := uuid .New ()
55
- sess , err := NewClientSession (& Pool {}, id , Explicit )
66
+ sess , err := NewClientSession (& Pool {}, id , Explicit , OptCausalConsistency ( true ) )
56
67
require .Nil (t , err , "Unexpected error" )
57
68
sess .EndSession ()
58
69
err = sess .UpdateUseTime ()
59
70
require .NotNil (t , err , "Expected error, received nil" )
60
71
})
72
+
73
+ t .Run ("TestAdvanceOperationTime" , func (t * testing.T ) {
74
+ id , _ := uuid .New ()
75
+ sess , err := NewClientSession (& Pool {}, id , Explicit , OptCausalConsistency (true ))
76
+ require .Nil (t , err , "Unexpected error" )
77
+
78
+ optime1 := & bson.Timestamp {
79
+ T : 1 ,
80
+ I : 0 ,
81
+ }
82
+ err = sess .AdvanceOperationTime (optime1 )
83
+ testhelpers .RequireNil (t , err , "error updating first operation time: %s" , err )
84
+ compareOperationTimes (t , optime1 , sess .OperationTime )
85
+
86
+ optime2 := & bson.Timestamp {
87
+ T : 2 ,
88
+ I : 0 ,
89
+ }
90
+ err = sess .AdvanceOperationTime (optime2 )
91
+ testhelpers .RequireNil (t , err , "error updating second operation time: %s" , err )
92
+ compareOperationTimes (t , optime2 , sess .OperationTime )
93
+
94
+ optime3 := & bson.Timestamp {
95
+ T : 2 ,
96
+ I : 1 ,
97
+ }
98
+ err = sess .AdvanceOperationTime (optime3 )
99
+ testhelpers .RequireNil (t , err , "error updating third operation time: %s" , err )
100
+ compareOperationTimes (t , optime3 , sess .OperationTime )
101
+
102
+ err = sess .AdvanceOperationTime (& bson.Timestamp {
103
+ T : 1 ,
104
+ I : 10 ,
105
+ })
106
+ testhelpers .RequireNil (t , err , "error updating fourth operation time: %s" , err )
107
+ compareOperationTimes (t , optime3 , sess .OperationTime )
108
+ sess .EndSession ()
109
+ })
61
110
}
0 commit comments