|
| 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