Skip to content

Commit 384560d

Browse files
authored
GODRIVER-2248 Use mock deployment for slow BulkWrite update test (#827)
1 parent 20a9223 commit 384560d

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

mongo/integration/collection_test.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,11 +1737,12 @@ func TestCollection(t *testing.T) {
17371737
deletes := len(mt.GetAllStartedEvents())
17381738
assert.True(mt, deletes > 1, "expected multiple batches, got %v", deletes)
17391739
})
1740-
mt.Run("update with batches", func(mt *mtest.T) {
1740+
mt.RunOpts("update with batches", mtest.NewOptions().ClientType(mtest.Mock), func(mt *mtest.T) {
1741+
maxBatchCount := int(mtest.MockDescription.MaxBatchCount)
1742+
numModels := maxBatchCount + 50
17411743
var models []mongo.WriteModel
1742-
numModels := 100050
17431744

1744-
// it's significantly faster to upsert one model and modify the rest than to upsert all of them
1745+
// It's significantly faster to upsert the first model and only modify the rest than to upsert all of them.
17451746
for i := 0; i < numModels-1; i++ {
17461747
update := bson.D{
17471748
{"$set", bson.D{
@@ -1755,21 +1756,42 @@ func TestCollection(t *testing.T) {
17551756
SetUpdate(update).SetUpsert(true)
17561757
models = append(models, model)
17571758
}
1758-
// add one last upsert
1759+
// Add one last upsert for second batch.
17591760
models = append(models, mongo.NewUpdateOneModel().
17601761
SetFilter(bson.D{{"x", int32(1)}}).
17611762
SetUpdate(bson.D{{"$set", bson.D{{"x", int32(1)}}}}).
17621763
SetUpsert(true),
17631764
)
17641765

1766+
// Seed mock responses for the BulkWrite.
1767+
//
1768+
// The response from the first batch should look like:
1769+
// {ok: 1, n: 100000, nModified: 99999, upserted: [{index: 0, _id: <id>}]}
1770+
firstBatchUpserted := bson.A{bson.D{{"index", 0}, {"_id", primitive.NewObjectID()}}}
1771+
firstBatchResponse := mtest.CreateSuccessResponse(
1772+
bson.E{"n", 100000},
1773+
bson.E{"nModified", 99999},
1774+
bson.E{"upserted", firstBatchUpserted},
1775+
)
1776+
// The response from the second batch should look like:
1777+
// {ok: 1, n: 50, nModified: 49, upserted: [{index: 49, _id: <id>}]}
1778+
secondBatchUpserted := bson.A{bson.D{{"index", 49}, {"_id", primitive.NewObjectID()}}}
1779+
secondBatchResponse := mtest.CreateSuccessResponse(
1780+
bson.E{"n", 50},
1781+
bson.E{"nModified", 49},
1782+
bson.E{"upserted", secondBatchUpserted},
1783+
)
1784+
mt.AddMockResponses([]primitive.D{firstBatchResponse, secondBatchResponse}...)
1785+
17651786
mt.ClearEvents()
17661787
res, err := mt.Coll.BulkWrite(mtest.Background, models)
17671788
assert.Nil(mt, err, "BulkWrite error: %v", err)
17681789

17691790
mt.FilterStartedEvents(func(evt *event.CommandStartedEvent) bool {
17701791
return evt.CommandName == "update"
17711792
})
1772-
// MaxWriteBatchSize changed between 3.4 and 3.6, so there isn't a given number of batches that this will be split into
1793+
// MaxWriteBatchSize changed between 3.4 and 3.6, so there isn't a given number of batches that
1794+
// this will be split into.
17731795
updates := len(mt.GetAllStartedEvents())
17741796
assert.True(mt, updates > 1, "expected multiple batches, got %v", updates)
17751797

@@ -1778,29 +1800,11 @@ func TestCollection(t *testing.T) {
17781800
assert.Equal(mt, int64(2), res.UpsertedCount, "expected %v upserted documents, got %v", 2, res.UpsertedCount)
17791801
assert.Equal(mt, 2, len(res.UpsertedIDs), "expected %v upserted ids, got %v", 2, len(res.UpsertedIDs))
17801802

1781-
// find the upserted documents and check their contents
1782-
id1, ok := res.UpsertedIDs[0]
1803+
// Check that IDs exist in result for upserted documents.
1804+
_, ok := res.UpsertedIDs[0]
17831805
assert.True(mt, ok, "expected id at key 0")
1784-
id2, ok := res.UpsertedIDs[int64(numModels-1)]
1806+
_, ok = res.UpsertedIDs[int64(numModels-1)]
17851807
assert.True(mt, ok, "expected id at key %v", numModels-1)
1786-
1787-
doc, err := mt.Coll.FindOne(mtest.Background, bson.D{{"_id", id1}}).DecodeBytes()
1788-
assert.Nil(mt, err, "FindOne error: %v", err)
1789-
a, ok := doc.Lookup("a").Int32OK()
1790-
assert.True(mt, ok, "expected a to be an int32")
1791-
assert.Equal(mt, int32(numModels-1), a, "expected a value %v, got %v", numModels-1, a)
1792-
b, ok := doc.Lookup("b").Int32OK()
1793-
assert.True(mt, ok, "expected b to be an int32")
1794-
assert.Equal(mt, int32((numModels-2)*2), b, "expected b value %v, got %v", (numModels-2)*2, b)
1795-
c, ok := doc.Lookup("c").Int32OK()
1796-
assert.True(mt, ok, "expected c to be an int32")
1797-
assert.Equal(mt, int32((numModels-2)*3), c, "expected b value %v, got %v", (numModels-2)*3, c)
1798-
1799-
doc, err = mt.Coll.FindOne(mtest.Background, bson.D{{"_id", id2}}).DecodeBytes()
1800-
assert.Nil(mt, err, "FindOne error: %v", err)
1801-
x, ok := doc.Lookup("x").Int32OK()
1802-
assert.True(mt, ok, "expected x to be an int32")
1803-
assert.Equal(mt, int32(1), x, "expected a value 1, got %v", x)
18041808
})
18051809
mt.RunOpts("map hint", noClientOpts, func(mt *mtest.T) {
18061810
filter := bson.D{{"_id", "foo"}}

0 commit comments

Comments
 (0)