Skip to content

Commit f0cd171

Browse files
committed
cleanup
1 parent f1b81d6 commit f0cd171

File tree

9 files changed

+7705
-14
lines changed

9 files changed

+7705
-14
lines changed

mongo/integration/crud_prose_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestClientBulkWrite(t *testing.T) {
529529
})
530530

531531
mt.Run("bulkWrite handles individual WriteErrors across batches", func(mt *mtest.T) {
532-
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, true)
532+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
533533
err := coll.Drop(context.Background())
534534
require.NoError(mt, err, "Drop error")
535535
_, err = coll.InsertOne(context.Background(), bson.D{{"_id", 1}})
@@ -580,7 +580,7 @@ func TestClientBulkWrite(t *testing.T) {
580580
})
581581

582582
mt.Run("bulkWrite handles a cursor requiring a getMore", func(mt *mtest.T) {
583-
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, true)
583+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
584584
err := coll.Drop(context.Background())
585585
require.NoError(mt, err, "Drop error")
586586

@@ -618,7 +618,7 @@ func TestClientBulkWrite(t *testing.T) {
618618
})
619619

620620
mt.Run("bulkWrite handles a cursor requiring a getMore within a transaction", func(mt *mtest.T) {
621-
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, true)
621+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
622622
err := coll.Drop(context.Background())
623623
require.NoError(mt, err, "Drop error")
624624

@@ -693,7 +693,7 @@ func TestClientBulkWrite(t *testing.T) {
693693
},
694694
})
695695

696-
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, true)
696+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
697697
err = coll.Drop(context.Background())
698698
require.NoError(mt, err, "Drop error")
699699

@@ -910,7 +910,7 @@ func TestClientBulkWrite(t *testing.T) {
910910

911911
mt.ResetClient(options.Client().SetMonitor(monitor))
912912

913-
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, true)
913+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
914914
err = coll.Drop(context.Background())
915915
require.NoError(mt, err, "Drop error")
916916

mongo/integration/csot_prose_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.mongodb.org/mongo-driver/event"
1717
"go.mongodb.org/mongo-driver/internal/assert"
1818
"go.mongodb.org/mongo-driver/internal/integtest"
19+
"go.mongodb.org/mongo-driver/internal/require"
1920
"go.mongodb.org/mongo-driver/mongo"
2021
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
2122
"go.mongodb.org/mongo-driver/mongo/options"
@@ -160,4 +161,56 @@ func TestCSOTProse(t *testing.T) {
160161
"expected ping to fail within 150ms")
161162
})
162163
})
164+
mt.RunOpts("11. multi-batch bulkWrites", mtest.NewOptions().MinServerVersion("8.0").
165+
AtlasDataLake(false).Topologies(mtest.Single), func(mt *mtest.T) {
166+
coll := mt.CreateCollection(mtest.Collection{DB: "db", Name: "coll"}, false)
167+
err := coll.Drop(context.Background())
168+
require.NoError(mt, err, "Drop error: %v", err)
169+
170+
mt.SetFailPoint(mtest.FailPoint{
171+
ConfigureFailPoint: "failCommand",
172+
Mode: mtest.FailPointMode{
173+
Times: 2,
174+
},
175+
Data: mtest.FailPointData{
176+
FailCommands: []string{"bulkWrite"},
177+
BlockConnection: true,
178+
BlockTimeMS: 1010,
179+
},
180+
})
181+
182+
var hello struct {
183+
MaxBsonObjectSize int
184+
MaxMessageSizeBytes int
185+
}
186+
err = mt.DB.RunCommand(context.Background(), bson.D{{"hello", 1}}).Decode(&hello)
187+
require.NoError(mt, err, "Hello error: %v", err)
188+
189+
models := &mongo.ClientWriteModels{}
190+
n := hello.MaxMessageSizeBytes/hello.MaxBsonObjectSize + 1
191+
for i := 0; i < n; i++ {
192+
models.
193+
AppendInsertOne("db", "coll", &mongo.ClientInsertOneModel{
194+
Document: bson.D{{"a", strings.Repeat("b", hello.MaxBsonObjectSize-500)}},
195+
})
196+
}
197+
198+
var cnt int
199+
cm := &event.CommandMonitor{
200+
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
201+
if evt.CommandName == "bulkWrite" {
202+
cnt++
203+
}
204+
},
205+
}
206+
cliOptions := options.Client().
207+
SetTimeout(2 * time.Second).
208+
SetMonitor(cm).
209+
ApplyURI(mtest.ClusterURI())
210+
cli, err := mongo.Connect(context.Background(), cliOptions)
211+
require.NoError(mt, err, "Connect error: %v", err)
212+
_, err = cli.BulkWrite(context.Background(), models)
213+
assert.ErrorContains(mt, err, "context deadline exceeded", "expected a timeout error, got: %v", err)
214+
assert.Equal(mt, 2, cnt, "expected bulkWrite calls: %d, got: %d", 2, cnt)
215+
})
163216
}

mongo/integration/unified/error.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@ import (
1919
// expectedError represents an error that is expected to occur during a test. This type ignores the "isError" field in
2020
// test files because it is always true if it is specified, so the runner can simply assert that an error occurred.
2121
type expectedError struct {
22-
IsClientError *bool `bson:"isClientError"`
23-
IsTimeoutError *bool `bson:"isTimeoutError"`
24-
ErrorSubstring *string `bson:"errorContains"`
25-
Code *int32 `bson:"errorCode"`
26-
CodeName *string `bson:"errorCodeName"`
27-
IncludedLabels []string `bson:"errorLabelsContain"`
28-
OmittedLabels []string `bson:"errorLabelsOmit"`
29-
ExpectedResult *bson.RawValue `bson:"expectResult"`
30-
ErrorResponse *bson.Raw `bson:"errorResponse"`
22+
IsClientError *bool `bson:"isClientError"`
23+
IsTimeoutError *bool `bson:"isTimeoutError"`
24+
ErrorSubstring *string `bson:"errorContains"`
25+
Code *int32 `bson:"errorCode"`
26+
CodeName *string `bson:"errorCodeName"`
27+
IncludedLabels []string `bson:"errorLabelsContain"`
28+
OmittedLabels []string `bson:"errorLabelsOmit"`
29+
ExpectedResult *bson.RawValue `bson:"expectResult"`
30+
ErrorResponse *bson.Raw `bson:"errorResponse"`
31+
WriteErrors map[int]clientBulkWriteException `bson:"writeErrors"`
32+
WriteConcernErrors []clientBulkWriteException `bson:"writeConcernErrors"`
33+
}
34+
35+
type clientBulkWriteException struct {
36+
Code *int `bson:"code"`
37+
Message *string `bson:"message"`
3138
}
3239

3340
// verifyOperationError compares the expected error to the actual operation result. If the expected parameter is nil,
@@ -140,6 +147,40 @@ func verifyOperationError(ctx context.Context, expected *expectedError, result *
140147
return fmt.Errorf("error response comparison error: %w", err)
141148
}
142149
}
150+
if expected.WriteErrors != nil {
151+
var exception mongo.ClientBulkWriteException
152+
if !errors.As(result.Err, &exception) {
153+
return fmt.Errorf("expected a ClientBulkWriteException, got %T", result.Err)
154+
}
155+
if len(expected.WriteErrors) != len(exception.WriteErrors) {
156+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteErrors, exception.WriteErrors)
157+
}
158+
for k, e := range expected.WriteErrors {
159+
if e.Code != nil && *e.Code != exception.WriteErrors[k].Code {
160+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteConcernErrors, exception.WriteConcernErrors)
161+
}
162+
if e.Message != nil && *e.Message != exception.WriteErrors[k].Message {
163+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteConcernErrors, exception.WriteConcernErrors)
164+
}
165+
}
166+
}
167+
if expected.WriteConcernErrors != nil {
168+
var exception mongo.ClientBulkWriteException
169+
if !errors.As(result.Err, &exception) {
170+
return fmt.Errorf("expected a ClientBulkWriteException, got %T", result.Err)
171+
}
172+
if len(expected.WriteConcernErrors) != len(exception.WriteConcernErrors) {
173+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteConcernErrors, exception.WriteConcernErrors)
174+
}
175+
for i, e := range expected.WriteConcernErrors {
176+
if e.Code != nil && *e.Code != exception.WriteConcernErrors[i].Code {
177+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteConcernErrors, exception.WriteConcernErrors)
178+
}
179+
if e.Message != nil && *e.Message != exception.WriteConcernErrors[i].Message {
180+
return fmt.Errorf("expected errors: %v, got: %v", expected.WriteConcernErrors, exception.WriteConcernErrors)
181+
}
182+
}
183+
}
143184
return nil
144185
}
145186

0 commit comments

Comments
 (0)