Skip to content

Commit 3082e68

Browse files
Move grpc client test (#290)
1 parent 20f092c commit 3082e68

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/unit/client/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,13 @@ add_ydb_test(NAME client-draft_ut
9797
LABELS
9898
unit
9999
)
100+
101+
add_ydb_test(NAME grpc-client-low_ut
102+
SOURCES
103+
grpc_client_low_ut.cpp
104+
LINK_LIBRARIES
105+
cpp-testing-unittest_main
106+
grpc-client
107+
LABELS
108+
unit
109+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <ydb-cpp-sdk/library/grpc/client/grpc_client_low.h>
2+
3+
#include <library/cpp/testing/unittest/registar.h>
4+
5+
using namespace NYdbGrpc;
6+
7+
class TTestStub {
8+
public:
9+
std::shared_ptr<grpc::ChannelInterface> ChannelInterface;
10+
TTestStub(std::shared_ptr<grpc::ChannelInterface> channelInterface)
11+
: ChannelInterface(channelInterface)
12+
{}
13+
};
14+
15+
Y_UNIT_TEST_SUITE(ChannelPoolTests) {
16+
Y_UNIT_TEST(UnusedStubsHoldersDeletion) {
17+
TGRpcClientConfig clientConfig("invalid_host:invalid_port");
18+
TTcpKeepAliveSettings tcpKeepAliveSettings =
19+
{
20+
true,
21+
30, // NYdb::TCP_KEEPALIVE_IDLE, unused in UT, but is necessary in constructor
22+
5, // NYdb::TCP_KEEPALIVE_COUNT, unused in UT, but is necessary in constructor
23+
10 // NYdb::TCP_KEEPALIVE_INTERVAL, unused in UT, but is necessary in constructor
24+
};
25+
auto channelPool = TChannelPool(tcpKeepAliveSettings, TDuration::MilliSeconds(250));
26+
std::vector<std::weak_ptr<grpc::ChannelInterface>> ChannelInterfacesWeak;
27+
28+
{
29+
std::vector<std::shared_ptr<TTestStub>> stubsHoldersShared;
30+
auto storeStubsHolders = [&](TStubsHolder& stubsHolder) {
31+
stubsHoldersShared.emplace_back(stubsHolder.GetOrCreateStub<TTestStub>());
32+
ChannelInterfacesWeak.emplace_back((*stubsHoldersShared.rbegin())->ChannelInterface);
33+
return;
34+
};
35+
for (int i = 0; i < 10; ++i) {
36+
channelPool.GetStubsHolderLocked(
37+
ToString(i),
38+
clientConfig,
39+
storeStubsHolders
40+
);
41+
}
42+
}
43+
44+
auto now = Now();
45+
while (Now() < now + TDuration::MilliSeconds(500)){
46+
Sleep(TDuration::MilliSeconds(100));
47+
}
48+
49+
channelPool.DeleteExpiredStubsHolders();
50+
51+
bool allDeleted = true;
52+
for (auto i = ChannelInterfacesWeak.begin(); i != ChannelInterfacesWeak.end(); ++i) {
53+
allDeleted = allDeleted && i->expired();
54+
}
55+
56+
// assertion is made for channel interfaces instead of stubs, because after stub deletion
57+
// TStubsHolder has the only shared_ptr for channel interface.
58+
UNIT_ASSERT_C(allDeleted, "expired stubsHolders were not deleted after timeout");
59+
60+
}
61+
} // ChannelPoolTests ut suite

0 commit comments

Comments
 (0)