3
3
#include "TestSuite.h"
4
4
#include "test-libmongoc.h"
5
5
#include "mongoc-tests.h"
6
+ #include "mock-server.h"
7
+ #include "mongoc-client-private.h"
6
8
7
9
8
10
static void
@@ -391,6 +393,59 @@ test_get_collection_names (void)
391
393
mongoc_client_destroy (client );
392
394
}
393
395
396
+ static void
397
+ collection_names_handler (mock_server_t * server ,
398
+ mongoc_stream_t * stream ,
399
+ mongoc_rpc_t * rpc ,
400
+ void * user_data )
401
+ {
402
+ mongoc_stream_close (stream );
403
+ }
404
+
405
+ static void
406
+ test_get_collection_names_error (void )
407
+ {
408
+ mongoc_database_t * database ;
409
+ mongoc_client_t * client ;
410
+ mock_server_t * server ;
411
+ uint16_t port ;
412
+ bson_error_t error = { 0 };
413
+ bool success = false;
414
+ bson_t b = BSON_INITIALIZER ;
415
+ char * uristr ;
416
+ char * * names ;
417
+
418
+ port = (uint16_t )(20000 + (rand () % 1000 ));
419
+
420
+ server = mock_server_new ("127.0.0.1" , port , collection_names_handler ,
421
+ & success );
422
+ mock_server_set_wire_version (server , 0 , 3 ); /* listCollections command */
423
+ mock_server_run_in_thread (server );
424
+
425
+ usleep (5000 );
426
+
427
+ uristr = bson_strdup_printf ("mongodb://127.0.0.1:%hu/" , port );
428
+ client = mongoc_client_new (uristr );
429
+
430
+ if (!_mongoc_client_warm_up (client , & error )) {
431
+ assert (false);
432
+ }
433
+
434
+ database = mongoc_client_get_database (client , "test" );
435
+ suppress_one_message ();
436
+ suppress_one_message ();
437
+ names = mongoc_database_get_collection_names (database , & error );
438
+ assert (!names );
439
+ ASSERT_CMPINT (MONGOC_ERROR_STREAM , = = , error .domain );
440
+ ASSERT_CMPINT (MONGOC_ERROR_STREAM_SOCKET , = = , error .code );
441
+
442
+ mongoc_database_destroy (database );
443
+ mongoc_client_destroy (client );
444
+ mock_server_quit (server , 0 );
445
+ bson_destroy (& b );
446
+ bson_free (uristr );
447
+ }
448
+
394
449
void
395
450
test_database_install (TestSuite * suite )
396
451
{
@@ -402,4 +457,6 @@ test_database_install (TestSuite *suite)
402
457
test_get_collection_info );
403
458
TestSuite_Add (suite , "/Database/get_collection_names" ,
404
459
test_get_collection_names );
460
+ TestSuite_Add (suite , "/Database/get_collection_names_error" ,
461
+ test_get_collection_names_error );
405
462
}
0 commit comments