@@ -11,7 +11,6 @@ import (
1111 "path"
1212 "reflect"
1313 "testing"
14- "time"
1514
1615 "go.mongodb.org/mongo-driver/bson"
1716 "go.mongodb.org/mongo-driver/bson/bsoncodec"
@@ -184,9 +183,6 @@ func runSpecTestFile(t *testing.T, specDir, fileName string) {
184183}
185184
186185func runSpecTestCase (mt * mtest.T , test * testCase , testFile testFile ) {
187- testClientOpts := createClientOptions (mt , test .ClientOptions )
188- testClientOpts .SetHeartbeatInterval (50 * time .Millisecond )
189-
190186 opts := mtest .NewOptions ().DatabaseName (testFile .DatabaseName ).CollectionName (testFile .CollectionName )
191187 if mt .TopologyKind () == mtest .Sharded && ! test .UseMultipleMongoses {
192188 // pin to a single mongos
@@ -200,12 +196,8 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
200196 {"validator" , validator },
201197 })
202198 }
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- }
208199
200+ // Start the test without setting client options so the setup will be done with a default client.
209201 mt .RunOpts (test .Description , opts , func (mt * mtest.T ) {
210202 if len (test .SkipReason ) > 0 {
211203 mt .Skip (test .SkipReason )
@@ -219,37 +211,39 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
219211
220212 // work around for SERVER-39704: run a non-transactional distinct against each shard in a sharded cluster
221213 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 )
231220 }
232221
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
234223 // been created and the collection drop in mongotest will hang for transactions to be aborted (60 seconds)
235224 // in error cases.
236225 defer killSessions (mt )
226+
227+ // Test setup: create collections that are tracked by mtest, insert test data, and set the failpoint.
237228 setupTest (mt , & testFile , test )
229+ if test .FailPoint != nil {
230+ mt .SetFailPoint (* test .FailPoint )
231+ }
238232
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 )
241236
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 )
243240 sess0 , sess1 := setupSessions (mt , test )
244241 if sess0 != nil {
245242 defer func () {
246243 sess0 .EndSession (mtest .Background )
247244 sess1 .EndSession (mtest .Background )
248245 }()
249246 }
250- if test .FailPoint != nil {
251- mt .SetFailPoint (* test .FailPoint )
252- }
253247
254248 // run operations
255249 mt .ClearEvents ()
@@ -762,24 +756,19 @@ func insertDocuments(mt *mtest.T, coll *mongo.Collection, rawDocs []bson.Raw) {
762756func setupTest (mt * mtest.T , testFile * testFile , testCase * testCase ) {
763757 mt .Helper ()
764758
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 ()
768759 // key vault data
769760 if len (testFile .KeyVaultData ) > 0 {
770761 keyVaultColl := mt .CreateCollection (mtest.Collection {
771- Name : "datakeys" ,
772- DB : "keyvault" ,
773- Client : setupClient ,
762+ Name : "datakeys" ,
763+ DB : "keyvault" ,
774764 }, false )
775765
776766 insertDocuments (mt , keyVaultColl , testFile .KeyVaultData )
777767 }
778768
779769 // regular documents
780770 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 )
783772 return
784773 }
785774
@@ -788,15 +777,13 @@ func setupTest(mt *mtest.T, testFile *testFile, testCase *testCase) {
788777
789778 if gfsData .Chunks != nil {
790779 chunks := mt .CreateCollection (mtest.Collection {
791- Name : gridFSChunks ,
792- Client : setupClient ,
780+ Name : gridFSChunks ,
793781 }, false )
794782 insertDocuments (mt , chunks , gfsData .Chunks )
795783 }
796784 if gfsData .Files != nil {
797785 files := mt .CreateCollection (mtest.Collection {
798- Name : gridFSFiles ,
799- Client : setupClient ,
786+ Name : gridFSFiles ,
800787 }, false )
801788 insertDocuments (mt , files , gfsData .Files )
802789
0 commit comments