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