@@ -11,7 +11,6 @@ import (
11
11
"path"
12
12
"reflect"
13
13
"testing"
14
- "time"
15
14
16
15
"go.mongodb.org/mongo-driver/bson"
17
16
"go.mongodb.org/mongo-driver/bson/bsoncodec"
@@ -184,9 +183,6 @@ func runSpecTestFile(t *testing.T, specDir, fileName string) {
184
183
}
185
184
186
185
func runSpecTestCase (mt * mtest.T , test * testCase , testFile testFile ) {
187
- testClientOpts := createClientOptions (mt , test .ClientOptions )
188
- testClientOpts .SetHeartbeatInterval (50 * time .Millisecond )
189
-
190
186
opts := mtest .NewOptions ().DatabaseName (testFile .DatabaseName ).CollectionName (testFile .CollectionName )
191
187
if mt .TopologyKind () == mtest .Sharded && ! test .UseMultipleMongoses {
192
188
// pin to a single mongos
@@ -200,12 +196,8 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
200
196
{"validator" , validator },
201
197
})
202
198
}
203
- if test .Description != cseMaxVersionTest {
204
- // don't specify client options for the maxWireVersion CSE test because the client cannot
205
- // be created successfully. Should be fixed by SPEC-1403.
206
- opts .ClientOptions (testClientOpts )
207
- }
208
199
200
+ // Start the test without setting client options so the setup will be done with a default client.
209
201
mt .RunOpts (test .Description , opts , func (mt * mtest.T ) {
210
202
if len (test .SkipReason ) > 0 {
211
203
mt .Skip (test .SkipReason )
@@ -219,37 +211,39 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
219
211
220
212
// work around for SERVER-39704: run a non-transactional distinct against each shard in a sharded cluster
221
213
if mt .TopologyKind () == mtest .Sharded && test .Description == "distinct" {
222
- opts := options .Client ().ApplyURI (mt .ConnString ())
223
- for _ , host := range opts .Hosts {
224
- shardClient , err := mongo .Connect (mtest .Background , opts .SetHosts ([]string {host }))
225
- assert .Nil (mt , err , "Connect error for shard %v: %v" , host , err )
226
- coll := shardClient .Database (mt .DB .Name ()).Collection (mt .Coll .Name ())
227
- _ , err = coll .Distinct (mtest .Background , "x" , bson.D {})
228
- assert .Nil (mt , err , "Distinct error for shard %v: %v" , host , err )
229
- _ = shardClient .Disconnect (mtest .Background )
230
- }
214
+ err := runCommandOnAllServers (mt , func (mongosClient * mongo.Client ) error {
215
+ coll := mongosClient .Database (mt .DB .Name ()).Collection (mt .Coll .Name ())
216
+ _ , err := coll .Distinct (mtest .Background , "x" , bson.D {})
217
+ return err
218
+ })
219
+ assert .Nil (mt , err , "error running distinct against all mongoses: %v" , err )
231
220
}
232
221
233
- // defer killSessions to ensure it runs regardless of the state of the test because the client has already
222
+ // Defer killSessions to ensure it runs regardless of the state of the test because the client has already
234
223
// been created and the collection drop in mongotest will hang for transactions to be aborted (60 seconds)
235
224
// in error cases.
236
225
defer killSessions (mt )
226
+
227
+ // Test setup: create collections that are tracked by mtest, insert test data, and set the failpoint.
237
228
setupTest (mt , & testFile , test )
229
+ if test .FailPoint != nil {
230
+ mt .SetFailPoint (* test .FailPoint )
231
+ }
238
232
239
- // create the GridFS bucket after resetting the client so it will be created with a connected client
240
- createBucket (mt , testFile , test )
233
+ // Reset the client using the client options specified in the test.
234
+ testClientOpts := createClientOptions (mt , test .ClientOptions )
235
+ mt .ResetClient (testClientOpts )
241
236
242
- // create sessions, fail points, and collection
237
+ // Create the GridFS bucket and sessions after resetting the client so it will be created with a connected
238
+ // client.
239
+ createBucket (mt , testFile , test )
243
240
sess0 , sess1 := setupSessions (mt , test )
244
241
if sess0 != nil {
245
242
defer func () {
246
243
sess0 .EndSession (mtest .Background )
247
244
sess1 .EndSession (mtest .Background )
248
245
}()
249
246
}
250
- if test .FailPoint != nil {
251
- mt .SetFailPoint (* test .FailPoint )
252
- }
253
247
254
248
// run operations
255
249
mt .ClearEvents ()
@@ -762,24 +756,19 @@ func insertDocuments(mt *mtest.T, coll *mongo.Collection, rawDocs []bson.Raw) {
762
756
func setupTest (mt * mtest.T , testFile * testFile , testCase * testCase ) {
763
757
mt .Helper ()
764
758
765
- // all setup should be done with the global client instead of the test client to prevent any errors created by
766
- // client configurations.
767
- setupClient := mt .GlobalClient ()
768
759
// key vault data
769
760
if len (testFile .KeyVaultData ) > 0 {
770
761
keyVaultColl := mt .CreateCollection (mtest.Collection {
771
- Name : "datakeys" ,
772
- DB : "keyvault" ,
773
- Client : setupClient ,
762
+ Name : "datakeys" ,
763
+ DB : "keyvault" ,
774
764
}, false )
775
765
776
766
insertDocuments (mt , keyVaultColl , testFile .KeyVaultData )
777
767
}
778
768
779
769
// regular documents
780
770
if testFile .Data .Documents != nil {
781
- insertColl := setupClient .Database (mt .DB .Name ()).Collection (mt .Coll .Name ())
782
- insertDocuments (mt , insertColl , testFile .Data .Documents )
771
+ insertDocuments (mt , mt .Coll , testFile .Data .Documents )
783
772
return
784
773
}
785
774
@@ -788,15 +777,13 @@ func setupTest(mt *mtest.T, testFile *testFile, testCase *testCase) {
788
777
789
778
if gfsData .Chunks != nil {
790
779
chunks := mt .CreateCollection (mtest.Collection {
791
- Name : gridFSChunks ,
792
- Client : setupClient ,
780
+ Name : gridFSChunks ,
793
781
}, false )
794
782
insertDocuments (mt , chunks , gfsData .Chunks )
795
783
}
796
784
if gfsData .Files != nil {
797
785
files := mt .CreateCollection (mtest.Collection {
798
- Name : gridFSFiles ,
799
- Client : setupClient ,
786
+ Name : gridFSFiles ,
800
787
}, false )
801
788
insertDocuments (mt , files , gfsData .Files )
802
789
0 commit comments