Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2876 Automatically clean up mtest resources. #1347

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestDocumentationExamples(t *testing.T) {
})

mt := mtest.New(t)
defer mt.Close()

// Stable API is supported in 5.0+
mtOpts := mtest.NewOptions().MinServerVersion("5.0")
Expand Down
2 changes: 0 additions & 2 deletions mongo/integration/causal_consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var readConcernOperations = map[string]struct{}{

func TestCausalConsistency_Supported(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("3.6").Topologies(mtest.ReplicaSet, mtest.Sharded).CreateClient(false))
defer mt.Close()

mt.Run("operation time nil", func(mt *mtest.T) {
// when a ClientSession is first created, the operation time is nil
Expand Down Expand Up @@ -211,7 +210,6 @@ func TestCausalConsistency_NotSupported(t *testing.T) {
{Topology: []mtest.TopologyKind{mtest.Single}},
}
mt := mtest.New(t, mtest.NewOptions().RunOn(rob...).CreateClient(false))
defer mt.Close()

mt.Run("afterClusterTime not included", func(mt *mtest.T) {
// a read in a causally consistent session does not include afterClusterTime in a deployment that does not
Expand Down
2 changes: 0 additions & 2 deletions mongo/integration/change_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const (
func TestChangeStream_Standalone(t *testing.T) {
mtOpts := mtest.NewOptions().MinServerVersion(minChangeStreamVersion).CreateClient(false).Topologies(mtest.Single)
mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.Run("no custom standalone error", func(mt *mtest.T) {
_, err := mt.Coll.Watch(context.Background(), mongo.Pipeline{})
Expand All @@ -61,7 +60,6 @@ func TestChangeStream_Standalone(t *testing.T) {
func TestChangeStream_ReplicaSet(t *testing.T) {
mtOpts := mtest.NewOptions().MinServerVersion(minChangeStreamVersion).CreateClient(false).Topologies(mtest.ReplicaSet)
mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.Run("first stage is $changeStream", func(mt *mtest.T) {
// first stage in the aggregate pipeline must be $changeStream
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/clam_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func TestCommandLoggingAndMonitoringProse(t *testing.T) {
t.Parallel()

mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

for _, tcase := range []struct {
// name is the name of the test case
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/client_side_encryption_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func TestClientSideEncryptionProse(t *testing.T) {

verifyClientSideEncryptionVarsSet(t)
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").Enterprise(true).CreateClient(false))
defer mt.Close()

defaultKvClientOptions := options.Client().ApplyURI(mtest.ClusterURI())
integtest.AddTestServerAPIVersion(defaultKvClientOptions)
Expand Down
4 changes: 0 additions & 4 deletions mongo/integration/client_side_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func makeMonitor(mt *mtest.T, captured *[]event.CommandStartedEvent) *event.Comm
func TestClientSideEncryptionWithExplicitSessions(t *testing.T) {
verifyClientSideEncryptionVarsSet(t)
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").Enterprise(true).CreateClient(false))
defer mt.Close()

kmsProvidersMap := map[string]map[string]interface{}{
"local": {"key": localMasterKey},
Expand Down Expand Up @@ -345,7 +344,6 @@ func (c *customCrypt) RewrapDataKey(_ context.Context, _ []byte,

func TestClientSideEncryptionCustomCrypt(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").Enterprise(true).CreateClient(false))
defer mt.Close()

kmsProvidersMap := map[string]map[string]interface{}{
"local": {"key": localMasterKey},
Expand Down Expand Up @@ -414,7 +412,6 @@ func TestFLE2CreateCollection(t *testing.T) {
mtest.LoadBalanced,
mtest.ShardedReplicaSet)
mt := mtest.New(t, mtOpts)
defer mt.Close()

efJSON := `
{
Expand Down Expand Up @@ -488,7 +485,6 @@ func TestFLE2DocsExample(t *testing.T) {
mtest.LoadBalanced,
mtest.ShardedReplicaSet)
mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.Run("Auto Encryption", func(mt *mtest.T) {
// Drop data from prior test runs.
Expand Down
3 changes: 0 additions & 3 deletions mongo/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (sc *slowConn) Read(b []byte) (n int, err error) {

func TestClient(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

registryOpts := options.Client().
SetRegistry(bson.NewRegistryBuilder().RegisterCodec(reflect.TypeOf(int64(0)), &negateCodec{}).Build())
Expand Down Expand Up @@ -798,7 +797,6 @@ func TestClient_BSONOptions(t *testing.T) {
t.Parallel()

mt := mtest.New(t, noClientOpts)
defer mt.Close()

type jsonTagsTest struct {
A string
Expand Down Expand Up @@ -917,7 +915,6 @@ func TestClient_BSONOptions(t *testing.T) {
func TestClientStress(t *testing.T) {
mtOpts := mtest.NewOptions().CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

// Test that a Client can recover from a massive traffic spike after the traffic spike is over.
mt.Run("Client recovers from traffic spike", func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var (

func TestCollection(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

mt.RunOpts("insert one", noClientOpts, func(mt *mtest.T) {
mt.Run("success", func(mt *mtest.T) {
Expand Down
5 changes: 0 additions & 5 deletions mongo/integration/crud_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestWriteErrorsWithLabels(t *testing.T) {
mtOpts := mtest.NewOptions().ClientOptions(clientOpts).MinServerVersion("4.0").Topologies(mtest.ReplicaSet).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

label := "ExampleError"
mt.Run("InsertMany errors with label", func(mt *mtest.T) {
Expand Down Expand Up @@ -124,7 +123,6 @@ func TestWriteErrorsDetails(t *testing.T) {
CreateClient(false)

mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.Run("JSON Schema validation", func(mt *mtest.T) {
// Create a JSON Schema validator document that requires properties "a" and "b". Use it in
Expand Down Expand Up @@ -293,7 +291,6 @@ func TestWriteErrorsDetails(t *testing.T) {
func TestHintErrors(t *testing.T) {
mtOpts := mtest.NewOptions().MaxServerVersion("3.2").CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

expected := errors.New("the 'hint' command parameter requires a minimum server wire version of 5")
mt.Run("UpdateMany", func(mt *mtest.T) {
Expand Down Expand Up @@ -325,7 +322,6 @@ func TestHintErrors(t *testing.T) {

func TestWriteConcernError(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

errInfoOpts := mtest.NewOptions().MinServerVersion("4.0").Topologies(mtest.ReplicaSet)
mt.RunOpts("errInfo is propagated", errInfoOpts, func(mt *mtest.T) {
Expand Down Expand Up @@ -372,7 +368,6 @@ func TestErrorsCodeNamePropagated(t *testing.T) {
Topologies(mtest.ReplicaSet).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.RunOpts("command error", mtest.NewOptions().MinServerVersion("3.4"), func(mt *mtest.T) {
// codeName is propagated in an ok:0 error.
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/crud_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func runCrudFile(t *testing.T, file string) {
assert.Nil(t, err, "UnmarshalExtJSONWithRegistry error: %v", err)

mt := mtest.New(t, mtest.NewOptions().MinServerVersion(testFile.MinServerVersion).MaxServerVersion(testFile.MaxServerVersion))
defer mt.Close()

// Skip test if serverless requirements are not met.
if err = verifyServerlessConstraint(mt, testFile.Serverless); err != nil {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/csot_cse_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
func TestCSOTClientSideEncryptionProse(t *testing.T) {
verifyClientSideEncryptionVarsSet(t)
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").CreateClient(false))
defer mt.Close()

mt.RunOpts("2. maxTimeMS is not set for commands sent to mongocryptd",
noClientOpts, func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/csot_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

func TestCSOTProse(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

mt.RunOpts("1. multi-batch writes", mtest.NewOptions().MinServerVersion("4.4").
Topologies(mtest.Single), func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (

func TestCursor(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()
cappedCollectionOpts := options.CreateCollection().SetCapped(true).SetSizeInBytes(64 * 1024)

// Server versions 2.6 and 3.0 use OP_GET_MORE so this works on >= 3.2 and when RequireAPIVersion is false;
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/data_lake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestAtlasDataLake(t *testing.T) {
// Prose tests against Atlas Data Lake.

mt := mtest.New(t, mtest.NewOptions().AtlasDataLake(true).CreateClient(false))
defer mt.Close()
getMtOpts := func() *mtest.Options {
return mtest.NewOptions().CollectionName("driverdata")
}
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var (

func TestDatabase(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

mt.RunOpts("run command", noClientOpts, func(mt *mtest.T) {
mt.Run("decode raw", func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (we wrappedError) Unwrap() error {

func TestErrors(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

mt.RunOpts("errors are wrapped", noClientOpts, func(mt *mtest.T) {
mt.Run("network error during application operation", func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/gridfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

func TestGridFS(x *testing.T) {
mt := mtest.New(x, noClientOpts)
defer mt.Close()

mt.Run("skipping download", func(mt *mtest.T) {
data := []byte("abc.def.ghi")
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

func TestHandshakeProse(t *testing.T) {
mt := mtest.New(t)
defer mt.Close()

opts := mtest.NewOptions().
CreateCollection(false).
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type index struct {

func TestIndexView(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

var pbool = func(b bool) *bool { return &b }
var pint32 = func(i int32) *int32 { return &i }
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/initial_dns_seedlist_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type seedlistTest struct {

func TestInitialDNSSeedlistDiscoverySpec(t *testing.T) {
mt := mtest.New(t, noClientOpts)
defer mt.Close()

mt.RunOpts("replica set", mtest.NewOptions().Topologies(mtest.ReplicaSet).CreateClient(false), func(mt *mtest.T) {
mt.Parallel()
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/load_balancer_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

func TestLoadBalancerSupport(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().Topologies(mtest.LoadBalanced).CreateClient(false))
defer mt.Close()

mt.Run("RunCommandCursor pins to a connection", func(mt *mtest.T) {
// The LB spec tests cover the behavior for cursors created by CRUD operations, but RunCommandCursor is
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/mock_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func getItems(f finder) ([]ShopItem, error) {

func TestMockFind(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

insertItems := []interface{}{
ShopItem{ID: 0, Price: 1.5},
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/mongos_pinning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestMongosPinning(t *testing.T) {
mtOpts := mtest.NewOptions().Topologies(mtest.Sharded).MinServerVersion("4.1").CreateClient(false).
ClientOptions(clientOpts)
mt := mtest.New(t, mtOpts)
defer mt.Close()

if len(options.Client().ApplyURI(mtest.ClusterURI()).Hosts) < 2 {
mt.Skip("skipping because at least 2 mongoses are required")
Expand Down
7 changes: 5 additions & 2 deletions mongo/integration/mtest/mongotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ func New(wrapped *testing.T, opts ...*Options) *T {
t.createTestClient()
}

wrapped.Cleanup(t.cleanup)

return t
}

// Close cleans up any resources associated with a T. There should be one Close corresponding to every New.
func (t *T) Close() {
// cleanup cleans up any resources associated with a T. It is intended to be
// called by [testing.T.Cleanup].
func (t *T) cleanup() {
if t.Client == nil {
return
}
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/primary_stepdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (

func TestConnectionsSurvivePrimaryStepDown(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().Topologies(mtest.ReplicaSet).CreateClient(false))
defer mt.Close()

getMoreOpts := mtest.NewOptions().MinServerVersion("4.2")
mt.RunOpts("getMore iteration", getMoreOpts, func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/retryable_reads_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestRetryableReadsProse(t *testing.T) {

mtOpts := mtest.NewOptions().ClientOptions(clientOpts).MinServerVersion("4.3")
mt := mtest.New(t, mtOpts)
defer mt.Close()

mt.Run("PoolClearedError retryability", func(mt *mtest.T) {
if mtest.ClusterTopologyKind() == mtest.LoadBalanced {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/retryable_writes_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestRetryableWritesProse(t *testing.T) {
SetReadConcern(mtest.MajorityRc)
mtOpts := mtest.NewOptions().ClientOptions(clientOpts).MinServerVersion("3.6").CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

includeOpts := mtest.NewOptions().Topologies(mtest.ReplicaSet, mtest.Sharded).CreateClient(false)
mt.RunOpts("txn number included", includeOpts, func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/retryable_writes_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func runRetryableWritesFile(t *testing.T, filePath string) {
assert.Nil(t, err, "UnmarshalExtJSONWithRegistry error: %v", err)

mt := mtest.New(t, mtest.NewOptions().RunOn(testFile.RunOn...).CreateClient(false))
defer mt.Close()

for _, test := range testFile.Tests {
runRetryableWritesTest(mt, test, testFile)
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/sdam_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

func TestSDAMProse(t *testing.T) {
mt := mtest.New(t)
defer mt.Close()

// Server limits non-streaming heartbeats and explicit server transition checks to at most one
// per 500ms. Set the test interval to 500ms to minimize the difference between the behavior of
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/server_selection_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func TestServerSelectionProse(t *testing.T) {
const localThreshold = 30 * time.Second

mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
defer mt.Close()

mtOpts := mtest.NewOptions().Topologies(mtest.Sharded).MinServerVersion("4.9")
mt.RunOpts("operationCount-based selection within latency window, with failpoint", mtOpts, func(mt *mtest.T) {
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/sessions_mongocryptd_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func TestSessionsMongocryptdProse(t *testing.T) {
// Create a new instance of mtest (MongoDB testing framework) for this
// test and configure it to control server versions.
mt := mtest.New(t, mtOpts)
mt.Cleanup(mt.Close)

proseTest18 := "18. implicit session is ignored if connection does not support sessions"
mt.RunOpts(proseTest18, mtOpts, func(mt *mtest.T) {
Expand Down
2 changes: 0 additions & 2 deletions mongo/integration/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

func TestSessionPool(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("3.6").CreateClient(false))
defer mt.Close()

mt.Run("last use time updated", func(mt *mtest.T) {
sess, err := mt.Client.StartSession()
Expand Down Expand Up @@ -162,7 +161,6 @@ func TestSessionsProse(t *testing.T) {
CreateClient(false)

mt := mtest.New(t, mtOpts)
defer mt.Close()

hosts := options.Client().ApplyURI(mtest.ClusterURI()).Hosts

Expand Down
1 change: 0 additions & 1 deletion mongo/integration/unified/unified_spec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func runTestFile(t *testing.T, filepath string, expectValidFail bool, opts ...*O
RunOn(fileReqs...).
CreateClient(false)
mt := mtest.New(t, mtOpts)
defer mt.Close()

for _, testCase := range testCases {
mtOpts := mtest.NewOptions().
Expand Down
1 change: 0 additions & 1 deletion mongo/integration/unified_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func runSpecTestFile(t *testing.T, specDir, fileName string) {
mtOpts.AtlasDataLake(true)
}
mt := mtest.New(t, mtOpts)
defer mt.Close()

for _, test := range testFile.Tests {
runSpecTestCase(mt, test, testFile)
Expand Down
Loading