Skip to content

Commit 40c7b7e

Browse files
GODRIVER-2458 Multi-batch writes CSOT test. (#1081)
Co-authored-by: Kevin Albertson <[email protected]>
1 parent d30e7bc commit 40c7b7e

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

mongo/integration/csot_cse_prose_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestCSOTClientSideEncryptionProse(t *testing.T) {
2828
mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").CreateClient(false))
2929
defer mt.Close()
3030

31-
mt.RunOpts("1. maxTimeMS is not set for commands sent to mongocryptd",
31+
mt.RunOpts("2. maxTimeMS is not set for commands sent to mongocryptd",
3232
noClientOpts, func(mt *mtest.T) {
3333
if testing.Short() {
3434
mt.Skip("skipping integration test in short mode")

mongo/integration/csot_prose_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ package integration
88

99
import (
1010
"context"
11+
"strings"
1112
"testing"
1213
"time"
1314

15+
"go.mongodb.org/mongo-driver/bson"
16+
"go.mongodb.org/mongo-driver/event"
17+
"go.mongodb.org/mongo-driver/internal/testutil"
1418
"go.mongodb.org/mongo-driver/internal/testutil/assert"
19+
"go.mongodb.org/mongo-driver/mongo"
1520
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
1621
"go.mongodb.org/mongo-driver/mongo/options"
1722
)
@@ -20,7 +25,66 @@ func TestCSOTProse(t *testing.T) {
2025
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
2126
defer mt.Close()
2227

23-
mt.Run("server selection", func(mt *mtest.T) {
28+
mt.RunOpts("1. multi-batch writes", mtest.NewOptions().MinServerVersion("4.4").
29+
Topologies(mtest.Single), func(mt *mtest.T) {
30+
// Test that multi-batch writes do not refresh the Timeout between batches.
31+
32+
err := mt.Client.Database("db").Collection("coll").Drop(context.Background())
33+
assert.Nil(mt, err, "Drop error: %v", err)
34+
35+
// Configure a fail point to block both inserts of the multi-write for 1010ms (2020ms total).
36+
mt.SetFailPoint(mtest.FailPoint{
37+
ConfigureFailPoint: "failCommand",
38+
Mode: mtest.FailPointMode{
39+
Times: 2,
40+
},
41+
Data: mtest.FailPointData{
42+
FailCommands: []string{"insert"},
43+
BlockConnection: true,
44+
BlockTimeMS: 1010,
45+
},
46+
})
47+
48+
// Use a separate client with 2s Timeout and a separate command monitor to run a multi-batch
49+
// insert against db.coll.
50+
var started []*event.CommandStartedEvent
51+
cm := &event.CommandMonitor{
52+
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
53+
started = append(started, evt)
54+
},
55+
}
56+
cliOptions := options.Client().
57+
SetTimeout(2 * time.Second).
58+
SetMonitor(cm).
59+
ApplyURI(mtest.ClusterURI())
60+
testutil.AddTestServerAPIVersion(cliOptions)
61+
cli, err := mongo.Connect(context.Background(), cliOptions)
62+
assert.Nil(mt, err, "Connect error: %v", err)
63+
64+
// Insert 50 1MB documents (OP_MSG payloads can only fit 48MB in one batch).
65+
var bigStringBuilder strings.Builder
66+
for i := 0; i < 1024*1024; i++ {
67+
bigStringBuilder.WriteByte('a')
68+
}
69+
bigString := bigStringBuilder.String()
70+
var docs []interface{}
71+
for i := 0; i < 50; i++ {
72+
docs = append(docs, bson.D{{"1mb", bigString}})
73+
}
74+
75+
// Expect a timeout error from InsertMany (from the second batch).
76+
_, err = cli.Database("db").Collection("coll").InsertMany(context.Background(), docs)
77+
assert.NotNil(mt, err, "expected error from InsertMany, got nil")
78+
assert.True(mt, mongo.IsTimeout(err), "expected error to be a timeout, got %v", err)
79+
80+
// Expect that two 'insert's were sent.
81+
assert.True(mt, len(started) == 2, "expected two started events, got %d", len(started))
82+
assert.Equal(mt, started[0].CommandName,
83+
"insert", "expected an insert event, got %v", started[0].CommandName)
84+
assert.Equal(mt, started[1].CommandName,
85+
"insert", "expected a second insert event, got %v", started[1].CommandName)
86+
})
87+
mt.Run("8. server selection", func(mt *mtest.T) {
2488
cliOpts := options.Client().ApplyURI("mongodb://invalid/?serverSelectionTimeoutMS=10")
2589
mtOpts := mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false)
2690
mt.RunOpts("serverSelectionTimeoutMS honored if timeoutMS is not set", mtOpts, func(mt *mtest.T) {

0 commit comments

Comments
 (0)