From 3082e6897748f924ee0e78253bbf1c160e6e8c78 Mon Sep 17 00:00:00 2001 From: stanislav_shchetinin Date: Thu, 8 Aug 2024 18:45:18 +0300 Subject: [PATCH] Move grpc client test (#290) --- 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..2257d18e50 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 +) 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..4abbafee7c --- /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