@@ -49,6 +49,11 @@ const (
49
49
namespaceExistsErrCode int32 = 48
50
50
)
51
51
52
+ type failPoint struct {
53
+ name string
54
+ client * mongo.Client
55
+ }
56
+
52
57
// T is a wrapper around testing.T.
53
58
type T struct {
54
59
// connsCheckedOut is the net number of connections checked out during test execution.
@@ -68,7 +73,8 @@ type T struct {
68
73
createdColls []* Collection // collections created in this test
69
74
proxyDialer * proxyDialer
70
75
dbName , collName string
71
- failPointNames []string
76
+ hasFailPoint bool
77
+ failPoints []failPoint
72
78
minServerVersion string
73
79
maxServerVersion string
74
80
validTopologies []TopologyKind
@@ -166,7 +172,14 @@ func (t *T) cleanup() {
166
172
167
173
// always disconnect the client regardless of clientType because Client.Disconnect will work against
168
174
// all deployments
169
- _ = t .Client .Disconnect (context .Background ())
175
+ if ! t .hasFailPoint {
176
+ _ = t .Client .Disconnect (context .Background ())
177
+ }
178
+ for _ , fp := range t .failPoints {
179
+ fp .client .Disconnect (context .Background ())
180
+ }
181
+ t .hasFailPoint = false
182
+ t .failPoints = t .failPoints [:0 ]
170
183
}
171
184
172
185
// Run creates a new T instance for a sub-test and runs the given callback. It also creates a new collection using the
@@ -220,7 +233,7 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
220
233
sub .ClearCollections ()
221
234
}
222
235
// only disconnect client if it's not being shared
223
- if sub .shareClient == nil || ! * sub .shareClient {
236
+ if ( sub .shareClient == nil || ! * sub .shareClient ) && ! sub . hasFailPoint {
224
237
_ = sub .Client .Disconnect (context .Background ())
225
238
}
226
239
assert .Equal (sub , 0 , sessions , "%v sessions checked out" , sessions )
@@ -364,7 +377,10 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
364
377
t .clientOpts = opts
365
378
}
366
379
367
- _ = t .Client .Disconnect (context .Background ())
380
+ if ! t .hasFailPoint {
381
+ _ = t .Client .Disconnect (context .Background ())
382
+ }
383
+ t .hasFailPoint = false
368
384
t .createTestClient ()
369
385
t .DB = t .Client .Database (t .dbName )
370
386
t .Coll = t .DB .Collection (t .collName , t .collOpts )
@@ -523,7 +539,8 @@ func (t *T) SetFailPoint(fp failpoint.FailPoint) {
523
539
if err := SetFailPoint (fp , t .Client ); err != nil {
524
540
t .Fatal (err )
525
541
}
526
- t .failPointNames = append (t .failPointNames , fp .ConfigureFailPoint )
542
+ t .hasFailPoint = true
543
+ t .failPoints = append (t .failPoints , failPoint {name : fp .ConfigureFailPoint , client : t .Client })
527
544
}
528
545
529
546
// SetFailPointFromDocument sets the fail point represented by the given document for the client associated with T. This
@@ -536,29 +553,32 @@ func (t *T) SetFailPointFromDocument(fp bson.Raw) {
536
553
}
537
554
538
555
name := fp .Index (0 ).Value ().StringValue ()
539
- t .failPointNames = append (t .failPointNames , name )
556
+ t .hasFailPoint = true
557
+ t .failPoints = append (t .failPoints , failPoint {name : name , client : t .Client })
540
558
}
541
559
542
560
// TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes.
543
561
// This function does not create a fail point on the server.
544
562
func (t * T ) TrackFailPoint (fpName string ) {
545
- t .failPointNames = append (t .failPointNames , fpName )
563
+ t .hasFailPoint = true
564
+ t .failPoints = append (t .failPoints , failPoint {name : fpName , client : t .Client })
546
565
}
547
566
548
567
// ClearFailPoints disables all previously set failpoints for this test.
549
568
func (t * T ) ClearFailPoints () {
550
- db := t .Client .Database ("admin" )
551
- for _ , fp := range t .failPointNames {
569
+ for _ , fp := range t .failPoints {
552
570
cmd := failpoint.FailPoint {
553
- ConfigureFailPoint : fp ,
571
+ ConfigureFailPoint : fp . name ,
554
572
Mode : failpoint .ModeOff ,
555
573
}
556
- err := db .RunCommand (context .Background (), cmd ).Err ()
574
+ err := fp . client . Database ( "admin" ) .RunCommand (context .Background (), cmd ).Err ()
557
575
if err != nil {
558
- t .Fatalf ("error clearing fail point %s: %v" , fp , err )
576
+ t .Fatalf ("error clearing fail point %s: %v" , fp . name , err )
559
577
}
578
+ _ = fp .client .Disconnect (context .Background ())
560
579
}
561
- t .failPointNames = t .failPointNames [:0 ]
580
+ t .hasFailPoint = false
581
+ t .failPoints = t .failPoints [:0 ]
562
582
}
563
583
564
584
// CloneDatabase modifies the default database for this test to match the given options.
0 commit comments