@@ -9,10 +9,13 @@ const {
9
9
const test = require ( './shared' ) . assert ;
10
10
const { MongoError } = require ( '../../src/error' ) ;
11
11
const { Long } = require ( '../../src' ) ;
12
+ const crypto = require ( 'crypto' ) ;
12
13
const chai = require ( 'chai' ) ;
13
14
const expect = chai . expect ;
14
15
chai . use ( require ( 'chai-subset' ) ) ;
15
16
17
+ const MAX_BSON_SIZE = 16777216 ;
18
+
16
19
describe ( 'Bulk' , function ( ) {
17
20
before ( function ( ) {
18
21
return setupDatabase ( this . configuration ) ;
@@ -1627,6 +1630,54 @@ describe('Bulk', function () {
1627
1630
} ) ;
1628
1631
} ) ;
1629
1632
1633
+ it ( 'properly accounts for bson size in bytes in bulk ordered inserts' , function ( ) {
1634
+ const client = this . configuration . newClient ( ) ;
1635
+ const size = MAX_BSON_SIZE / 2 ;
1636
+ const largeString = crypto . randomBytes ( size - 100 ) . toString ( 'hex' ) ;
1637
+ const documents = [ { s : largeString } , { s : largeString } ] ;
1638
+
1639
+ let db ;
1640
+
1641
+ return client
1642
+ . connect ( )
1643
+ . then ( ( ) => {
1644
+ db = client . db ( this . configuration . db ) ;
1645
+ return db . dropCollection ( 'doesnt_matter' ) . catch ( ( ) => { } ) ;
1646
+ } )
1647
+ . then ( ( ) => {
1648
+ return db . createCollection ( 'doesnt_matter' ) ;
1649
+ } )
1650
+ . then ( ( ) => {
1651
+ const coll = db . collection ( 'doesnt_matter' ) ;
1652
+ coll . insertMany ( documents , { ordered : true } ) ;
1653
+ } )
1654
+ . finally ( ( ) => client . close ( ) ) ;
1655
+ } ) ;
1656
+
1657
+ it ( 'properly accounts for bson size in bytes in bulk unordered inserts' , function ( ) {
1658
+ const client = this . configuration . newClient ( ) ;
1659
+ const size = MAX_BSON_SIZE / 2 ;
1660
+ const largeString = crypto . randomBytes ( size - 100 ) . toString ( 'hex' ) ;
1661
+ const documents = [ { s : largeString } , { s : largeString } ] ;
1662
+
1663
+ let db ;
1664
+
1665
+ return client
1666
+ . connect ( )
1667
+ . then ( ( ) => {
1668
+ db = client . db ( this . configuration . db ) ;
1669
+ return db . dropCollection ( 'doesnt_matter' ) . catch ( ( ) => { } ) ;
1670
+ } )
1671
+ . then ( ( ) => {
1672
+ return db . createCollection ( 'doesnt_matter' ) ;
1673
+ } )
1674
+ . then ( ( ) => {
1675
+ const coll = db . collection ( 'doesnt_matter' ) ;
1676
+ coll . insertMany ( documents , { ordered : false } ) ;
1677
+ } )
1678
+ . finally ( ( ) => client . close ( ) ) ;
1679
+ } ) ;
1680
+
1630
1681
function testPropagationOfBulkWriteError ( bulk ) {
1631
1682
return bulk . execute ( ) . then (
1632
1683
err => {
0 commit comments