@@ -1528,7 +1528,7 @@ test_single_error_unordered_bulk ()
1528
1528
1529
1529
1530
1530
static void
1531
- _test_write_concern (bool has_write_commands , bool ordered )
1531
+ _test_write_concern (bool has_write_commands , bool ordered , bool multi_err )
1532
1532
{
1533
1533
mock_server_t * mock_server ;
1534
1534
mongoc_client_t * client ;
@@ -1539,6 +1539,8 @@ _test_write_concern (bool has_write_commands, bool ordered)
1539
1539
bson_error_t error ;
1540
1540
future_t * future ;
1541
1541
request_t * request ;
1542
+ int32_t first_err ;
1543
+ int32_t second_err ;
1542
1544
1543
1545
/* set wire protocol version for legacy writes or write commands */
1544
1546
mock_server = mock_server_with_autoismaster (has_write_commands ? 3 : 0 );
@@ -1550,6 +1552,7 @@ _test_write_concern (bool has_write_commands, bool ordered)
1550
1552
mongoc_write_concern_set_wtimeout (wc , 100 );
1551
1553
bulk = mongoc_collection_create_bulk_operation (collection , ordered , wc );
1552
1554
mongoc_bulk_operation_insert (bulk , tmp_bson ("{'_id': 1}" ));
1555
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'_id': 2}" ));
1553
1556
1554
1557
future = future_bulk_operation_execute (bulk , & reply , & error );
1555
1558
@@ -1565,43 +1568,109 @@ _test_write_concern (bool has_write_commands, bool ordered)
1565
1568
ordered ? "true" : "false" );
1566
1569
1567
1570
assert (request );
1568
- mock_server_replies (
1569
- request , 0 , 0 , 0 , 1 ,
1571
+ mock_server_replies_simple (
1572
+ request ,
1570
1573
"{'ok': 1.0, 'n': 1, "
1571
1574
" 'writeConcernError': {'code': 17, 'errmsg': 'foo'}}" );
1575
+
1576
+ request_destroy (request );
1577
+ request = mock_server_receives_command (
1578
+ mock_server ,
1579
+ "test" ,
1580
+ MONGOC_QUERY_NONE ,
1581
+ "{'delete': 'test',"
1582
+ " 'writeConcern': {'w': 2, 'wtimeout': 100},"
1583
+ " 'ordered': %s,"
1584
+ " 'deletes': [{'q': {'_id': 2}, 'limit': 0}]}" ,
1585
+ ordered ? "true" : "false" );
1586
+
1587
+ if (multi_err ) {
1588
+ mock_server_replies_simple (
1589
+ request ,
1590
+ "{'ok': 1.0, 'n': 1, "
1591
+ " 'writeConcernError': {'code': 42, 'errmsg': 'bar'}}" );
1592
+ } else {
1593
+ mock_server_replies_simple (request , "{'ok': 1.0, 'n': 1}" );
1594
+ }
1595
+
1596
+ request_destroy (request );
1597
+
1598
+ /* server fictionally returns 17 and 42; expect driver to use first one */
1599
+ first_err = 17 ;
1600
+ second_err = 42 ;
1572
1601
} else {
1573
1602
request = mock_server_receives_insert (
1574
1603
mock_server , "test.test" , MONGOC_INSERT_NONE , "{'_id': 1}" );
1575
1604
1576
1605
request_destroy (request );
1577
-
1578
1606
request = mock_server_receives_command (
1579
1607
mock_server ,
1580
1608
"test" ,
1581
1609
MONGOC_QUERY_NONE ,
1582
1610
"{'getLastError': 1, 'w': 2, 'wtimeout': 100}" );
1583
1611
1584
1612
assert (request );
1585
- mock_server_replies (
1586
- request , 0 , 0 , 0 , 1 ,
1587
- "{'ok': 1.0, 'n': 0, 'err': 'foo', 'wtimeout': true}" );
1613
+ mock_server_replies_simple (
1614
+ request , "{'ok': 1.0, 'n': 0, 'err': 'foo', 'wtimeout': true}" );
1615
+
1616
+ request = mock_server_receives_delete (
1617
+ mock_server , "test.test" , MONGOC_REMOVE_NONE , "{'_id': 1}" );
1618
+
1619
+ request_destroy (request );
1620
+ request = mock_server_receives_command (
1621
+ mock_server ,
1622
+ "test" ,
1623
+ MONGOC_QUERY_NONE ,
1624
+ "{'getLastError': 1, 'w': 2, 'wtimeout': 100}" );
1625
+
1626
+ if (multi_err ) {
1627
+ mock_server_replies_simple (
1628
+ request , "{'ok': 1.0, 'n': 0, 'err': 'bar', 'wtimeout': true}" );
1629
+ } else {
1630
+ mock_server_replies_simple (request , "{'ok': 1.0, 'n': 1}" );
1631
+ }
1632
+
1633
+ request_destroy (request );
1634
+
1635
+ /* The client makes up the error code for legacy writes */
1636
+ first_err = second_err = 64 ;
1588
1637
}
1589
1638
1590
1639
/* join thread, assert mongoc_bulk_operation_execute () returned 0 */
1591
1640
assert (!future_get_uint32_t (future ));
1592
1641
1593
- ASSERT_MATCH (& reply , "{'nInserted': 1,"
1594
- " 'nMatched': 0,"
1595
- " 'nRemoved': 0,"
1596
- " 'nUpserted': 0,"
1597
- " 'writeErrors': [],"
1598
- " 'writeConcernErrors': ["
1599
- " {'code': %d, 'errmsg': 'foo'}]}" ,
1600
- has_write_commands ? 17 : 64 );
1642
+ if (multi_err ) {
1643
+ ASSERT_MATCH (& reply ,
1644
+ "{'nInserted': 1,"
1645
+ " 'nMatched': 0,"
1646
+ " 'nRemoved': 1,"
1647
+ " 'nUpserted': 0,"
1648
+ " 'writeErrors': [],"
1649
+ " 'writeConcernErrors': ["
1650
+ " {'code': %d, 'errmsg': 'foo'},"
1651
+ " {'code': %d, 'errmsg': 'bar'}]}" ,
1652
+ first_err , second_err );
1653
+
1654
+ ASSERT_CMPSTR ("Multiple write concern errors: \"foo\", \"bar\"" ,
1655
+ error .message );
1656
+ } else {
1657
+ ASSERT_MATCH (& reply ,
1658
+ "{'nInserted': 1,"
1659
+ " 'nMatched': 0,"
1660
+ " 'nRemoved': 1,"
1661
+ " 'nUpserted': 0,"
1662
+ " 'writeErrors': [],"
1663
+ " 'writeConcernErrors': ["
1664
+ " {'code': %d, 'errmsg': 'foo'}]}" ,
1665
+ first_err );
1666
+ ASSERT_CMPSTR ("foo" , error .message );
1667
+ }
1601
1668
1602
1669
check_n_modified (has_write_commands , & reply , 0 );
1603
1670
1604
- request_destroy (request );
1671
+ ASSERT_CMPINT (MONGOC_ERROR_WRITE_CONCERN , = = , error .domain );
1672
+ ASSERT_CMPINT (first_err , = = , error .code );
1673
+
1605
1674
future_destroy (future );
1606
1675
bson_destroy (& reply );
1607
1676
mongoc_bulk_operation_destroy (bulk );
@@ -1630,14 +1699,28 @@ test_write_concern_legacy_unordered (void)
1630
1699
static void
1631
1700
test_write_concern_write_command_ordered (void )
1632
1701
{
1633
- _test_write_concern (true, true);
1702
+ _test_write_concern (true, true, false);
1703
+ }
1704
+
1705
+
1706
+ static void
1707
+ test_write_concern_write_command_ordered_multi_err (void )
1708
+ {
1709
+ _test_write_concern (true, true, true);
1634
1710
}
1635
1711
1636
1712
1637
1713
static void
1638
1714
test_write_concern_write_command_unordered (void )
1639
1715
{
1640
- _test_write_concern (true, false);
1716
+ _test_write_concern (true, false, false);
1717
+ }
1718
+
1719
+
1720
+ static void
1721
+ test_write_concern_write_command_unordered_multi_err (void )
1722
+ {
1723
+ _test_write_concern (true, false, true);
1641
1724
}
1642
1725
1643
1726
@@ -2549,8 +2632,12 @@ test_bulk_install (TestSuite *suite)
2549
2632
#endif
2550
2633
TestSuite_Add (suite , "/BulkOperation/write_concern/write_command/ordered" ,
2551
2634
test_write_concern_write_command_ordered );
2635
+ TestSuite_Add (suite , "/BulkOperation/write_concern/write_command/ordered/multi_err" ,
2636
+ test_write_concern_write_command_ordered_multi_err );
2552
2637
TestSuite_Add (suite , "/BulkOperation/write_concern/write_command/unordered" ,
2553
2638
test_write_concern_write_command_unordered );
2639
+ TestSuite_Add (suite , "/BulkOperation/write_concern/write_command/unordered/multi_err" ,
2640
+ test_write_concern_write_command_unordered_multi_err );
2554
2641
TestSuite_Add (suite , "/BulkOperation/multiple_error_unordered_bulk" ,
2555
2642
test_multiple_error_unordered_bulk );
2556
2643
#ifdef TODO_CDRIVER_707
0 commit comments