是否可以在使用 GTests 时不存根 mongo-c-driver?
Is it possible to not stub mongo-c-driver whiling using GTests?
我写了一些使用 mongodb-c-driver 的基本函数。我尝试用 GTest 编写一些测试。
在每个测试用例中,我使用(mongoc_init()
和 mongoc_cleanup()
)初始化并销毁 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)
我是否需要存根所有 mongoc-driver 函数来对我的函数做一些 unit/integration 测试?
Mongo 初始化函数:
mongoc_init();
uri = mongoc_uri_new_with_error("mongodb://127.0.0.1:27017", &error);
return mongoc_client_new_from_uri(uri);
Mongo 清理函数
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#synopsis
mongoc_init() 应在测试 sheet 开始时调用,mongoc_cleanup() 应在测试 sheet.
结束时调用
我写了一些使用 mongodb-c-driver 的基本函数。我尝试用 GTest 编写一些测试。
在每个测试用例中,我使用(mongoc_init()
和 mongoc_cleanup()
)初始化并销毁 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)
我是否需要存根所有 mongoc-driver 函数来对我的函数做一些 unit/integration 测试?
Mongo 初始化函数:
mongoc_init();
uri = mongoc_uri_new_with_error("mongodb://127.0.0.1:27017", &error);
return mongoc_client_new_from_uri(uri);
Mongo 清理函数
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#synopsis
mongoc_init() 应在测试 sheet 开始时调用,mongoc_cleanup() 应在测试 sheet.
结束时调用