@@ -84,6 +84,11 @@ type WriteConcernErrorData struct {
84
84
ErrInfo bson.Raw `bson:"errInfo,omitempty"`
85
85
}
86
86
87
+ type failPoint struct {
88
+ name string
89
+ client * mongo.Client
90
+ }
91
+
87
92
// T is a wrapper around testing.T.
88
93
type T struct {
89
94
// connsCheckedOut is the net number of connections checked out during test execution.
@@ -103,7 +108,7 @@ type T struct {
103
108
createdColls []* Collection // collections created in this test
104
109
proxyDialer * proxyDialer
105
110
dbName , collName string
106
- failPointNames [] string
111
+ failPoints [] failPoint
107
112
minServerVersion string
108
113
maxServerVersion string
109
114
validTopologies []TopologyKind
@@ -128,10 +133,9 @@ type T struct {
128
133
succeeded []* event.CommandSucceededEvent
129
134
failed []* event.CommandFailedEvent
130
135
131
- Client * mongo.Client
132
- fpClient * mongo.Client
133
- DB * mongo.Database
134
- Coll * mongo.Collection
136
+ Client * mongo.Client
137
+ DB * mongo.Database
138
+ Coll * mongo.Collection
135
139
}
136
140
137
141
func newT (wrapped * testing.T , opts ... * Options ) * T {
@@ -179,7 +183,7 @@ func New(wrapped *testing.T, opts ...*Options) *T {
179
183
// only create a client if it needs to be shared in sub-tests
180
184
// otherwise, a new client will be created for each subtest
181
185
if t .shareClient != nil && * t .shareClient {
182
- t .createTestClient (true )
186
+ t .createTestClient ()
183
187
}
184
188
185
189
wrapped .Cleanup (t .cleanup )
@@ -203,7 +207,6 @@ func (t *T) cleanup() {
203
207
// always disconnect the client regardless of clientType because Client.Disconnect will work against
204
208
// all deployments
205
209
_ = t .Client .Disconnect (context .Background ())
206
- _ = t .fpClient .Disconnect (context .Background ())
207
210
}
208
211
209
212
// Run creates a new T instance for a sub-test and runs the given callback. It also creates a new collection using the
@@ -229,12 +232,11 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
229
232
// for shareClient, inherit the client from the parent
230
233
if sub .shareClient != nil && * sub .shareClient && sub .clientType == t .clientType {
231
234
sub .Client = t .Client
232
- sub .fpClient = t .fpClient
233
235
}
234
236
// only create a client if not already set
235
237
if sub .Client == nil {
236
238
if sub .createClient == nil || * sub .createClient {
237
- sub .createTestClient (true )
239
+ sub .createTestClient ()
238
240
}
239
241
}
240
242
// create a collection for this test
@@ -260,7 +262,6 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
260
262
// only disconnect client if it's not being shared
261
263
if sub .shareClient == nil || ! * sub .shareClient {
262
264
_ = sub .Client .Disconnect (context .Background ())
263
- // _ = sub.fpClient.Disconnect(context.Background())
264
265
}
265
266
assert .Equal (sub , 0 , sessions , "%v sessions checked out" , sessions )
266
267
assert .Equal (sub , 0 , conns , "%v connections checked out" , conns )
@@ -410,7 +411,7 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
410
411
}
411
412
412
413
_ = t .Client .Disconnect (context .Background ())
413
- t .createTestClient (false )
414
+ t .createTestClient ()
414
415
t .DB = t .Client .Database (t .dbName )
415
416
t .Coll = t .DB .Collection (t .collName , t .collOpts )
416
417
@@ -563,45 +564,59 @@ func (t *T) SetFailPoint(fp FailPoint) {
563
564
}
564
565
}
565
566
566
- if err := SetFailPoint (fp , t .fpClient ); err != nil {
567
+ client , err := mongo .NewClient (t .clientOpts )
568
+ if err != nil {
569
+ t .Fatalf ("error creating client: %v" , err )
570
+ }
571
+ if err = client .Connect (context .Background ()); err != nil {
572
+ t .Fatalf ("error connecting client: %v" , err )
573
+ }
574
+ if err = SetFailPoint (fp , client ); err != nil {
567
575
t .Fatal (err )
568
576
}
569
- t .failPointNames = append (t .failPointNames , fp .ConfigureFailPoint )
577
+ t .failPoints = append (t .failPoints , failPoint { fp .ConfigureFailPoint , client } )
570
578
}
571
579
572
580
// SetFailPointFromDocument sets the fail point represented by the given document for the client associated with T. This
573
581
// method assumes that the given document is in the form {configureFailPoint: <failPointName>, ...}. Commands to create
574
582
// the failpoint will appear in command monitoring channels. The fail point will be automatically disabled after this
575
583
// test has run.
576
584
func (t * T ) SetFailPointFromDocument (fp bson.Raw ) {
577
- if err := SetRawFailPoint (fp , t .fpClient ); err != nil {
585
+ client , err := mongo .NewClient (t .clientOpts )
586
+ if err != nil {
587
+ t .Fatalf ("error creating client: %v" , err )
588
+ }
589
+ if err = client .Connect (context .Background ()); err != nil {
590
+ t .Fatalf ("error connecting client: %v" , err )
591
+ }
592
+ if err = SetRawFailPoint (fp , client ); err != nil {
578
593
t .Fatal (err )
579
594
}
580
595
581
596
name := fp .Index (0 ).Value ().StringValue ()
582
- t .failPointNames = append (t .failPointNames , name )
597
+ t .failPoints = append (t .failPoints , failPoint { name , client } )
583
598
}
584
599
585
600
// TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes.
586
601
// This function does not create a fail point on the server.
587
- func (t * T ) TrackFailPoint (fpName string ) {
588
- t .failPointNames = append (t .failPointNames , fpName )
602
+ func (t * T ) TrackFailPoint (fpName string , client * mongo. Client ) {
603
+ t .failPoints = append (t .failPoints , failPoint { fpName , client } )
589
604
}
590
605
591
606
// ClearFailPoints disables all previously set failpoints for this test.
592
607
func (t * T ) ClearFailPoints () {
593
- db := t .fpClient .Database ("admin" )
594
- for _ , fp := range t .failPointNames {
608
+ for _ , fp := range t .failPoints {
595
609
cmd := bson.D {
596
- {"configureFailPoint" , fp },
610
+ {"configureFailPoint" , fp . name },
597
611
{"mode" , "off" },
598
612
}
599
- err := db .RunCommand (context .Background (), cmd ).Err ()
613
+ err := fp . client . Database ( "admin" ) .RunCommand (context .Background (), cmd ).Err ()
600
614
if err != nil {
601
- t .Fatalf ("error clearing fail point %s: %v" , fp , err )
615
+ t .Fatalf ("error clearing fail point %s: %v" , fp . name , err )
602
616
}
617
+ _ = fp .client .Disconnect (context .Background ())
603
618
}
604
- t .failPointNames = t .failPointNames [:0 ]
619
+ t .failPoints = t .failPoints [:0 ]
605
620
}
606
621
607
622
// CloneDatabase modifies the default database for this test to match the given options.
@@ -629,7 +644,7 @@ func sanitizeCollectionName(db string, coll string) string {
629
644
return coll
630
645
}
631
646
632
- func (t * T ) createTestClient (needFpClient bool ) {
647
+ func (t * T ) createTestClient () {
633
648
clientOpts := t .clientOpts
634
649
if clientOpts == nil {
635
650
// default opts
@@ -725,16 +740,6 @@ func (t *T) createTestClient(needFpClient bool) {
725
740
if err := t .Client .Connect (context .Background ()); err != nil {
726
741
t .Fatalf ("error connecting client: %v" , err )
727
742
}
728
-
729
- if needFpClient {
730
- t .fpClient , err = mongo .NewClient (t .clientOpts )
731
- if err != nil {
732
- t .Fatalf ("error creating client: %v" , err )
733
- }
734
- if err := t .fpClient .Connect (context .Background ()); err != nil {
735
- t .Fatalf ("error connecting client: %v" , err )
736
- }
737
- }
738
743
}
739
744
740
745
func (t * T ) createTestCollection () {
0 commit comments