Envoy 的单元测试套路
以下假设已经读过:
被测试对象
/source/extensions/transport_sockets/tcp_stats/tcp_stats.h
|
|
/source/extensions/transport_sockets/tcp_stats/tcp_stats.cc
|
|
Mocks
EXPECT_CALL
EXPECT_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))
Creates an expectation that the method
method_name
of the objectmock_object
is called with arguments that match the given matchersmatchers...
.EXPECT_CALL
must precede any code that exercises the mock object.
ON_CALL
ON_CALL(`*`mock_object`*`,`*`method_name`*`(`*`matchers...`*`))
Defines what happens when the method
method_name
of the objectmock_object
is called with arguments that match the given matchersmatchers...
. Requires a modifier clause to specify the method’s behavior. Does not set any expectations that the method will be called.
ON_CALL
与 EXPECT_CALL
的区别:https://google.github.io/googletest/gmock_cheat_sheet.html#OnCall
To customize the default action for a particular method of a specific mock object, use
ON_CALL
.ON_CALL
has a similar syntax toEXPECT_CALL
, but it is used for setting default behaviors when you do not require that the mock method is called. See Knowing When to Expect for a more detailed discussion.
/test/mocks/network/io_handle.h
|
|
|
|
Unit test
/test/extensions/transport_sockets/tcp_stats/tcp_stats_test.cc
|
|
我在上面的 tcp_stats_test.cc#L42 中设置了断点:
|
|
以下是调用 Stack,可以用 行号 对应到 tcp_stats_test.cc 等上面代码片段的位置。
Envoy::Extensions::TransportSockets::TcpStats::TcpStatsTest::initialize(bool)::'lambda'(int, int, void*, unsigned int*)::operator()(int, int, void*, unsigned int*) const (/workspaces/envoy/test/extensions/transport_sockets/tcp_stats/tcp_stats_test.cc:42)
...
...
testing::Action<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::Perform(tuple<int, int, void*, unsigned int*>) const (@testing::Action<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::Perform(tuple<int, int, void*, unsigned int*>) const:23)
testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::PerformDefaultAction(tuple<int, int, void*, unsigned int*>&&, basic_string<char, char_traits<char>, allocator<char>> const&) const (@testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::PerformDefaultAction(tuple<int, int, void*, unsigned int*>&&, basic_string<char, char_traits<char>, allocator<char>> const&) const:35)
...
testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::UntypedPerformDefaultAction(void*, basic_string<char, char_traits<char>, allocator<char>> const&) const (@testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::UntypedPerformDefaultAction(void*, basic_string<char, char_traits<char>, allocator<char>> const&) const:19)
testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void*) (@testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void*):60)
testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::Invoke(int, int, void*, unsigned int*) (@testing::internal::FunctionMocker<Envoy::Api::SysCallResult<int> (int, int, void*, unsigned int*)>::Invoke(int, int, void*, unsigned int*):34)
Envoy::Network::MockIoHandle::getOption(int, int, void*, unsigned int*) (/workspaces/envoy/test/mocks/network/io_handle.h:49)
Envoy::Extensions::TransportSockets::TcpStats::TcpStatsSocket::querySocketInfo() (/workspaces/envoy/source/extensions/transport_sockets/tcp_stats/tcp_stats.cc:81)
Envoy::Extensions::TransportSockets::TcpStats::TcpStatsSocket::recordStats() (/workspaces/envoy/source/extensions/transport_sockets/tcp_stats/tcp_stats.cc:92)
Envoy::Extensions::TransportSockets::TcpStats::TcpStatsSocket::onConnected()::$_2::operator()() const (/workspaces/envoy/source/extensions/transport_sockets/tcp_stats/tcp_stats.cc:49)
...
Envoy::Extensions::TransportSockets::TcpStats::TcpStatsTest_Periodic_Test::TestBody() (/workspaces/envoy/test/extensions/transport_sockets/tcp_stats/tcp_stats_test.cc:103)
void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (@void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*):35)
void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) (@void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*):29)
testing::Test::Run() (@testing::Test::Run():47)
testing::TestInfo::Run() (@testing::TestInfo::Run():53)
testing::TestSuite::Run() (@testing::TestSuite::Run():65)
testing::internal::UnitTestImpl::RunAllTests() (@testing::internal::UnitTestImpl::RunAllTests():223)
bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (@bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*):35)
bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) (@bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*):29)
testing::UnitTest::Run() (@testing::UnitTest::Run():48)
RUN_ALL_TESTS() (@RUN_ALL_TESTS():8)
Envoy::TestRunner::runTests(int, char**) (/workspaces/envoy/test/test_runner.cc:171)
main (/workspaces/envoy/test/main.cc:34)
__libc_start_main (@__libc_start_main:64)
_start (@_start:15)
debug unit test 前的准备工作
以下假设已经读过:
|
|
.vscode/launch.json :
|
|