Skip to content

Commit be655d5

Browse files
committed
updates
1 parent 08cfe49 commit be655d5

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

mongo/integration/mtest/mongotest.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ type T struct {
128128
succeeded []*event.CommandSucceededEvent
129129
failed []*event.CommandFailedEvent
130130

131-
Client *mongo.Client
132-
fpClient *mongo.Client
133-
DB *mongo.Database
134-
Coll *mongo.Collection
131+
Client *mongo.Client
132+
// fpClient *mongo.Client
133+
DB *mongo.Database
134+
Coll *mongo.Collection
135135
}
136136

137137
func newT(wrapped *testing.T, opts ...*Options) *T {
@@ -176,17 +176,10 @@ func New(wrapped *testing.T, opts ...*Options) *T {
176176

177177
t := newT(wrapped, opts...)
178178

179-
// fpOpt := t.clientOpts
180-
// if fpOpt != nil {
181-
// fpOpt.AutoEncryptionOptions = nil
182-
// }
183-
// t.fpClient = t.createTestClient(fpOpt)
184-
185179
// only create a client if it needs to be shared in sub-tests
186180
// otherwise, a new client will be created for each subtest
187181
if t.shareClient != nil && *t.shareClient {
188-
t.fpClient = t.createTestClient(t.clientOpts)
189-
t.Client = t.createTestClient(t.clientOpts)
182+
t.createTestClient()
190183
}
191184

192185
wrapped.Cleanup(t.cleanup)
@@ -209,7 +202,7 @@ func (t *T) cleanup() {
209202

210203
// always disconnect the client regardless of clientType because Client.Disconnect will work against
211204
// all deployments
212-
_ = t.fpClient.Disconnect(context.Background())
205+
// _ = t.fpClient.Disconnect(context.Background())
213206
_ = t.Client.Disconnect(context.Background())
214207
}
215208

@@ -233,23 +226,15 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
233226
sub.AddMockResponses(sub.mockResponses...)
234227
}
235228

236-
// if sub.fpClient == nil {
237-
// clientOpts := sub.clientOpts
238-
// if clientOpts != nil {
239-
// clientOpts.AutoEncryptionOptions = nil
240-
// }
241-
// sub.fpClient = sub.createTestClient(clientOpts)
242-
// }
243229
// for shareClient, inherit the client from the parent
244230
if sub.shareClient != nil && *sub.shareClient && sub.clientType == t.clientType {
245-
sub.fpClient = t.fpClient
231+
// sub.fpClient = t.fpClient
246232
sub.Client = t.Client
247233
}
248234
// only create a client if not already set
249235
if sub.Client == nil {
250236
if sub.createClient == nil || *sub.createClient {
251-
sub.fpClient = sub.createTestClient(sub.clientOpts)
252-
sub.Client = sub.createTestClient(sub.clientOpts)
237+
sub.createTestClient()
253238
}
254239
}
255240
// create a collection for this test
@@ -274,7 +259,7 @@ func (t *T) RunOpts(name string, opts *Options, callback func(mt *T)) {
274259
}
275260
// only disconnect client if it's not being shared
276261
if sub.shareClient == nil || !*sub.shareClient {
277-
_ = sub.fpClient.Disconnect(context.Background())
262+
// _ = sub.fpClient.Disconnect(context.Background())
278263
_ = sub.Client.Disconnect(context.Background())
279264
}
280265
assert.Equal(sub, 0, sessions, "%v sessions checked out", sessions)
@@ -424,10 +409,9 @@ func (t *T) ResetClient(opts *options.ClientOptions) {
424409
t.clientOpts = opts
425410
}
426411

427-
_ = t.fpClient.Disconnect(context.Background())
412+
// _ = t.fpClient.Disconnect(context.Background())
428413
_ = t.Client.Disconnect(context.Background())
429-
t.fpClient = t.createTestClient(t.clientOpts)
430-
t.Client = t.createTestClient(t.clientOpts)
414+
t.createTestClient()
431415
t.DB = t.Client.Database(t.dbName)
432416
t.Coll = t.DB.Collection(t.collName, t.collOpts)
433417

@@ -619,8 +603,6 @@ func (t *T) ClearFailPoints() {
619603
}
620604
}
621605
t.failPointNames = t.failPointNames[:0]
622-
// t.fpClient.Disconnect(context.Background())
623-
// t.fpClient = nil
624606
}
625607

626608
// CloneDatabase modifies the default database for this test to match the given options.
@@ -648,10 +630,14 @@ func sanitizeCollectionName(db string, coll string) string {
648630
return coll
649631
}
650632

651-
func (t *T) createTestClient(clientOpts *options.ClientOptions) *mongo.Client {
652-
if clientOpts == nil {
633+
func (t *T) createTestClient() {
634+
var clientOpts *options.ClientOptions
635+
if t.clientOpts == nil {
653636
// default opts
654637
clientOpts = options.Client().SetWriteConcern(MajorityWc).SetReadPreference(PrimaryRp)
638+
} else {
639+
opts := *t.clientOpts
640+
clientOpts = &opts
655641
}
656642
// set ServerAPIOptions to latest version if required
657643
if clientOpts.Deployment == nil && t.clientType != Mock && clientOpts.ServerAPIOptions == nil && testContext.requireAPIVersion {
@@ -706,20 +692,17 @@ func (t *T) createTestClient(clientOpts *options.ClientOptions) *mongo.Client {
706692
})
707693
}
708694

709-
var client *mongo.Client
710-
var err error
695+
var uriOpts *options.ClientOptions
711696
switch t.clientType {
712697
case Pinned:
713698
// pin to first mongos
714699
pinnedHostList := []string{testContext.connString.Hosts[0]}
715-
uriOpts := options.Client().ApplyURI(testContext.connString.Original).SetHosts(pinnedHostList)
716-
client, err = mongo.NewClient(uriOpts, clientOpts)
700+
uriOpts = options.Client().ApplyURI(testContext.connString.Original).SetHosts(pinnedHostList)
717701
case Mock:
718702
// clear pool monitor to avoid configuration error
719703
clientOpts.PoolMonitor = nil
720704
t.mockDeployment = newMockDeployment()
721705
clientOpts.Deployment = t.mockDeployment
722-
client, err = mongo.NewClient(clientOpts)
723706
case Proxy:
724707
t.proxyDialer = newProxyDialer()
725708
clientOpts.SetDialer(t.proxyDialer)
@@ -729,23 +712,21 @@ func (t *T) createTestClient(clientOpts *options.ClientOptions) *mongo.Client {
729712
case Default:
730713
// Use a different set of options to specify the URI because clientOpts may already have a URI or host seedlist
731714
// specified.
732-
var uriOpts *options.ClientOptions
733715
if clientOpts.Deployment == nil {
734716
// Only specify URI if the deployment is not set to avoid setting topology/server options along with the
735717
// deployment.
736718
uriOpts = options.Client().ApplyURI(testContext.connString.Original)
737719
}
738-
739-
// Pass in uriOpts first so clientOpts wins if there are any conflicting settings.
740-
client, err = mongo.NewClient(uriOpts, clientOpts)
741720
}
721+
var err error
722+
// Pass in uriOpts first so clientOpts wins if there are any conflicting settings.
723+
t.Client, err = mongo.NewClient(uriOpts, clientOpts)
742724
if err != nil {
743725
t.Fatalf("error creating client: %v", err)
744726
}
745-
if err := client.Connect(context.Background()); err != nil {
727+
if err := t.Client.Connect(context.Background()); err != nil {
746728
t.Fatalf("error connecting client: %v", err)
747729
}
748-
return client
749730
}
750731

751732
func (t *T) createTestCollection() {

0 commit comments

Comments
 (0)