void future_pool_test (bool verbose) { printf (" * future_pool: "); // @selftest future_pool_t *pool = future_pool_new (4); test_args_t args1 = {5, 3}; test_args_t args2 = {10, 2}; future_t *future1 = future_pool_execute ( pool, test_future_fn, (void *) &args1); future_t *future2 = future_pool_execute ( pool, test_future_fn, (void *) &args2); assert ((long) future_get (future1) == 15); assert ((long) future_get (future2) == 20); future_destroy (&future1); future_destroy (&future2); future_pool_destroy (&pool); // @end printf ("OK\n"); }
static void test_find_and_modify_opts (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; mongoc_find_and_modify_opts_t *opts; future_t *future; request_t *request; server = mock_server_with_autoismaster (0); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); opts = mongoc_find_and_modify_opts_new (); assert (mongoc_find_and_modify_opts_set_max_time_ms (opts, 42)); assert (mongoc_find_and_modify_opts_append (opts, tmp_bson ("{'foo': 1}"))); future = future_collection_find_and_modify_with_opts ( collection, tmp_bson ("{}"), opts, NULL, &error); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_NONE, "{'findAndModify': 'collection', 'maxTimeMS': 42, 'foo': 1}"); mock_server_replies_ok_and_destroys (request); ASSERT_OR_PRINT (future_get_bool (future), error); future_destroy (future); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void _test_find_command (const mongoc_uri_t *uri, mock_server_t *server, const char *query_in, mongoc_read_prefs_t *read_prefs, mongoc_query_flags_t expected_find_cmd_query_flags, const char *expected_find_cmd) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); mongoc_collection_set_read_prefs (collection, read_prefs); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 1, 0, tmp_bson (query_in), NULL, read_prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "test", expected_find_cmd_query_flags, expected_find_cmd); mock_server_replies (request, MONGOC_REPLY_NONE, /* flags */ 0, /* cursorId */ 0, /* startingFrom */ 1, /* numberReturned */ "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"); /* mongoc_cursor_next returned true */ assert (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
/* direct connection to a secondary requires read pref primaryPreferred to * avoid "not master" error from server */ static void _test_op_msg_direct_connection (bool is_mongos, test_op_msg_direct_fn_t fn, const char *expected_cmd) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_read_prefs_t *prefs = NULL; mongoc_cursor_t *cursor; const bson_t *doc; bson_t *cmd; future_t *future; request_t *request; const char *reply; int i; if (is_mongos) { server = mock_mongos_new (WIRE_VERSION_OP_MSG); } else { server = mock_server_with_autoismaster (WIRE_VERSION_OP_MSG); } mock_server_auto_endsessions (server); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); for (i = 0; i < 2; i++) { if (i == 1) { /* user-supplied read preference primary makes no difference */ prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); } cursor = fn (collection, prefs); future = future_cursor_next (cursor, &doc); cmd = tmp_bson (expected_cmd); request = mock_server_receives_msg (server, 0, cmd); reply = "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"; mock_server_replies_simple (request, reply); BSON_ASSERT (future_get_bool (future)); future_destroy (future); request_destroy (request); mongoc_cursor_destroy (cursor); mongoc_read_prefs_destroy (prefs); /* null ok */ } mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
/* test that we add readConcern only inside $query, not outside it too */ static void test_mongos_read_concern (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_read_prefs_t *prefs; mongoc_cursor_t *cursor; const bson_t *doc; future_t *future; request_t *request; server = mock_mongos_new (WIRE_VERSION_READ_CONCERN); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "test", "test"); prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY); cursor = mongoc_collection_find_with_opts ( collection, tmp_bson ("{'a': 1}"), tmp_bson ("{'readConcern': {'level': 'foo'}}"), prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "test", MONGOC_QUERY_SLAVE_OK, "{" " '$query': {" " 'find': 'test', 'filter': {}, 'readConcern': {'level': 'foo'}" " }," " '$readPreference': {" " 'mode': 'secondary'" " }," " 'readConcern': {'$exists': false}" "}"); mock_server_replies_to_find ( request, MONGOC_QUERY_SLAVE_OK, 0, 1, "db.collection", "{}", true); /* mongoc_cursor_next returned true */ BSON_ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_read_prefs_destroy (prefs); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
void promise_test (bool verbose) { printf (" * promise: "); // @selftest // Test promise is fulfilled. pthread_t thread1; promise_t *promise1 = promise_new (); future_t *future1 = promise_get_future (promise1); pthread_create (&thread1, NULL, test_fulfill_thread, (void *) promise1); char *value = (char *) future_get (future1); assert (streq (value, "value")); free (value); pthread_join (thread1, NULL); promise_destroy (&promise1); future_destroy (&future1); // Test promise is rejected. pthread_t thread2; promise_t *promise2 = promise_new (); future_t *future2 = promise_get_future (promise2); pthread_create (&thread2, NULL, test_reject_thread, (void *) promise2); future_wait (future2); assert (future_is_resolved (future2)); assert (future_is_fulfilled (future2) == false); assert (future_rejected_code (future2) == -1); pthread_join (thread2, NULL); promise_destroy (&promise2); future_destroy (&future2); // @end printf ("OK\n"); }
static void _test_op_query (const mongoc_uri_t *uri, mock_server_t *server, const char *query_in, mongoc_read_prefs_t *read_prefs, mongoc_query_flags_t expected_query_flags, const char *expected_query) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 1, 0, tmp_bson (query_in), NULL, read_prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_query ( server, "test.test", expected_query_flags, 0, 1, expected_query, NULL); mock_server_replies (request, MONGOC_REPLY_NONE, /* flags */ 0, /* cursorId */ 0, /* startingFrom */ 1, /* numberReturned */ "{'a': 1}"); /* mongoc_cursor_next returned true */ BSON_ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
static void cleanup_future (future_t *future) { future_value_t v; BSON_ASSERT (future_wait (future)); v = future->return_value; if (v.type == future_value_mongoc_change_stream_ptr_type) { mongoc_change_stream_destroy (v.value.mongoc_change_stream_ptr_value); } else if (v.type == future_value_char_ptr_ptr_type) { bson_strfreev (v.value.char_ptr_ptr_value); } future_destroy (future); }
static void _test_command_simple (const mongoc_uri_t *uri, mock_server_t *server, const char *command, mongoc_read_prefs_t *read_prefs, mongoc_query_flags_t expected_query_flags, const char *expected_query) { mongoc_client_t *client; mongoc_collection_t *collection; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); mongoc_collection_set_read_prefs (collection, read_prefs); future = future_client_command_simple (client, "test", tmp_bson (command), read_prefs, NULL, NULL); request = mock_server_receives_command ( server, "test", expected_query_flags, expected_query); mock_server_replies (request, MONGOC_REPLY_NONE, /* flags */ 0, /* cursorId */ 0, /* startingFrom */ 1, /* numberReturned */ "{'ok': 1}"); /* mongoc_cursor_next returned true */ assert (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
static void _test_op_msg (const mongoc_uri_t *uri, mock_server_t *server, const char *query_in, mongoc_read_prefs_t *read_prefs, const char *expected_op_msg) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; const bson_t *doc; bson_t b = BSON_INITIALIZER; future_t *future; request_t *request; client = mongoc_client_new_from_uri (uri); collection = mongoc_client_get_collection (client, "test", "test"); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 1, 0, tmp_bson (query_in), NULL, read_prefs); future = future_cursor_next (cursor, &doc); request = mock_server_receives_msg (server, 0, tmp_bson (expected_op_msg)); mock_server_replies_simple (request, "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': [{'a': 1}]}}"); /* mongoc_cursor_next returned true */ BSON_ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&b); }
static void test_exhaust (void) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; request_t *request; future_t *future; const bson_t *doc; bson_error_t error; server = mock_server_with_autoismaster (WIRE_VERSION_FIND_CMD); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); cursor = mongoc_collection_find_with_opts (collection, tmp_bson (NULL), NULL, tmp_bson ("{'exhaust': true}")); future = future_cursor_next (cursor, &doc); /* Find, getMore and killCursors commands spec: "The find command does not * support the exhaust flag from OP_QUERY. Drivers that support exhaust MUST * fallback to existing OP_QUERY wire protocol messages." */ request = mock_server_receives_request (server); mock_server_replies_to_find ( request, MONGOC_QUERY_SLAVE_OK | MONGOC_QUERY_EXHAUST, 0, 0, "db.collection", "{}", false /* is_command */); ASSERT (future_get_bool (future)); ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void _test_collection_op_query_or_find_command ( test_collection_find_with_opts_t *test_data, check_request_fn_t check_request_fn, const char *reply_json, int32_t max_wire_version) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; future_t *future; request_t *request; const bson_t *doc; server = mock_server_with_autoismaster (max_wire_version); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); cursor = mongoc_collection_find_with_opts (collection, test_data->filter_bson, test_data->read_prefs, test_data->opts_bson); ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error); future = future_cursor_next (cursor, &doc); request = check_request_fn (server, test_data); ASSERT (request); mock_server_replies_simple (request, reply_json); ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void test_find_and_modify_bypass (bool bypass) { mongoc_collection_t *collection; mongoc_client_t *client; mock_server_t *server; request_t *request; future_t *future; bson_error_t error; bson_t *update; bson_t doc = BSON_INITIALIZER; bson_t reply; mongoc_find_and_modify_opts_t *opts; server = mock_server_new (); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); ASSERT (client); collection = mongoc_client_get_collection (client, "test", "test_find_and_modify"); auto_ismaster (server, WIRE_VERSION_FAM_WRITE_CONCERN, /* max_wire_version */ 48000000, /* max_message_size */ 16777216, /* max_bson_size */ 1000); /* max_write_batch_size */ BSON_APPEND_INT32 (&doc, "superduper", 77889); update = BCON_NEW ("$set", "{", "superduper", BCON_INT32 (1234), "}"); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_bypass_document_validation (opts, bypass); mongoc_find_and_modify_opts_set_update (opts, update); mongoc_find_and_modify_opts_set_flags (opts, MONGOC_FIND_AND_MODIFY_RETURN_NEW); future = future_collection_find_and_modify_with_opts ( collection, &doc, opts, &reply, &error); if (bypass) { request = mock_server_receives_command ( server, "test", MONGOC_QUERY_NONE, "{ 'findAndModify' : 'test_find_and_modify', " "'query' : { 'superduper' : 77889 }," "'update' : { '$set' : { 'superduper' : 1234 } }," "'new' : true," "'bypassDocumentValidation' : true }"); } else { request = mock_server_receives_command ( server, "test", MONGOC_QUERY_NONE, "{ 'findAndModify' : 'test_find_and_modify', " "'query' : { 'superduper' : 77889 }," "'update' : { '$set' : { 'superduper' : 1234 } }," "'new' : true," "'bypassDocumentValidation' : false }"); } mock_server_replies_simple (request, "{ 'value' : null, 'ok' : 1 }"); ASSERT_OR_PRINT (future_get_bool (future), error); future_destroy (future); mongoc_find_and_modify_opts_destroy (opts); bson_destroy (&reply); bson_destroy (update); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); bson_destroy (&doc); }
static void test_find_and_modify_collation (int wire) { mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; bson_error_t error; mongoc_find_and_modify_opts_t *opts; future_t *future; request_t *request; bson_t *collation; server = mock_server_with_autoismaster (wire); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); collation = BCON_NEW ("collation", "{", "locale", BCON_UTF8 ("en_US"), "caseFirst", BCON_UTF8 ("lower"), "}"); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_append (opts, collation); if (wire >= WIRE_VERSION_COLLATION) { future = future_collection_find_and_modify_with_opts ( collection, tmp_bson ("{}"), opts, NULL, &error); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_NONE, "{'findAndModify': 'collection'," " 'collation': { 'locale': 'en_US', 'caseFirst': 'lower'}" "}"); mock_server_replies_ok_and_destroys (request); ASSERT_OR_PRINT (future_get_bool (future), error); future_destroy (future); } else { bool ok = mongoc_collection_find_and_modify_with_opts ( collection, tmp_bson ("{}"), opts, NULL, &error); ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION, "The selected server does not support collation"); ASSERT (!ok); } bson_destroy (collation); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void test_getmore_cmd_await (void) { bson_t *opts; mock_server_t *server; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; future_t *future; request_t *request; const bson_t *doc; opts = tmp_bson ("{'tailable': true," " 'awaitData': true," " 'maxAwaitTimeMS': {'$numberLong': '9999'}}"); /* * "find" command */ server = mock_server_with_autoismaster (WIRE_VERSION_FIND_CMD); mock_server_run (server); client = mongoc_client_new_from_uri (mock_server_get_uri (server)); collection = mongoc_client_get_collection (client, "db", "collection"); cursor = mongoc_collection_find_with_opts (collection, tmp_bson (NULL), NULL, opts); future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_SLAVE_OK, "{'find': 'collection', 'filter': {}}"); ASSERT (request); mock_server_replies_simple (request, "{'ok': 1," " 'cursor': {" " 'id': {'$numberLong': '123'}," " 'ns': 'db.collection'," " 'firstBatch': [{}]}}"); ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); /* * "getMore" command */ future = future_cursor_next (cursor, &doc); request = mock_server_receives_command ( server, "db", MONGOC_QUERY_SLAVE_OK, "{'getMore': {'$numberLong': '123'}," " 'collection': 'collection'," " 'maxTimeMS': {'$numberLong': '9999'}}}"); ASSERT (request); mock_server_replies_simple (request, "{'ok': 1," " 'cursor': {" " 'id': {'$numberLong': '0'}," " 'ns': 'db.collection'," " 'nextBatch': [{}]}}"); ASSERT (future_get_bool (future)); request_destroy (request); future_destroy (future); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mock_server_destroy (server); }
static void test_func_inherits_opts (void *ctx) { opt_inheritance_test_t *test = (opt_inheritance_test_t *) ctx; /* for example, test mongoc_collection_find_with_opts with no read pref, * with a read pref set on the collection (OPT_SOURCE_COLL), with an explicit * read pref (OPT_SOURCE_FUNC), or with one read pref on the collection and * a different one passed explicitly */ opt_source_t source_matrix[] = {OPT_SOURCE_NONE, test->opt_source, OPT_SOURCE_FUNC, test->opt_source | OPT_SOURCE_FUNC}; size_t i; func_ctx_t func_ctx; mock_rs_t *rs; mongoc_client_t *client; mongoc_database_t *db; mongoc_collection_t *collection; bson_t opts = BSON_INITIALIZER; mongoc_read_prefs_t *func_prefs = NULL; future_t *future; request_t *request; bson_t cmd = BSON_INITIALIZER; bool expect_secondary; bson_error_t error; /* one primary, one secondary */ rs = mock_rs_with_autoismaster (WIRE_VERSION_OP_MSG, true, 1, 0); /* we use read pref tags like "collection": "yes" to verify where the * pref was inherited from; ensure all secondaries match all tags */ mock_rs_tag_secondary (rs, 0, tmp_bson ("{'client': 'yes'," " 'database': 'yes'," " 'collection': 'yes'," " 'function': 'yes'}")); mock_rs_run (rs); /* iterate over all combinations of options sources: e.g., an option set on * collection and not function, on function not collection, both, neither */ for (i = 0; i < sizeof (source_matrix) / (sizeof (opt_source_t)); i++) { expect_secondary = false; func_prefs = NULL; bson_reinit (&cmd); bson_reinit (&opts); client = mongoc_client_new_from_uri (mock_rs_get_uri (rs)); if (source_matrix[i] & OPT_SOURCE_CLIENT) { set_client_opt (client, test->opt_type); } db = mongoc_client_get_database (client, "database"); if (source_matrix[i] & OPT_SOURCE_DB) { set_database_opt (db, test->opt_type); } collection = mongoc_database_get_collection (db, "collection"); if (source_matrix[i] & OPT_SOURCE_COLL) { set_collection_opt (collection, test->opt_type); } if (source_matrix[i] & OPT_SOURCE_FUNC) { set_func_opt (&opts, &func_prefs, test->opt_type); } func_ctx_init ( &func_ctx, test, client, db, collection, func_prefs, &opts); /* func_with_opts creates expected "cmd", like {insert: 'collection'} */ future = test->func_with_opts (&func_ctx, &cmd); if (source_matrix[i] != OPT_SOURCE_NONE) { add_expected_opt (source_matrix[i], test->opt_type, &cmd); if (test->opt_type == OPT_READ_PREFS) { expect_secondary = true; } } /* write commands send two OP_MSG sections */ if (test->n_sections == 2) { request = mock_rs_receives_msg (rs, 0, &cmd, tmp_bson ("{}")); } else { request = mock_rs_receives_msg (rs, 0, &cmd); } if (expect_secondary) { BSON_ASSERT (mock_rs_request_is_to_secondary (rs, request)); } else { BSON_ASSERT (mock_rs_request_is_to_primary (rs, request)); } if (func_ctx.cursor) { mock_server_replies_simple (request, "{'ok': 1," " 'cursor': {" " 'id': 0," " 'ns': 'db.collection'," " 'firstBatch': []}}"); BSON_ASSERT (!future_get_bool (future)); future_destroy (future); ASSERT_OR_PRINT (!mongoc_cursor_error (func_ctx.cursor, &error), error); } else { mock_server_replies_simple (request, "{'ok': 1}"); cleanup_future (future); } request_destroy (request); mongoc_read_prefs_destroy (func_prefs); func_ctx_cleanup (&func_ctx); mongoc_collection_destroy (collection); mongoc_database_destroy (db); mongoc_client_destroy (client); } bson_destroy (&cmd); bson_destroy (&opts); mock_rs_destroy (rs); }
static void _test_heartbeat_events (bool pooled, bool succeeded) { context_t context; mock_server_t *server; mongoc_uri_t *uri; mongoc_client_t *client; mongoc_client_pool_t *pool = NULL; future_t *future; request_t *request; char *expected_json; bson_error_t error; context_init (&context); /* auto-respond to "foo" command */ server = mock_server_new (); mock_server_run (server); mock_server_autoresponds (server, responder, NULL, NULL); uri = mongoc_uri_copy (mock_server_get_uri (server)); mongoc_uri_set_option_as_int32 (uri, "serverSelectionTimeoutMS", 400); if (pooled) { pool = mongoc_client_pool_new (uri); pool_set_heartbeat_event_callbacks (pool, &context); client = mongoc_client_pool_pop (pool); } else { client = mongoc_client_new_from_uri (uri); client_set_heartbeat_event_callbacks (client, &context); } /* trigger "ismaster" handshake */ future = future_client_command_simple (client, "admin", tmp_bson ("{'foo': 1}"), NULL, NULL, &error); /* topology scanner calls ismaster once */ request = mock_server_receives_ismaster (server); if (succeeded) { mock_server_replies_ok_and_destroys (request); } else { mock_server_hangs_up (request); request_destroy (request); } /* pooled client opens new socket, handshakes it by calling ismaster again */ if (pooled && succeeded) { request = mock_server_receives_ismaster (server); mock_server_replies_ok_and_destroys (request); } if (succeeded) { /* "foo" command succeeds */ ASSERT_OR_PRINT (future_get_bool (future), error); } else { ASSERT (!future_get_bool (future)); } if (pooled) { mongoc_client_pool_push (pool, client); mongoc_client_pool_destroy (pool); } else { mongoc_client_destroy (client); } /* even if pooled, only topology scanner sends events, so we get one pair */ if (succeeded) { expected_json = bson_strdup_printf ( "{'0': {'heartbeat_started_event': {'host': '%s'}}," " '1': {'heartbeat_succeeded_event': {'host': '%s'}}}", mock_server_get_host_and_port (server), mock_server_get_host_and_port (server)); } else { expected_json = bson_strdup_printf ( "{'0': {'heartbeat_started_event': {'host': '%s'}}," " '1': {'heartbeat_failed_event': {'host': '%s'}}}", mock_server_get_host_and_port (server), mock_server_get_host_and_port (server)); } check_json_apm_events (&context.events, tmp_bson (expected_json)); future_destroy (future); bson_free (expected_json); mongoc_uri_destroy (uri); mock_server_destroy (server); context_destroy (&context); }
/* * Append to the platform field a huge string * Make sure that it gets truncated */ static void test_mongoc_metadata_too_big (void) { mongoc_client_t *client; mock_server_t *server; mongoc_uri_t *uri; future_t *future; request_t *request; const bson_t *ismaster_doc; bson_iter_t iter; enum { BUFFER_SIZE = METADATA_MAX_SIZE }; char big_string[BUFFER_SIZE]; uint32_t len; const uint8_t *dummy; server = mock_server_new (); mock_server_run (server); _reset_metadata (); memset (big_string, 'a', BUFFER_SIZE - 1); big_string[BUFFER_SIZE - 1] = '\0'; ASSERT (mongoc_metadata_append (NULL, NULL, big_string)); uri = mongoc_uri_copy (mock_server_get_uri (server)); client = mongoc_client_new_from_uri (uri); ASSERT (mongoc_client_set_appname (client, "my app")); /* Send a ping, mock server deals with it */ future = future_client_command_simple (client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, NULL); request = mock_server_receives_ismaster (server); /* Make sure the isMaster request has a metadata field, and it's not huge */ ASSERT (request); ismaster_doc = request_get_doc (request, 0); ASSERT (ismaster_doc); ASSERT (bson_has_field (ismaster_doc, "isMaster")); ASSERT (bson_has_field (ismaster_doc, METADATA_FIELD)); /* isMaster with metadata isn't too big */ bson_iter_init_find (&iter, ismaster_doc, METADATA_FIELD); ASSERT (BSON_ITER_HOLDS_DOCUMENT (&iter)); bson_iter_document (&iter, &len, &dummy); /* Should have truncated the platform field so it fits exactly */ ASSERT (len == METADATA_MAX_SIZE); mock_server_replies_simple (request, "{'ok': 1}"); request_destroy (request); request = mock_server_receives_command (server, "admin", MONGOC_QUERY_SLAVE_OK, "{'ping': 1}"); mock_server_replies_simple (request, "{'ok': 1}"); ASSERT (future_get_bool (future)); future_destroy (future); request_destroy (request); mongoc_client_destroy (client); mongoc_uri_destroy (uri); mock_server_destroy (server); /* So later tests don't have "aaaaa..." as the md platform string */ _reset_metadata (); }