@@ -128,9 +128,10 @@ type T struct {
128128 succeeded []* event.CommandSucceededEvent
129129 failed []* event.CommandFailedEvent
130130
131- Client * mongo.Client
132- DB * mongo.Database
133- Coll * mongo.Collection
131+ Client * mongo.Client
132+ fpClient * mongo.Client
133+ DB * mongo.Database
134+ Coll * mongo.Collection
134135}
135136
136137func newT (wrapped * testing.T , opts ... * Options ) * T {
@@ -175,10 +176,12 @@ func New(wrapped *testing.T, opts ...*Options) *T {
175176
176177 t := newT (wrapped , opts ... )
177178
179+ t .fpClient = t .createTestClient ()
180+
178181 // only create a client if it needs to be shared in sub-tests
179182 // otherwise, a new client will be created for each subtest
180183 if t .shareClient != nil && * t .shareClient {
181- t .createTestClient ()
184+ t .Client = t . createTestClient ()
182185 }
183186
184187 wrapped .Cleanup (t .cleanup )
@@ -228,10 +231,13 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
228231 if sub .shareClient != nil && * sub .shareClient && sub .clientType == t .clientType {
229232 sub .Client = t .Client
230233 }
234+ if sub .fpClient == nil {
235+ sub .fpClient = sub .createTestClient ()
236+ }
231237 // only create a client if not already set
232238 if sub .Client == nil {
233239 if sub .createClient == nil || * sub .createClient {
234- sub .createTestClient ()
240+ sub .Client = sub . createTestClient ()
235241 }
236242 }
237243 // create a collection for this test
@@ -406,7 +412,7 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
406412 }
407413
408414 _ = t .Client .Disconnect (context .Background ())
409- t .createTestClient ()
415+ t .Client = t . createTestClient ()
410416 t .DB = t .Client .Database (t .dbName )
411417 t .Coll = t .DB .Collection (t .collName , t .collOpts )
412418
@@ -625,7 +631,7 @@ func sanitizeCollectionName(db string, coll string) string {
625631 return coll
626632}
627633
628- func (t * T ) createTestClient () {
634+ func (t * T ) createTestClient () * mongo. Client {
629635 clientOpts := t .clientOpts
630636 if clientOpts == nil {
631637 // default opts
@@ -684,19 +690,20 @@ func (t *T) createTestClient() {
684690 })
685691 }
686692
693+ var client * mongo.Client
687694 var err error
688695 switch t .clientType {
689696 case Pinned :
690697 // pin to first mongos
691698 pinnedHostList := []string {testContext .connString .Hosts [0 ]}
692699 uriOpts := options .Client ().ApplyURI (testContext .connString .Original ).SetHosts (pinnedHostList )
693- t . Client , err = mongo .NewClient (uriOpts , clientOpts )
700+ client , err = mongo .NewClient (uriOpts , clientOpts )
694701 case Mock :
695702 // clear pool monitor to avoid configuration error
696703 clientOpts .PoolMonitor = nil
697704 t .mockDeployment = newMockDeployment ()
698705 clientOpts .Deployment = t .mockDeployment
699- t . Client , err = mongo .NewClient (clientOpts )
706+ client , err = mongo .NewClient (clientOpts )
700707 case Proxy :
701708 t .proxyDialer = newProxyDialer ()
702709 clientOpts .SetDialer (t .proxyDialer )
@@ -714,14 +721,15 @@ func (t *T) createTestClient() {
714721 }
715722
716723 // Pass in uriOpts first so clientOpts wins if there are any conflicting settings.
717- t . Client , err = mongo .NewClient (uriOpts , clientOpts )
724+ client , err = mongo .NewClient (uriOpts , clientOpts )
718725 }
719726 if err != nil {
720727 t .Fatalf ("error creating client: %v" , err )
721728 }
722- if err := t . Client .Connect (context .Background ()); err != nil {
729+ if err := client .Connect (context .Background ()); err != nil {
723730 t .Fatalf ("error connecting client: %v" , err )
724731 }
732+ return client
725733}
726734
727735func (t * T ) createTestCollection () {
0 commit comments