17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
19
using System . Threading ;
20
- using System . Threading . Tasks ;
21
20
using FluentAssertions ;
22
21
using MongoDB . Bson ;
23
22
using MongoDB . Bson . Serialization . Serializers ;
24
23
using MongoDB . Bson . TestHelpers . XunitExtensions ;
25
24
using MongoDB . Driver . Core . Bindings ;
26
- using MongoDB . Driver . Core . Clusters ;
27
- using MongoDB . Driver . Core . Configuration ;
28
25
using MongoDB . Driver . Core . Events ;
29
26
using MongoDB . Driver . Core . Misc ;
30
- using MongoDB . Driver . Core . TestHelpers ;
31
27
using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
32
28
using Xunit ;
33
29
@@ -1257,11 +1253,149 @@ public void Execute_with_update_should_send_session_id_when_supported(
1257
1253
VerifySessionIdWasSentWhenSupported ( subject , "update" , async) ;
1258
1254
}
1259
1255
1260
- private List < BsonDocument > ReadAllFromCollection ( IReadBinding binding )
1256
+ [ SkippableTheory ]
1257
+ [ InlineData ( new [ ] { 1 } , new [ ] { 1 } ) ]
1258
+ [ InlineData ( new [ ] { 1 , 1 } , new [ ] { 2 } ) ]
1259
+ [ InlineData ( new [ ] { 8388605 , 8388605 } , new [ ] { 2 } ) ]
1260
+ [ InlineData ( new [ ] { 8388605 , 8388606 } , new [ ] { 1 , 1 } ) ]
1261
+ [ InlineData ( new [ ] { 16777216 } , new [ ] { 1 } ) ]
1262
+ [ InlineData ( new [ ] { 16777216 , 1 } , new [ ] { 1 , 1 } ) ]
1263
+ [ InlineData ( new [ ] { 16777216 , 16777216 } , new [ ] { 1 , 1 } ) ]
1264
+ public void Execute_with_multiple_deletes_should_split_batches_as_expected_when_using_write_commands_via_opquery ( int [ ] requestSizes , int [ ] expectedBatchCounts )
1261
1265
{
1262
- var operation = new FindOperation < BsonDocument > ( _collectionNamespace , BsonDocumentSerializer . Instance , _messageEncoderSettings ) ;
1263
- var cursor = ExecuteOperation ( operation , binding , false ) ;
1264
- return ReadCursorToEnd ( cursor ) ;
1266
+ RequireServer . Check ( ) . Supports ( Feature . WriteCommands ) ;
1267
+ DropCollection ( ) ;
1268
+
1269
+ using ( EventContext . BeginOperation ( ) )
1270
+ {
1271
+ var eventCapturer = new EventCapturer ( ) . Capture < CommandStartedEvent > ( e => e . CommandName == "delete" && e . OperationId == EventContext . OperationId ) ;
1272
+ using ( var cluster = CoreTestConfiguration . CreateCluster ( b => b . Subscribe ( eventCapturer ) ) )
1273
+ using ( var session = NoCoreSession . NewHandle ( ) )
1274
+ using ( var binding = new ReadWriteBindingHandle ( new WritableServerBinding ( cluster , session . Fork ( ) ) ) )
1275
+ {
1276
+ var requests = requestSizes . Select ( size => CreateDeleteRequest ( size ) ) ;
1277
+ var operation = new BulkMixedWriteOperation ( _collectionNamespace , requests , _messageEncoderSettings ) ;
1278
+
1279
+ var result = ExecuteOperation ( operation , binding , async: false ) ;
1280
+
1281
+ result . RequestCount . Should ( ) . Be ( requestSizes . Length ) ;
1282
+ var commandStartedEvents = eventCapturer . Events . OfType < CommandStartedEvent > ( ) . ToList ( ) ;
1283
+ var actualBatchCounts = commandStartedEvents . Select ( e => e . Command [ "deletes" ] . AsBsonArray . Count ) . ToList ( ) ;
1284
+ actualBatchCounts . Should ( ) . Equal ( expectedBatchCounts ) ;
1285
+ }
1286
+ }
1287
+ }
1288
+
1289
+ [ SkippableTheory ]
1290
+ [ InlineData ( new [ ] { 1 } , new [ ] { 1 } ) ]
1291
+ [ InlineData ( new [ ] { 1 , 1 } , new [ ] { 2 } ) ]
1292
+ [ InlineData ( new [ ] { 8388605 , 8388605 } , new [ ] { 2 } ) ]
1293
+ [ InlineData ( new [ ] { 8388605 , 8388606 } , new [ ] { 1 , 1 } ) ]
1294
+ [ InlineData ( new [ ] { 16777216 } , new [ ] { 1 } ) ]
1295
+ [ InlineData ( new [ ] { 16777216 , 1 } , new [ ] { 1 , 1 } ) ]
1296
+ [ InlineData ( new [ ] { 16777216 , 16777216 } , new [ ] { 1 , 1 } ) ]
1297
+ public void Execute_with_multiple_inserts_should_split_batches_as_expected_when_using_write_commands_via_opquery ( int [ ] documentSizes , int [ ] expectedBatchCounts )
1298
+ {
1299
+ RequireServer . Check ( ) . Supports ( Feature . WriteCommands ) ;
1300
+ DropCollection ( ) ;
1301
+
1302
+ using ( EventContext . BeginOperation ( ) )
1303
+ {
1304
+ var eventCapturer = new EventCapturer ( ) . Capture < CommandStartedEvent > ( e => e . CommandName == "insert" && e . OperationId == EventContext . OperationId ) ;
1305
+ using ( var cluster = CoreTestConfiguration . CreateCluster ( b => b . Subscribe ( eventCapturer ) ) )
1306
+ using ( var session = NoCoreSession . NewHandle ( ) )
1307
+ using ( var binding = new ReadWriteBindingHandle ( new WritableServerBinding ( cluster , session . Fork ( ) ) ) )
1308
+ {
1309
+ var documents = documentSizes . Select ( ( size , index ) => CreateDocument ( index + 1 , size ) ) . ToList ( ) ;
1310
+ var requests = documents . Select ( d => new InsertRequest ( d ) ) . ToList ( ) ;
1311
+ var operation = new BulkMixedWriteOperation ( _collectionNamespace , requests , _messageEncoderSettings ) ;
1312
+
1313
+ var result = ExecuteOperation ( operation , binding , async: false ) ;
1314
+
1315
+ result . InsertedCount . Should ( ) . Be ( documents . Count ) ;
1316
+ var commandStartedEvents = eventCapturer . Events . OfType < CommandStartedEvent > ( ) . ToList ( ) ;
1317
+ var actualBatchCounts = commandStartedEvents . Select ( e => e . Command [ "documents" ] . AsBsonArray . Count ) . ToList ( ) ;
1318
+ actualBatchCounts . Should ( ) . Equal ( expectedBatchCounts ) ;
1319
+ }
1320
+ }
1321
+ }
1322
+
1323
+ [ SkippableTheory ]
1324
+ [ InlineData ( new [ ] { 1 } , new [ ] { 1 } ) ]
1325
+ [ InlineData ( new [ ] { 1 , 1 } , new [ ] { 2 } ) ]
1326
+ [ InlineData ( new [ ] { 8388605 , 8388605 } , new [ ] { 2 } ) ]
1327
+ [ InlineData ( new [ ] { 8388605 , 8388606 } , new [ ] { 1 , 1 } ) ]
1328
+ [ InlineData ( new [ ] { 16777216 } , new [ ] { 1 } ) ]
1329
+ [ InlineData ( new [ ] { 16777216 , 1 } , new [ ] { 1 , 1 } ) ]
1330
+ [ InlineData ( new [ ] { 16777216 , 16777216 } , new [ ] { 1 , 1 } ) ]
1331
+ public void Execute_with_multiple_updates_should_split_batches_as_expected_when_using_write_commands_via_opquery ( int [ ] requestSizes , int [ ] expectedBatchCounts )
1332
+ {
1333
+ RequireServer . Check ( ) . Supports ( Feature . WriteCommands ) ;
1334
+ DropCollection ( ) ;
1335
+
1336
+ using ( EventContext . BeginOperation ( ) )
1337
+ {
1338
+ var eventCapturer = new EventCapturer ( ) . Capture < CommandStartedEvent > ( e => e . CommandName == "update" && e . OperationId == EventContext . OperationId ) ;
1339
+ using ( var cluster = CoreTestConfiguration . CreateCluster ( b => b . Subscribe ( eventCapturer ) ) )
1340
+ using ( var session = NoCoreSession . NewHandle ( ) )
1341
+ using ( var binding = new ReadWriteBindingHandle ( new WritableServerBinding ( cluster , session . Fork ( ) ) ) )
1342
+ {
1343
+ var requests = requestSizes . Select ( size => CreateUpdateRequest ( size ) ) ;
1344
+ var operation = new BulkMixedWriteOperation ( _collectionNamespace , requests , _messageEncoderSettings ) ;
1345
+
1346
+ var result = ExecuteOperation ( operation , binding , async: false ) ;
1347
+
1348
+ result . RequestCount . Should ( ) . Be ( requestSizes . Length ) ;
1349
+ var commandStartedEvents = eventCapturer . Events . OfType < CommandStartedEvent > ( ) . ToList ( ) ;
1350
+ var actualBatchCounts = commandStartedEvents . Select ( e => e . Command [ "updates" ] . AsBsonArray . Count ) . ToList ( ) ;
1351
+ actualBatchCounts . Should ( ) . Equal ( expectedBatchCounts ) ;
1352
+ }
1353
+ }
1354
+ }
1355
+
1356
+ // private methods
1357
+ private DeleteRequest CreateDeleteRequest ( int size )
1358
+ {
1359
+ var filter = new BsonDocument ( "filler" , "" ) ;
1360
+ var requestDocument = new BsonDocument
1361
+ {
1362
+ { "q" , filter } ,
1363
+ { "limit" , 1 }
1364
+ } ;
1365
+ var fillerSize = size - requestDocument . ToBson ( ) . Length ;
1366
+ if ( fillerSize > 0 )
1367
+ {
1368
+ filter [ "filler" ] = new string ( 'x' , fillerSize ) ;
1369
+ }
1370
+ return new DeleteRequest ( filter ) ;
1371
+ }
1372
+
1373
+ private BsonDocument CreateDocument ( int id , int size )
1374
+ {
1375
+ var document = new BsonDocument { { "_id" , id } , { "filler" , "" } } ;
1376
+ var fillerSize = size - document . ToBson ( ) . Length ;
1377
+ if ( fillerSize > 0 )
1378
+ {
1379
+ document [ "filler" ] = new string ( 'x' , fillerSize ) ;
1380
+ }
1381
+ return document ;
1382
+ }
1383
+
1384
+ private UpdateRequest CreateUpdateRequest ( int size )
1385
+ {
1386
+ var filter = new BsonDocument ( "filler" , "" ) ;
1387
+ var update = new BsonDocument ( "$set" , new BsonDocument ( "x" , 1 ) ) ;
1388
+ var requestDocument = new BsonDocument
1389
+ {
1390
+ { "q" , filter } ,
1391
+ { "u" , update }
1392
+ } ;
1393
+ var fillerSize = size - requestDocument . ToBson ( ) . Length ;
1394
+ if ( fillerSize > 0 )
1395
+ {
1396
+ filter [ "filler" ] = new string ( 'x' , fillerSize ) ;
1397
+ }
1398
+ return new UpdateRequest ( UpdateType . Update , filter , update ) ;
1265
1399
}
1266
1400
1267
1401
private void EnsureTestData ( )
@@ -1275,5 +1409,12 @@ private void EnsureTestData()
1275
1409
BsonDocument . Parse ( "{_id: 5, x: 2 }" ) ,
1276
1410
BsonDocument . Parse ( "{_id: 6, x: 3 }" ) ) ;
1277
1411
}
1412
+
1413
+ private List < BsonDocument > ReadAllFromCollection ( IReadBinding binding )
1414
+ {
1415
+ var operation = new FindOperation < BsonDocument > ( _collectionNamespace , BsonDocumentSerializer . Instance , _messageEncoderSettings ) ;
1416
+ var cursor = ExecuteOperation ( operation , binding , false ) ;
1417
+ return ReadCursorToEnd ( cursor ) ;
1418
+ }
1278
1419
}
1279
1420
}
0 commit comments