Skip to content

Commit ba5b63f

Browse files
committed
cache clients for failpoints.
1 parent e230a32 commit ba5b63f

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

internal/integration/mtest/mongotest.go

+33-13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ const (
4949
namespaceExistsErrCode int32 = 48
5050
)
5151

52+
type failPoint struct {
53+
name string
54+
client *mongo.Client
55+
}
56+
5257
// T is a wrapper around testing.T.
5358
type T struct {
5459
// connsCheckedOut is the net number of connections checked out during test execution.
@@ -68,7 +73,8 @@ type T struct {
6873
createdColls []*Collection // collections created in this test
6974
proxyDialer *proxyDialer
7075
dbName, collName string
71-
failPointNames []string
76+
hasFailPoint bool
77+
failPoints []failPoint
7278
minServerVersion string
7379
maxServerVersion string
7480
validTopologies []TopologyKind
@@ -166,7 +172,14 @@ func (t *T) cleanup() {
166172

167173
// always disconnect the client regardless of clientType because Client.Disconnect will work against
168174
// 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]
170183
}
171184

172185
// 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)) {
220233
sub.ClearCollections()
221234
}
222235
// 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 {
224237
_ = sub.Client.Disconnect(context.Background())
225238
}
226239
assert.Equal(sub, 0, sessions, "%v sessions checked out", sessions)
@@ -364,7 +377,10 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
364377
t.clientOpts = opts
365378
}
366379

367-
_ = t.Client.Disconnect(context.Background())
380+
if !t.hasFailPoint {
381+
_ = t.Client.Disconnect(context.Background())
382+
}
383+
t.hasFailPoint = false
368384
t.createTestClient()
369385
t.DB = t.Client.Database(t.dbName)
370386
t.Coll = t.DB.Collection(t.collName, t.collOpts)
@@ -523,7 +539,8 @@ func (t *T) SetFailPoint(fp failpoint.FailPoint) {
523539
if err := SetFailPoint(fp, t.Client); err != nil {
524540
t.Fatal(err)
525541
}
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})
527544
}
528545

529546
// 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) {
536553
}
537554

538555
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})
540558
}
541559

542560
// TrackFailPoint adds the given fail point to the list of fail points to be disabled when the current test finishes.
543561
// This function does not create a fail point on the server.
544562
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})
546565
}
547566

548567
// ClearFailPoints disables all previously set failpoints for this test.
549568
func (t *T) ClearFailPoints() {
550-
db := t.Client.Database("admin")
551-
for _, fp := range t.failPointNames {
569+
for _, fp := range t.failPoints {
552570
cmd := failpoint.FailPoint{
553-
ConfigureFailPoint: fp,
571+
ConfigureFailPoint: fp.name,
554572
Mode: failpoint.ModeOff,
555573
}
556-
err := db.RunCommand(context.Background(), cmd).Err()
574+
err := fp.client.Database("admin").RunCommand(context.Background(), cmd).Err()
557575
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)
559577
}
578+
_ = fp.client.Disconnect(context.Background())
560579
}
561-
t.failPointNames = t.failPointNames[:0]
580+
t.hasFailPoint = false
581+
t.failPoints = t.failPoints[:0]
562582
}
563583

564584
// CloneDatabase modifies the default database for this test to match the given options.

0 commit comments

Comments
 (0)