Skip to content

Replace all uses of mtest.Background with context.Background() #839

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

Merged
merged 1 commit into from
Jan 19, 2022
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
41 changes: 21 additions & 20 deletions mongo/integration/causal_consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package integration

import (
"context"
"testing"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -36,14 +37,14 @@ func TestCausalConsistency_Supported(t *testing.T) {

sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())
assert.Nil(mt, sess.OperationTime(), "expected nil operation time, got %v", sess.OperationTime())
})
mt.Run("no cluster time on first command", func(mt *mtest.T) {
// first read in a causally consistent session must not send afterClusterTime to the server

ccOpts := options.Session().SetCausalConsistency(true)
_ = mt.Client.UseSessionWithOptions(mtest.Background, ccOpts, func(sc mongo.SessionContext) error {
_ = mt.Client.UseSessionWithOptions(context.Background(), ccOpts, func(sc mongo.SessionContext) error {
_, _ = mt.Coll.Find(sc, bson.D{})
return nil
})
Expand All @@ -57,9 +58,9 @@ func TestCausalConsistency_Supported(t *testing.T) {

sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_, _ = mt.Coll.Find(sc, bson.D{})
return nil
})
Expand All @@ -85,9 +86,9 @@ func TestCausalConsistency_Supported(t *testing.T) {
mt.Run(sf.name, func(mt *mtest.T) {
sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
Expand Down Expand Up @@ -115,14 +116,14 @@ func TestCausalConsistency_Supported(t *testing.T) {
mt.Run(sf.name, func(mt *mtest.T) {
sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = sf.execute(mt, sess)
currOptime := sess.OperationTime()
assert.NotNil(mt, currOptime, "expected session operation time, got nil")

mt.ClearEvents()
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
Expand All @@ -136,7 +137,7 @@ func TestCausalConsistency_Supported(t *testing.T) {
// a read operation in a non causally-consistent session should not include afterClusterTime

sessOpts := options.Session().SetCausalConsistency(false)
_ = mt.Client.UseSessionWithOptions(mtest.Background, sessOpts, func(sc mongo.SessionContext) error {
_ = mt.Client.UseSessionWithOptions(context.Background(), sessOpts, func(sc mongo.SessionContext) error {
_, _ = mt.Coll.Find(sc, bson.D{})
mt.ClearEvents()
_, _ = mt.Coll.Find(sc, bson.D{})
Expand All @@ -152,15 +153,15 @@ func TestCausalConsistency_Supported(t *testing.T) {

sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
currOptime := sess.OperationTime()
mt.ClearEvents()
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
Expand All @@ -174,15 +175,15 @@ func TestCausalConsistency_Supported(t *testing.T) {
mt.RunOpts("custom read concern", mtest.NewOptions().ClientOptions(localRcOpts), func(mt *mtest.T) {
sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
currOptime := sess.OperationTime()
mt.ClearEvents()
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_ = mt.Coll.FindOne(sc, bson.D{})
return nil
})
Expand All @@ -198,9 +199,9 @@ func TestCausalConsistency_Supported(t *testing.T) {

sess, err := mt.Client.StartSession()
assert.Nil(mt, err, "StartSession error: %v", err)
defer sess.EndSession(mtest.Background)
defer sess.EndSession(context.Background())

_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
_, _ = mt.Coll.InsertOne(sc, bson.D{{"x", 1}})
return nil
})
Expand All @@ -210,7 +211,7 @@ func TestCausalConsistency_Supported(t *testing.T) {
mt.Run("clusterTime included", func(mt *mtest.T) {
// $clusterTime should be included in commands if the deployment supports cluster times

_ = mt.Coll.FindOne(mtest.Background, bson.D{})
_ = mt.Coll.FindOne(context.Background(), bson.D{})
evt := mt.GetStartedEvent()
assert.Equal(mt, "find", evt.CommandName, "expected command 'find', got '%v'", evt.CommandName)
_, err := evt.Command.LookupErr("$clusterTime")
Expand All @@ -233,7 +234,7 @@ func TestCausalConsistency_NotSupported(t *testing.T) {
// support cluster times

sessOpts := options.Session().SetCausalConsistency(true)
_ = mt.Client.UseSessionWithOptions(mtest.Background, sessOpts, func(sc mongo.SessionContext) error {
_ = mt.Client.UseSessionWithOptions(context.Background(), sessOpts, func(sc mongo.SessionContext) error {
_, _ = mt.Coll.Find(sc, bson.D{})
return nil
})
Expand All @@ -245,7 +246,7 @@ func TestCausalConsistency_NotSupported(t *testing.T) {
mt.Run("clusterTime not included", func(mt *mtest.T) {
// $clusterTime should not be included in commands if the deployment does not support cluster times

_ = mt.Coll.FindOne(mtest.Background, bson.D{})
_ = mt.Coll.FindOne(context.Background(), bson.D{})
evt := mt.GetStartedEvent()
assert.Equal(mt, "find", evt.CommandName, "expected command 'find', got '%v'", evt.CommandName)
_, err := evt.Command.LookupErr("$clusterTime")
Expand Down
11 changes: 6 additions & 5 deletions mongo/integration/change_stream_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package integration

import (
"context"
"io/ioutil"
"path"
"testing"
Expand Down Expand Up @@ -133,20 +134,20 @@ func runChangeStreamTest(mt *mtest.T, test changeStreamTest, testFile changeStre
mt.Fatalf("unrecognized change stream target: %v", test.Target)
}
csOpts := createChangeStreamOptions(mt, test.Options)
changeStream, err := watcher.Watch(mtest.Background, test.Pipeline, csOpts)
changeStream, err := watcher.Watch(context.Background(), test.Pipeline, csOpts)
if err == nil {
err = runChangeStreamOperations(mt, test)
}
if err == nil && test.Result.Error != nil {
// if there was no error and an error is expected, capture the result from iterating the stream once
changeStream.Next(mtest.Background)
changeStream.Next(context.Background())
err = changeStream.Err()
}
if err == nil && len(test.Result.Success) != 0 {
// if there was no error and success array is non-empty, iterate stream until it returns as many changes
// as there are elements in the success array or an error is thrown
for i := 0; i < len(test.Result.Success); i++ {
if !changeStream.Next(mtest.Background) {
if !changeStream.Next(context.Background()) {
break
}

Expand All @@ -158,7 +159,7 @@ func runChangeStreamTest(mt *mtest.T, test changeStreamTest, testFile changeStre
err = changeStream.Err()
}
if changeStream != nil {
closeErr := changeStream.Close(mtest.Background)
closeErr := changeStream.Close(context.Background())
assert.Nil(mt, closeErr, "Close error: %v", err)
}

Expand Down Expand Up @@ -213,7 +214,7 @@ func runChangeStreamOperations(mt *mtest.T, test changeStreamTest) error {
}, false)
err = res.Err()
case "drop":
err = mt.Coll.Drop(mtest.Background)
err = mt.Coll.Drop(context.Background())
default:
mt.Fatalf("unrecognized operation: %v", op.Name)
}
Expand Down
Loading