From 60e2b831b9b1ad556145d8ac0bccec36b62ec292 Mon Sep 17 00:00:00 2001 From: st-shchetinin Date: Thu, 8 Aug 2024 13:54:22 +0300 Subject: [PATCH] added grpc_client_low_ut --- tests/unit/client/CMakeLists.txt | 10 ++++ tests/unit/client/grpc_client_low_ut.cpp | 61 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/unit/client/grpc_client_low_ut.cpp diff --git a/tests/unit/client/CMakeLists.txt b/tests/unit/client/CMakeLists.txt index 9c7a6a50a2..8cde2e7be6 100644 --- a/tests/unit/client/CMakeLists.txt +++ b/tests/unit/client/CMakeLists.txt @@ -97,3 +97,13 @@ add_ydb_test(NAME client-draft_ut LABELS unit ) + +add_ydb_test(NAME grpc-client-low_ut + SOURCES + grpc_client_low_ut.cpp + LINK_LIBRARIES + cpp-testing-unittest_main + grpc-client + LABELS + unit +) \ No newline at end of file diff --git a/tests/unit/client/grpc_client_low_ut.cpp b/tests/unit/client/grpc_client_low_ut.cpp new file mode 100644 index 0000000000..43f3bdf39b --- /dev/null +++ b/tests/unit/client/grpc_client_low_ut.cpp @@ -0,0 +1,61 @@ +#include + +#include + +using namespace NYdbGrpc; + +class TTestStub { +public: + std::shared_ptr ChannelInterface; + TTestStub(std::shared_ptr channelInterface) + : ChannelInterface(channelInterface) + {} +}; + +Y_UNIT_TEST_SUITE(ChannelPoolTests) { + Y_UNIT_TEST(UnusedStubsHoldersDeletion) { + TGRpcClientConfig clientConfig("invalid_host:invalid_port"); + TTcpKeepAliveSettings tcpKeepAliveSettings = + { + true, + 30, // NYdb::TCP_KEEPALIVE_IDLE, unused in UT, but is necessary in constructor + 5, // NYdb::TCP_KEEPALIVE_COUNT, unused in UT, but is necessary in constructor + 10 // NYdb::TCP_KEEPALIVE_INTERVAL, unused in UT, but is necessary in constructor + }; + auto channelPool = TChannelPool(tcpKeepAliveSettings, TDuration::MilliSeconds(250)); + std::vector> ChannelInterfacesWeak; + + { + std::vector> stubsHoldersShared; + auto storeStubsHolders = [&](TStubsHolder& stubsHolder) { + stubsHoldersShared.emplace_back(stubsHolder.GetOrCreateStub()); + ChannelInterfacesWeak.emplace_back((*stubsHoldersShared.rbegin())->ChannelInterface); + return; + }; + for (int i = 0; i < 10; ++i) { + channelPool.GetStubsHolderLocked( + ToString(i), + clientConfig, + storeStubsHolders + ); + } + } + + auto now = Now(); + while (Now() < now + TDuration::MilliSeconds(500)){ + Sleep(TDuration::MilliSeconds(100)); + } + + channelPool.DeleteExpiredStubsHolders(); + + bool allDeleted = true; + for (auto i = ChannelInterfacesWeak.begin(); i != ChannelInterfacesWeak.end(); ++i) { + allDeleted = allDeleted && i->expired(); + } + + // assertion is made for channel interfaces instead of stubs, because after stub deletion + // TStubsHolder has the only shared_ptr for channel interface. + UNIT_ASSERT_C(allDeleted, "expired stubsHolders were not deleted after timeout"); + + } +} // ChannelPoolTests ut suite \ No newline at end of file