Skip to content

Commit 2a39aae

Browse files
authored
Replace all uses of mtest.Background with context.Background() (#839)
1 parent cec2f72 commit 2a39aae

35 files changed

+678
-661
lines changed

mongo/integration/causal_consistency_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package integration
88

99
import (
10+
"context"
1011
"testing"
1112

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

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

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

5859
sess, err := mt.Client.StartSession()
5960
assert.Nil(mt, err, "StartSession error: %v", err)
60-
defer sess.EndSession(mtest.Background)
61+
defer sess.EndSession(context.Background())
6162

62-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
63+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
6364
_, _ = mt.Coll.Find(sc, bson.D{})
6465
return nil
6566
})
@@ -85,9 +86,9 @@ func TestCausalConsistency_Supported(t *testing.T) {
8586
mt.Run(sf.name, func(mt *mtest.T) {
8687
sess, err := mt.Client.StartSession()
8788
assert.Nil(mt, err, "StartSession error: %v", err)
88-
defer sess.EndSession(mtest.Background)
89+
defer sess.EndSession(context.Background())
8990

90-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
91+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
9192
_ = mt.Coll.FindOne(sc, bson.D{})
9293
return nil
9394
})
@@ -115,14 +116,14 @@ func TestCausalConsistency_Supported(t *testing.T) {
115116
mt.Run(sf.name, func(mt *mtest.T) {
116117
sess, err := mt.Client.StartSession()
117118
assert.Nil(mt, err, "StartSession error: %v", err)
118-
defer sess.EndSession(mtest.Background)
119+
defer sess.EndSession(context.Background())
119120

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

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

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

153154
sess, err := mt.Client.StartSession()
154155
assert.Nil(mt, err, "StartSession error: %v", err)
155-
defer sess.EndSession(mtest.Background)
156+
defer sess.EndSession(context.Background())
156157

157-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
158+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
158159
_ = mt.Coll.FindOne(sc, bson.D{})
159160
return nil
160161
})
161162
currOptime := sess.OperationTime()
162163
mt.ClearEvents()
163-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
164+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
164165
_ = mt.Coll.FindOne(sc, bson.D{})
165166
return nil
166167
})
@@ -174,15 +175,15 @@ func TestCausalConsistency_Supported(t *testing.T) {
174175
mt.RunOpts("custom read concern", mtest.NewOptions().ClientOptions(localRcOpts), func(mt *mtest.T) {
175176
sess, err := mt.Client.StartSession()
176177
assert.Nil(mt, err, "StartSession error: %v", err)
177-
defer sess.EndSession(mtest.Background)
178+
defer sess.EndSession(context.Background())
178179

179-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
180+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
180181
_ = mt.Coll.FindOne(sc, bson.D{})
181182
return nil
182183
})
183184
currOptime := sess.OperationTime()
184185
mt.ClearEvents()
185-
_ = mongo.WithSession(mtest.Background, sess, func(sc mongo.SessionContext) error {
186+
_ = mongo.WithSession(context.Background(), sess, func(sc mongo.SessionContext) error {
186187
_ = mt.Coll.FindOne(sc, bson.D{})
187188
return nil
188189
})
@@ -198,9 +199,9 @@ func TestCausalConsistency_Supported(t *testing.T) {
198199

199200
sess, err := mt.Client.StartSession()
200201
assert.Nil(mt, err, "StartSession error: %v", err)
201-
defer sess.EndSession(mtest.Background)
202+
defer sess.EndSession(context.Background())
202203

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

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

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

248-
_ = mt.Coll.FindOne(mtest.Background, bson.D{})
249+
_ = mt.Coll.FindOne(context.Background(), bson.D{})
249250
evt := mt.GetStartedEvent()
250251
assert.Equal(mt, "find", evt.CommandName, "expected command 'find', got '%v'", evt.CommandName)
251252
_, err := evt.Command.LookupErr("$clusterTime")

mongo/integration/change_stream_spec_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package integration
88

99
import (
10+
"context"
1011
"io/ioutil"
1112
"path"
1213
"testing"
@@ -133,20 +134,20 @@ func runChangeStreamTest(mt *mtest.T, test changeStreamTest, testFile changeStre
133134
mt.Fatalf("unrecognized change stream target: %v", test.Target)
134135
}
135136
csOpts := createChangeStreamOptions(mt, test.Options)
136-
changeStream, err := watcher.Watch(mtest.Background, test.Pipeline, csOpts)
137+
changeStream, err := watcher.Watch(context.Background(), test.Pipeline, csOpts)
137138
if err == nil {
138139
err = runChangeStreamOperations(mt, test)
139140
}
140141
if err == nil && test.Result.Error != nil {
141142
// if there was no error and an error is expected, capture the result from iterating the stream once
142-
changeStream.Next(mtest.Background)
143+
changeStream.Next(context.Background())
143144
err = changeStream.Err()
144145
}
145146
if err == nil && len(test.Result.Success) != 0 {
146147
// if there was no error and success array is non-empty, iterate stream until it returns as many changes
147148
// as there are elements in the success array or an error is thrown
148149
for i := 0; i < len(test.Result.Success); i++ {
149-
if !changeStream.Next(mtest.Background) {
150+
if !changeStream.Next(context.Background()) {
150151
break
151152
}
152153

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

@@ -213,7 +214,7 @@ func runChangeStreamOperations(mt *mtest.T, test changeStreamTest) error {
213214
}, false)
214215
err = res.Err()
215216
case "drop":
216-
err = mt.Coll.Drop(mtest.Background)
217+
err = mt.Coll.Drop(context.Background())
217218
default:
218219
mt.Fatalf("unrecognized operation: %v", op.Name)
219220
}

0 commit comments

Comments
 (0)