@@ -8,10 +8,15 @@ package integration
8
8
9
9
import (
10
10
"context"
11
+ "strings"
11
12
"testing"
12
13
"time"
13
14
15
+ "go.mongodb.org/mongo-driver/bson"
16
+ "go.mongodb.org/mongo-driver/event"
17
+ "go.mongodb.org/mongo-driver/internal/testutil"
14
18
"go.mongodb.org/mongo-driver/internal/testutil/assert"
19
+ "go.mongodb.org/mongo-driver/mongo"
15
20
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
16
21
"go.mongodb.org/mongo-driver/mongo/options"
17
22
)
@@ -20,7 +25,66 @@ func TestCSOTProse(t *testing.T) {
20
25
mt := mtest .New (t , mtest .NewOptions ().CreateClient (false ))
21
26
defer mt .Close ()
22
27
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 ) {
24
88
cliOpts := options .Client ().ApplyURI ("mongodb://invalid/?serverSelectionTimeoutMS=10" )
25
89
mtOpts := mtest .NewOptions ().ClientOptions (cliOpts ).CreateCollection (false )
26
90
mt .RunOpts ("serverSelectionTimeoutMS honored if timeoutMS is not set" , mtOpts , func (mt * mtest.T ) {
0 commit comments