我编写了一些使用mongodb-c-driver的基本函数。我试着用GTest写一些测试
在每个测试用例中,我用(mongoc_init()
和mongoc_cleanup()
) init和destroy mongoc。
当我运行一个测试用例时,一切都很好,但是当我运行两个或更多测试时,我得到一些无效的读取:
==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)
我需要存根所有的蒙古驱动程序功能做一些单元/集成测试我的功能吗?
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函数
mongoc_client_destroy(mongoClient);
mongoc_cleanup();
要测试的函数
return mongoc_client_get_collection(mongoClient, database, collectionName);
要回答我的问题,问题是在每个测试用例结束时调用mongoc_cleanup()。
一旦调用了mongoc_cleanup(),再调用mongoc_init()是无效的。http://mongoc.org/libmongoc/current/init-cleanup.html简介
mongoc_init()应该在测试表开始时调用,而mongoc_cleanup()应该在测试表结束时调用。