aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/test/ut/module_client_one_way_ut.cpp
blob: fd511e2dd9b3f1f6073e3d2ccdb1e82059e7ebab (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <library/cpp/testing/unittest/registar.h>

#include <library/cpp/messagebus/test/helper/example.h>
#include <library/cpp/messagebus/test/helper/message_handler_error.h>

#include <library/cpp/messagebus/misc/test_sync.h>
#include <library/cpp/messagebus/oldmodule/module.h>

using namespace NBus;
using namespace NBus::NTest;

Y_UNIT_TEST_SUITE(ModuleClientOneWay) {
    struct TTestServer: public TBusServerHandlerError {
        TExampleProtocol Proto;

        TTestSync* const TestSync;

        TBusMessageQueuePtr Queue;
        TBusServerSessionPtr ServerSession;

        TTestServer(TTestSync* testSync)
            : TestSync(testSync)
        {
            Queue = CreateMessageQueue();
            ServerSession = TBusServerSession::Create(&Proto, this, TBusServerSessionConfig(), Queue);
        }

        void OnMessage(TOnMessageContext& context) override {
            TestSync->WaitForAndIncrement(1);
            context.ForgetRequest();
        }
    };

    struct TClientModule: public TBusModule {
        TExampleProtocol Proto;

        TTestSync* const TestSync;
        unsigned const Port;

        TBusClientSessionPtr ClientSession;

        TClientModule(TTestSync* testSync, unsigned port)
            : TBusModule("m")
            , TestSync(testSync)
            , Port(port)
        { 
        } 

        TJobHandler Start(TBusJob* job, TBusMessage*) override {
            TestSync->WaitForAndIncrement(0);

            job->SendOneWayTo(new TExampleRequest(&Proto.RequestCount), ClientSession.Get(), TNetAddr("localhost", Port));

            return &TClientModule::Sent;
        }

        TJobHandler Sent(TBusJob* job, TBusMessage*) {
            TestSync->WaitForAndIncrement(2);
            job->Cancel(MESSAGE_DONT_ASK);
            return nullptr;
        }

        TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override {
            ClientSession = CreateDefaultSource(queue, &Proto, TBusServerSessionConfig());
            return nullptr;
        }
    };

    Y_UNIT_TEST(Simple) {
        TTestSync testSync;

        TTestServer server(&testSync);

        TBusMessageQueuePtr queue = CreateMessageQueue();
        TClientModule clientModule(&testSync, server.ServerSession->GetActualListenPort());

        clientModule.CreatePrivateSessions(queue.Get());
        clientModule.StartInput();

        clientModule.StartJob(new TExampleRequest(&clientModule.Proto.StartCount));

        testSync.WaitForAndIncrement(3);

        clientModule.Shutdown();
    }

    struct TSendErrorModule: public TBusModule {
        TExampleProtocol Proto;

        TTestSync* const TestSync;

        TBusClientSessionPtr ClientSession;

        TSendErrorModule(TTestSync* testSync)
            : TBusModule("m")
            , TestSync(testSync)
        { 
        } 

        TJobHandler Start(TBusJob* job, TBusMessage*) override {
            TestSync->WaitForAndIncrement(0);

            job->SendOneWayTo(new TExampleRequest(&Proto.RequestCount), ClientSession.Get(), TNetAddr("localhost", 1));

            return &TSendErrorModule::Sent;
        }

        TJobHandler Sent(TBusJob* job, TBusMessage*) {
            TestSync->WaitForAndIncrement(1);
            job->Cancel(MESSAGE_DONT_ASK);
            return nullptr;
        }

        TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override {
            TBusServerSessionConfig sessionConfig;
            sessionConfig.ConnectTimeout = 1;
            sessionConfig.SendTimeout = 1;
            sessionConfig.TotalTimeout = 1;
            sessionConfig.Secret.TimeoutPeriod = TDuration::MilliSeconds(1);
            ClientSession = CreateDefaultSource(queue, &Proto, sessionConfig);
            return nullptr;
        }
    };

    Y_UNIT_TEST(SendError) {
        TTestSync testSync;

        TBusQueueConfig queueConfig;
        queueConfig.NumWorkers = 5;

        TBusMessageQueuePtr queue = CreateMessageQueue(queueConfig);
        TSendErrorModule clientModule(&testSync);

        clientModule.CreatePrivateSessions(queue.Get());
        clientModule.StartInput();

        clientModule.StartJob(new TExampleRequest(&clientModule.Proto.StartCount));

        testSync.WaitForAndIncrement(2);

        clientModule.Shutdown();
    }
}