Skip to content

Commit c1ed704

Browse files
committed
updates
1 parent ec18123 commit c1ed704

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

mongo/integration/mtest/mongotest.go

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

87+
type failPoint struct {
88+
name string
89+
client *mongo.Client
90+
}
91+
8792
// T is a wrapper around testing.T.
8893
type T struct {
8994
// connsCheckedOut is the net number of connections checked out during test execution.
@@ -103,7 +108,7 @@ type T struct {
103108
createdColls []*Collection // collections created in this test
104109
proxyDialer *proxyDialer
105110
dbName, collName string
106-
failPointNames []string
111+
failPoints []failPoint
107112
minServerVersion string
108113
maxServerVersion string
109114
validTopologies []TopologyKind
@@ -128,10 +133,9 @@ type T struct {
128133
succeeded []*event.CommandSucceededEvent
129134
failed []*event.CommandFailedEvent
130135

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
135139
}
136140

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

185189
wrapped.Cleanup(t.cleanup)
@@ -203,7 +207,6 @@ func (t *T) cleanup() {
203207
// always disconnect the client regardless of clientType because Client.Disconnect will work against
204208
// all deployments
205209
_ = t.Client.Disconnect(context.Background())
206-
_ = t.fpClient.Disconnect(context.Background())
207210
}
208211

209212
// 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)) {
229232
// for shareClient, inherit the client from the parent
230233
if sub.shareClient != nil && *sub.shareClient && sub.clientType == t.clientType {
231234
sub.Client = t.Client
232-
sub.fpClient = t.fpClient
233235
}
234236
// only create a client if not already set
235237
if sub.Client == nil {
236238
if sub.createClient == nil || *sub.createClient {
237-
sub.createTestClient(true)
239+
sub.createTestClient()
238240
}
239241
}
240242
// create a collection for this test
@@ -260,7 +262,6 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
260262
// only disconnect client if it's not being shared
261263
if sub.shareClient == nil || !*sub.shareClient {
262264
_ = sub.Client.Disconnect(context.Background())
263-
// _ = sub.fpClient.Disconnect(context.Background())
264265
}
265266
assert.Equal(sub, 0, sessions, "%v sessions checked out", sessions)
266267
assert.Equal(sub, 0, conns, "%v connections checked out", conns)
@@ -410,7 +411,7 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
410411
}
411412

412413
_ = t.Client.Disconnect(context.Background())
413-
t.createTestClient(false)
414+
t.createTestClient()
414415
t.DB = t.Client.Database(t.dbName)
415416
t.Coll = t.DB.Collection(t.collName, t.collOpts)
416417

@@ -563,45 +564,59 @@ func (t *T) SetFailPoint(fp FailPoint) {
563564
}
564565
}
565566

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 {
567575
t.Fatal(err)
568576
}
569-
t.failPointNames = append(t.failPointNames, fp.ConfigureFailPoint)
577+
t.failPoints = append(t.failPoints, failPoint{fp.ConfigureFailPoint, client})
570578
}
571579

572580
// SetFailPointFromDocument sets the fail point represented by the given document for the client associated with T. This
573581
// method assumes that the given document is in the form {configureFailPoint: <failPointName>, ...}. Commands to create
574582
// the failpoint will appear in command monitoring channels. The fail point will be automatically disabled after this
575583
// test has run.
576584
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 {
578593
t.Fatal(err)
579594
}
580595

581596
name := fp.Index(0).Value().StringValue()
582-
t.failPointNames = append(t.failPointNames, name)
597+
t.failPoints = append(t.failPoints, failPoint{name, client})
583598
}
584599

585600
// TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes.
586601
// 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})
589604
}
590605

591606
// ClearFailPoints disables all previously set failpoints for this test.
592607
func (t *T) ClearFailPoints() {
593-
db := t.fpClient.Database("admin")
594-
for _, fp := range t.failPointNames {
608+
for _, fp := range t.failPoints {
595609
cmd := bson.D{
596-
{"configureFailPoint", fp},
610+
{"configureFailPoint", fp.name},
597611
{"mode", "off"},
598612
}
599-
err := db.RunCommand(context.Background(), cmd).Err()
613+
err := fp.client.Database("admin").RunCommand(context.Background(), cmd).Err()
600614
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)
602616
}
617+
_ = fp.client.Disconnect(context.Background())
603618
}
604-
t.failPointNames = t.failPointNames[:0]
619+
t.failPoints = t.failPoints[:0]
605620
}
606621

607622
// CloneDatabase modifies the default database for this test to match the given options.
@@ -629,7 +644,7 @@ func sanitizeCollectionName(db string, coll string) string {
629644
return coll
630645
}
631646

632-
func (t *T) createTestClient(needFpClient bool) {
647+
func (t *T) createTestClient() {
633648
clientOpts := t.clientOpts
634649
if clientOpts == nil {
635650
// default opts
@@ -725,16 +740,6 @@ func (t *T) createTestClient(needFpClient bool) {
725740
if err := t.Client.Connect(context.Background()); err != nil {
726741
t.Fatalf("error connecting client: %v", err)
727742
}
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-
}
738743
}
739744

740745
func (t *T) createTestCollection() {

mongo/integration/unified_spec_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,11 @@ func executeTestRunnerOperation(mt *mtest.T, testCase *testCase, op *operation,
476476
if err != nil {
477477
return fmt.Errorf("Connect error for targeted client: %w", err)
478478
}
479-
defer func() { _ = client.Disconnect(context.Background()) }()
480479

481480
if err = client.Database("admin").RunCommand(context.Background(), fp).Err(); err != nil {
482481
return fmt.Errorf("error setting targeted fail point: %w", err)
483482
}
484-
mt.TrackFailPoint(fp.ConfigureFailPoint)
483+
mt.TrackFailPoint(fp.ConfigureFailPoint, client)
485484
case "configureFailPoint":
486485
fp, err := op.Arguments.LookupErr("failPoint")
487486
if err != nil {

0 commit comments

Comments
 (0)