I wrote some basic funtions that uses mongodb-c-driver. and I tried to write some tests with GTest.
in each test case I init and destroy mongoc with ( mongoc_init()
and mongoc_cleanup()
).
When I run one test case, everything goes fine but when I run two tests or more I’m getting some invalid reads :
==3726== Invalid read of size 1
==3726== at 0x4842B60: memmove (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3726== by 0x4A81C82: memcpy (string_fortified.h:34)
==3726== by 0x4A81C82: bson_string_append (bson-string.c:143)
==3726== by 0x49F9C72: _append_platform_field (mongoc-handshake.c:495)
==3726== by 0x49F9C72: _mongoc_handshake_build_doc_with_application (mongoc-handshake.c:560)
==3726== by 0x4A1A11A: _build_ismaster_with_handshake (mongoc-topology-scanner.c:232)
==3726== by 0x4A1A11A: _mongoc_topology_scanner_get_ismaster (mongoc-topology-scanner.c:263)
==3726== by 0x4A1A2BF: _begin_ismaster_cmd (mongoc-topology-scanner.c:291)
==3726== by 0x4A1AC78: mongoc_topology_scanner_node_setup_tcp (mongoc-topology-scanner.c:836)
==3726== by 0x4A1AF57: mongoc_topology_scanner_node_setup (mongoc-topology-scanner.c:960)
==3726== by 0x4A1B18E: mongoc_topology_scanner_start (mongoc-topology-scanner.c:1083)
==3726== by 0x4A15126: mongoc_topology_scan_once (mongoc-topology.c:765)
==3726== by 0x4A15126: _mongoc_topology_do_blocking_scan (mongoc-topology.c:797)
==3726== by 0x4A157F4: mongoc_topology_select_server_id (mongoc-topology.c:1030)
==3726== by 0x49DCD90: _mongoc_cluster_select_server_id (mongoc-cluster.c:2704)
==3726== by 0x49E169F: _mongoc_cluster_stream_for_optype (mongoc-cluster.c:2750)
Do I need to stub all mongoc-driver fuctions to do some unit/integration tests to my functions ?
Mongo Init Function :
mongoc_init();
uri = mongoc_uri_new_with_error("mongodb://127.0.0.1:27017", &error);
return mongoc_client_new_from_uri(uri);
Mongo Cleanup function
mongoc_client_destroy(mongoClient);
mongoc_cleanup();
Functions to test
return mongoc_client_get_collection(mongoClient, database, collectionName);
To answer my question, the probleme was calling mongoc_cleanup() at the end of each test case.
Once mongoc_cleanup() is called, it is invalid to call mongoc_init() again. http://mongoc.org/libmongoc/current/init-cleanup.html#synopsis
mongoc_init() should be called at the start of the test sheet, and mongoc_cleanup() at the end test sheet.