@@ -128,9 +128,10 @@ type T struct {
128
128
succeeded []* event.CommandSucceededEvent
129
129
failed []* event.CommandFailedEvent
130
130
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
134
135
}
135
136
136
137
func newT (wrapped * testing.T , opts ... * Options ) * T {
@@ -175,10 +176,12 @@ func New(wrapped *testing.T, opts ...*Options) *T {
175
176
176
177
t := newT (wrapped , opts ... )
177
178
179
+ t .fpClient = t .createTestClient ()
180
+
178
181
// only create a client if it needs to be shared in sub-tests
179
182
// otherwise, a new client will be created for each subtest
180
183
if t .shareClient != nil && * t .shareClient {
181
- t .createTestClient ()
184
+ t .Client = t . createTestClient ()
182
185
}
183
186
184
187
wrapped .Cleanup (t .cleanup )
@@ -228,10 +231,13 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
228
231
if sub .shareClient != nil && * sub .shareClient && sub .clientType == t .clientType {
229
232
sub .Client = t .Client
230
233
}
234
+ if sub .fpClient == nil {
235
+ sub .fpClient = sub .createTestClient ()
236
+ }
231
237
// only create a client if not already set
232
238
if sub .Client == nil {
233
239
if sub .createClient == nil || * sub .createClient {
234
- sub .createTestClient ()
240
+ sub .Client = sub . createTestClient ()
235
241
}
236
242
}
237
243
// create a collection for this test
@@ -406,7 +412,7 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
406
412
}
407
413
408
414
_ = t .Client .Disconnect (context .Background ())
409
- t .createTestClient ()
415
+ t .Client = t . createTestClient ()
410
416
t .DB = t .Client .Database (t .dbName )
411
417
t .Coll = t .DB .Collection (t .collName , t .collOpts )
412
418
@@ -625,7 +631,7 @@ func sanitizeCollectionName(db string, coll string) string {
625
631
return coll
626
632
}
627
633
628
- func (t * T ) createTestClient () {
634
+ func (t * T ) createTestClient () * mongo. Client {
629
635
clientOpts := t .clientOpts
630
636
if clientOpts == nil {
631
637
// default opts
@@ -684,19 +690,20 @@ func (t *T) createTestClient() {
684
690
})
685
691
}
686
692
693
+ var client * mongo.Client
687
694
var err error
688
695
switch t .clientType {
689
696
case Pinned :
690
697
// pin to first mongos
691
698
pinnedHostList := []string {testContext .connString .Hosts [0 ]}
692
699
uriOpts := options .Client ().ApplyURI (testContext .connString .Original ).SetHosts (pinnedHostList )
693
- t . Client , err = mongo .NewClient (uriOpts , clientOpts )
700
+ client , err = mongo .NewClient (uriOpts , clientOpts )
694
701
case Mock :
695
702
// clear pool monitor to avoid configuration error
696
703
clientOpts .PoolMonitor = nil
697
704
t .mockDeployment = newMockDeployment ()
698
705
clientOpts .Deployment = t .mockDeployment
699
- t . Client , err = mongo .NewClient (clientOpts )
706
+ client , err = mongo .NewClient (clientOpts )
700
707
case Proxy :
701
708
t .proxyDialer = newProxyDialer ()
702
709
clientOpts .SetDialer (t .proxyDialer )
@@ -714,14 +721,15 @@ func (t *T) createTestClient() {
714
721
}
715
722
716
723
// 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 )
718
725
}
719
726
if err != nil {
720
727
t .Fatalf ("error creating client: %v" , err )
721
728
}
722
- if err := t . Client .Connect (context .Background ()); err != nil {
729
+ if err := client .Connect (context .Background ()); err != nil {
723
730
t .Fatalf ("error connecting client: %v" , err )
724
731
}
732
+ return client
725
733
}
726
734
727
735
func (t * T ) createTestCollection () {
0 commit comments