Skip to content

Commit 0241e4d

Browse files
committed
updates
1 parent 3bc01fb commit 0241e4d

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

mongo/integration/mtest/mongotest.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ type WriteConcernErrorData struct {
8484
ErrInfo bson.Raw `bson:"errInfo,omitempty"`
8585
}
8686

87-
type failPoint struct {
88-
name string
89-
clientOpts *options.ClientOptions
90-
}
91-
9287
// T is a wrapper around testing.T.
9388
type T struct {
9489
// connsCheckedOut is the net number of connections checked out during test execution.
@@ -108,7 +103,7 @@ type T struct {
108103
createdColls []*Collection // collections created in this test
109104
proxyDialer *proxyDialer
110105
dbName, collName string
111-
failPointNames []failPoint
106+
failPointNames []string
112107
minServerVersion string
113108
maxServerVersion string
114109
validTopologies []TopologyKind
@@ -133,9 +128,10 @@ type T struct {
133128
succeeded []*event.CommandSucceededEvent
134129
failed []*event.CommandFailedEvent
135130

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
139135
}
140136

141137
func newT(wrapped *testing.T, opts ...*Options) *T {
@@ -183,7 +179,7 @@ func New(wrapped *testing.T, opts ...*Options) *T {
183179
// only create a client if it needs to be shared in sub-tests
184180
// otherwise, a new client will be created for each subtest
185181
if t.shareClient != nil && *t.shareClient {
186-
t.createTestClient()
182+
t.createTestClient(true)
187183
}
188184

189185
wrapped.Cleanup(t.cleanup)
@@ -207,6 +203,7 @@ func (t *T) cleanup() {
207203
// always disconnect the client regardless of clientType because Client.Disconnect will work against
208204
// all deployments
209205
_ = t.Client.Disconnect(context.Background())
206+
_ = t.fpClient.Disconnect(context.Background())
210207
}
211208

212209
// 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)) {
236233
// only create a client if not already set
237234
if sub.Client == nil {
238235
if sub.createClient == nil || *sub.createClient {
239-
sub.createTestClient()
236+
sub.createTestClient(false)
240237
}
241238
}
242239
// create a collection for this test
243240
if sub.Client != nil {
244241
sub.createTestCollection()
245242
}
243+
sub.fpClient = t.fpClient
246244

247245
// defer dropping all collections if the test is using a client
248246
defer func() {
@@ -411,7 +409,7 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
411409
}
412410

413411
_ = t.Client.Disconnect(context.Background())
414-
t.createTestClient()
412+
t.createTestClient(false)
415413
t.DB = t.Client.Database(t.dbName)
416414
t.Coll = t.DB.Collection(t.collName, t.collOpts)
417415

@@ -564,54 +562,42 @@ func (t *T) SetFailPoint(fp FailPoint) {
564562
}
565563
}
566564

567-
if err := SetFailPoint(fp, t.Client); err != nil {
565+
if err := SetFailPoint(fp, t.fpClient); err != nil {
568566
t.Fatal(err)
569567
}
570-
clientOpts := *t.clientOpts
571-
t.failPointNames = append(t.failPointNames, failPoint{fp.ConfigureFailPoint, &clientOpts})
568+
t.failPointNames = append(t.failPointNames, fp.ConfigureFailPoint)
572569
}
573570

574571
// SetFailPointFromDocument sets the fail point represented by the given document for the client associated with T. This
575572
// method assumes that the given document is in the form {configureFailPoint: <failPointName>, ...}. Commands to create
576573
// the failpoint will appear in command monitoring channels. The fail point will be automatically disabled after this
577574
// test has run.
578575
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 {
580577
t.Fatal(err)
581578
}
582579

583580
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)
586582
}
587583

588584
// TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes.
589585
// This function does not create a fail point on the server.
590586
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)
593588
}
594589

595590
// ClearFailPoints disables all previously set failpoints for this test.
596591
func (t *T) ClearFailPoints() {
592+
db := t.fpClient.Database("admin")
597593
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-
}()
608594
cmd := bson.D{
609-
{"configureFailPoint", fp.name},
595+
{"configureFailPoint", fp},
610596
{"mode", "off"},
611597
}
612-
err = client.Database("admin").RunCommand(context.Background(), cmd).Err()
598+
err := db.RunCommand(context.Background(), cmd).Err()
613599
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)
615601
}
616602
}
617603
t.failPointNames = t.failPointNames[:0]
@@ -642,7 +628,7 @@ func sanitizeCollectionName(db string, coll string) string {
642628
return coll
643629
}
644630

645-
func (t *T) createTestClient() {
631+
func (t *T) createTestClient(createFpClient bool) {
646632
clientOpts := t.clientOpts
647633
if clientOpts == nil {
648634
// default opts
@@ -738,6 +724,16 @@ func (t *T) createTestClient() {
738724
if err := t.Client.Connect(context.Background()); err != nil {
739725
t.Fatalf("error connecting client: %v", err)
740726
}
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+
}
741737
}
742738

743739
func (t *T) createTestCollection() {

0 commit comments

Comments
 (0)