aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/grpc/client/ut/grpc_client_low_ut.cpp
blob: b119b9d9afeab238d0b83c96d593235ff99a501f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <library/cpp/grpc/client/grpc_client_low.h> 
 
#include <library/cpp/testing/unittest/registar.h> 
 
using namespace NGrpc; 
 
class TTestStub { 
public: 
    std::shared_ptr<grpc::ChannelInterface> ChannelInterface; 
    TTestStub(std::shared_ptr<grpc::ChannelInterface> 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<std::weak_ptr<grpc::ChannelInterface>> ChannelInterfacesWeak; 
 
        { 
            std::vector<std::shared_ptr<TTestStub>> stubsHoldersShared; 
            auto storeStubsHolders = [&](TStubsHolder& stubsHolder) { 
                stubsHoldersShared.emplace_back(stubsHolder.GetOrCreateStub<TTestStub>()); 
                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