7
7
package integration
8
8
9
9
import (
10
+ "bytes"
10
11
"testing"
11
12
12
13
"go.mongodb.org/mongo-driver/bson"
13
14
"go.mongodb.org/mongo-driver/internal/testutil/assert"
15
+ "go.mongodb.org/mongo-driver/mongo"
14
16
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
15
17
"go.mongodb.org/mongo-driver/mongo/options"
16
18
"go.mongodb.org/mongo-driver/x/mongo/driver"
@@ -19,12 +21,12 @@ import (
19
21
func TestRetryableWritesProse (t * testing.T ) {
20
22
clientOpts := options .Client ().SetRetryWrites (true ).SetWriteConcern (mtest .MajorityWc ).
21
23
SetReadConcern (mtest .MajorityRc )
22
- mtOpts := mtest .NewOptions ().ClientOptions (clientOpts ).MinServerVersion ("3.6" ).Topologies (mtest .ReplicaSet , mtest .Sharded ).
23
- CreateClient (false )
24
+ mtOpts := mtest .NewOptions ().ClientOptions (clientOpts ).MinServerVersion ("3.6" ).CreateClient (false )
24
25
mt := mtest .New (t , mtOpts )
25
26
defer mt .Close ()
26
27
27
- mt .RunOpts ("txn number included" , noClientOpts , func (mt * mtest.T ) {
28
+ includeOpts := mtest .NewOptions ().Topologies (mtest .ReplicaSet , mtest .Sharded ).CreateClient (false )
29
+ mt .RunOpts ("txn number included" , includeOpts , func (mt * mtest.T ) {
28
30
updateDoc := bson.D {{"$inc" , bson.D {{"x" , 1 }}}}
29
31
insertOneDoc := bson.D {{"x" , 1 }}
30
32
insertManyOrderedArgs := bson.D {
@@ -75,7 +77,8 @@ func TestRetryableWritesProse(t *testing.T) {
75
77
})
76
78
}
77
79
})
78
- mt .Run ("wrap mmapv1 error" , func (mt * mtest.T ) {
80
+ errorOpts := mtest .NewOptions ().Topologies (mtest .ReplicaSet , mtest .Sharded )
81
+ mt .RunOpts ("wrap mmapv1 error" , errorOpts , func (mt * mtest.T ) {
79
82
res , err := mt .DB .RunCommand (mtest .Background , bson.D {{"serverStatus" , 1 }}).DecodeBytes ()
80
83
assert .Nil (mt , err , "serverStatus error: %v" , err )
81
84
storageEngine , ok := res .Lookup ("storageEngine" , "name" ).StringValueOK ()
@@ -87,4 +90,50 @@ func TestRetryableWritesProse(t *testing.T) {
87
90
assert .Equal (mt , driver .ErrUnsupportedStorageEngine , err ,
88
91
"expected error %v, got %v" , driver .ErrUnsupportedStorageEngine , err )
89
92
})
93
+
94
+ standaloneOpts := mtest .NewOptions ().Topologies (mtest .Single ).CreateClient (false )
95
+ mt .RunOpts ("transaction number not sent on writes" , standaloneOpts , func (mt * mtest.T ) {
96
+ mt .Run ("explicit session" , func (mt * mtest.T ) {
97
+ // Standalones do not support retryable writes and will error if a transaction number is sent
98
+
99
+ sess , err := mt .Client .StartSession ()
100
+ assert .Nil (mt , err , "StartSession error: %v" , err )
101
+ defer sess .EndSession (mtest .Background )
102
+
103
+ mt .ClearEvents ()
104
+
105
+ err = mongo .WithSession (mtest .Background , sess , func (ctx mongo.SessionContext ) error {
106
+ doc := bson.D {{"foo" , 1 }}
107
+ _ , err := mt .Coll .InsertOne (ctx , doc )
108
+ return err
109
+ })
110
+ assert .Nil (mt , err , "InsertOne error: %v" , err )
111
+
112
+ _ , wantID := sess .ID ().Lookup ("id" ).Binary ()
113
+ command := mt .GetStartedEvent ().Command
114
+ lsid , err := command .LookupErr ("lsid" )
115
+ assert .Nil (mt , err , "Error getting lsid: %v" , err )
116
+ _ , gotID := lsid .Document ().Lookup ("id" ).Binary ()
117
+ assert .True (mt , bytes .Equal (wantID , gotID ), "expected session ID %v, got %v" , wantID , gotID )
118
+ txnNumber , err := command .LookupErr ("txnNumber" )
119
+ assert .NotNil (mt , err , "expected no txnNumber, got %v" , txnNumber )
120
+ })
121
+ mt .Run ("implicit session" , func (mt * mtest.T ) {
122
+ // Standalones do not support retryable writes and will error if a transaction number is sent
123
+
124
+ mt .ClearEvents ()
125
+
126
+ doc := bson.D {{"foo" , 1 }}
127
+ _ , err := mt .Coll .InsertOne (mtest .Background , doc )
128
+ assert .Nil (mt , err , "InsertOne error: %v" , err )
129
+
130
+ command := mt .GetStartedEvent ().Command
131
+ lsid , err := command .LookupErr ("lsid" )
132
+ assert .Nil (mt , err , "Error getting lsid: %v" , err )
133
+ _ , gotID := lsid .Document ().Lookup ("id" ).Binary ()
134
+ assert .NotNil (mt , gotID , "expected session ID, got nil" )
135
+ txnNumber , err := command .LookupErr ("txnNumber" )
136
+ assert .NotNil (mt , err , "expected no txnNumber, got %v" , txnNumber )
137
+ })
138
+ })
90
139
}
0 commit comments