Skip to content

Commit efd15eb

Browse files
committed
CDRIVER-618 show collection names aren't NULL on err
1 parent db810ec commit efd15eb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/test-mongoc-database.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "TestSuite.h"
44
#include "test-libmongoc.h"
55
#include "mongoc-tests.h"
6+
#include "mock-server.h"
7+
#include "mongoc-client-private.h"
68

79

810
static void
@@ -391,6 +393,59 @@ test_get_collection_names (void)
391393
mongoc_client_destroy (client);
392394
}
393395

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+
394449
void
395450
test_database_install (TestSuite *suite)
396451
{
@@ -402,4 +457,6 @@ test_database_install (TestSuite *suite)
402457
test_get_collection_info);
403458
TestSuite_Add (suite, "/Database/get_collection_names",
404459
test_get_collection_names);
460+
TestSuite_Add (suite, "/Database/get_collection_names_error",
461+
test_get_collection_names_error);
405462
}

0 commit comments

Comments
 (0)