@@ -1737,11 +1737,12 @@ func TestCollection(t *testing.T) {
1737
1737
deletes := len (mt .GetAllStartedEvents ())
1738
1738
assert .True (mt , deletes > 1 , "expected multiple batches, got %v" , deletes )
1739
1739
})
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
1741
1743
var models []mongo.WriteModel
1742
- numModels := 100050
1743
1744
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.
1745
1746
for i := 0 ; i < numModels - 1 ; i ++ {
1746
1747
update := bson.D {
1747
1748
{"$set" , bson.D {
@@ -1755,21 +1756,42 @@ func TestCollection(t *testing.T) {
1755
1756
SetUpdate (update ).SetUpsert (true )
1756
1757
models = append (models , model )
1757
1758
}
1758
- // add one last upsert
1759
+ // Add one last upsert for second batch.
1759
1760
models = append (models , mongo .NewUpdateOneModel ().
1760
1761
SetFilter (bson.D {{"x" , int32 (1 )}}).
1761
1762
SetUpdate (bson.D {{"$set" , bson.D {{"x" , int32 (1 )}}}}).
1762
1763
SetUpsert (true ),
1763
1764
)
1764
1765
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
+
1765
1786
mt .ClearEvents ()
1766
1787
res , err := mt .Coll .BulkWrite (mtest .Background , models )
1767
1788
assert .Nil (mt , err , "BulkWrite error: %v" , err )
1768
1789
1769
1790
mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
1770
1791
return evt .CommandName == "update"
1771
1792
})
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.
1773
1795
updates := len (mt .GetAllStartedEvents ())
1774
1796
assert .True (mt , updates > 1 , "expected multiple batches, got %v" , updates )
1775
1797
@@ -1778,29 +1800,11 @@ func TestCollection(t *testing.T) {
1778
1800
assert .Equal (mt , int64 (2 ), res .UpsertedCount , "expected %v upserted documents, got %v" , 2 , res .UpsertedCount )
1779
1801
assert .Equal (mt , 2 , len (res .UpsertedIDs ), "expected %v upserted ids, got %v" , 2 , len (res .UpsertedIDs ))
1780
1802
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 ]
1783
1805
assert .True (mt , ok , "expected id at key 0" )
1784
- id2 , ok : = res .UpsertedIDs [int64 (numModels - 1 )]
1806
+ _ , ok = res .UpsertedIDs [int64 (numModels - 1 )]
1785
1807
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 )
1804
1808
})
1805
1809
mt .RunOpts ("map hint" , noClientOpts , func (mt * mtest.T ) {
1806
1810
filter := bson.D {{"_id" , "foo" }}
0 commit comments