summaryrefslogtreecommitdiff
path: root/cpp/test/IceBridge/simple/AllTests.cpp
blob: 16a76f21cb8997f91fd2fa7d039a248d2a65d1e9 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/Ice.h>
#include <TestHelper.h>
#include <Test.h>
#include <mutex>
#include <chrono>
#include <atomic>

using namespace std;
using namespace std::chrono_literals;

namespace
{

class CallbackI final : public Test::Callback
{
public:

    void
    ping(const Ice::Current&) override
    {
        ++_count;
    }

    int
    getCount(const Ice::Current&) override
    {
        return _count;
    }

    void
    datagram(const Ice::Current& c) override
    {
        test(c.con->getEndpoint()->getInfo()->datagram());
        ++_datagramCount;
    }

    int
    getDatagramCount(const Ice::Current&) override
    {
        return _datagramCount;
    }

private:

    int _count = 0;
    int _datagramCount = 0;
};

}

void
allTests(Test::TestHelper* helper)
{
    auto communicator = helper->communicator();
    cout << "testing connection to bridge... " << flush;
    auto prx = communicator->stringToProxy("test:" + helper->getTestEndpoint(1) + ":" +
                                           helper->getTestEndpoint(1, "udp"));
    test(prx);
    auto cl = Ice::checkedCast<Test::MyClassPrx>(prx);
    cl->ice_ping();
    cout << "ok" << endl;

    cout << "testing datagrams... " << flush;
    {
        for(int i = 0; i < 20; i++)
        {
            cl->ice_datagram()->datagram();
        }
        int nRetry = 20;
        while(cl->getDatagramCount() < 10 && --nRetry > 0)
        {
            this_thread::sleep_for(50ms);
        }
        test(cl->getDatagramCount() >= 10);
    }
    cout << "ok" << endl;

    cout << "testing connection close... " << flush;
    {
        auto clc =
            Ice::checkedCast<Test::MyClassPrx>(cl->ice_getConnection()->createProxy(cl->ice_getIdentity()));
        clc->ice_ping();
        clc->closeConnection(false);
        int nRetry = 20;
        while(--nRetry > 0)
        {
            try
            {
                clc->ice_ping();
                test(false);
            }
            catch(const Ice::CloseConnectionException&)
            {
                // Wait for the CloseConnectionException before continuing
                break;
            }
            catch(const Ice::UnknownLocalException& ex)
            {
                // The bridge forwards the CloseConnectionException from the server as an
                // UnknownLocalException. It eventually closes the connection when notified
                // of the connection close.
                test(ex.unknown.find("CloseConnectionException") != string::npos);
            }
            this_thread::sleep_for(1ms);
        }
        try
        {
            clc->ice_ping();
            test(false);
        }
        catch(const Ice::CloseConnectionException&)
        {
        }
    }
    cout << "ok" << endl;

    cout << "testing multiple connections... " << flush;
    {
        test(cl->getConnectionInfo() == cl->getConnectionInfo());
        int nRetry = 20;
        while(cl->getConnectionCount() != 2 && --nRetry > 0)
        {
            this_thread::sleep_for(50ms);
        }
        test(cl->getConnectionCount() == 2);
        test(cl->ice_connectionId("other")->getConnectionInfo() != cl->getConnectionInfo());
        test(cl->getConnectionCount() == 3);
        cl->ice_connectionId("other")->ice_getConnection()->close(Ice::ConnectionClose::Gracefully);
        nRetry = 20;
        while(cl->getConnectionCount() != 2 && --nRetry > 0)
        {
            this_thread::sleep_for(50ms);
        }
        test(cl->getConnectionCount() == 2);
    }
    cout << "ok" << endl;

    cout << "testing ordering... " << flush;
    {
        //
        // Make sure ordering is preserved on connection establishemnt.
        //
        int counter = 0;
        for(int i = 0; i < 10; ++i)
        {
            ostringstream os;
            os << i;
            auto p = cl->ice_connectionId(os.str());
            for(int j = 0; j < 20; ++j)
            {
                p->incCounterAsync(++counter, nullptr);
            }
            cl->waitCounter(counter);
            p->closeConnection(false);
        }
        for(int i = 0; i < 10; ++i)
        {
            ostringstream os;
            os << i;
            auto p = cl->ice_connectionId(os.str())->ice_oneway();
            for(int j = 0; j < 20; ++j)
            {
                p->incCounterAsync(++counter, nullptr);
            }
            cl->waitCounter(counter);
            p->closeConnection(false);
        }
    }
    cout << "ok" << endl;

    auto adapter = communicator->createObjectAdapter("");
    Ice::Identity id;
    id.name = "callback";
    adapter->add(make_shared<CallbackI>(), id);

    cout << "testing bi-dir callbacks... " << flush;
    {
        cl->ice_getConnection()->setAdapter(adapter);
        cl->callCallback();
        cl->callCallback();
        cl->callCallback();
        test(cl->getCallbackCount() == 3);
        cl->callCallback();
        test(cl->getCallbackCount() == 4);
    }
    cout << "ok" << endl;

    cout << "testing datagram bi-dir callbacks... " << flush;
    {
        auto p = cl->ice_datagram();
        p->ice_getConnection()->setAdapter(adapter);
        for(int i = 0; i < 20; i++)
        {
            p->callDatagramCallback();
        }
        int nRetry = 20;
        while(cl->getCallbackDatagramCount() < 10 && --nRetry > 0)
        {
            this_thread::sleep_for(50ms);
        }
        test(cl->getCallbackDatagramCount() >= 10);
    }
    cout << "ok" << endl;

    cout << "testing router... " << flush;
    {
        auto base = communicator->stringToProxy("Ice/RouterFinder:" + helper->getTestEndpoint(1));
        auto finder = Ice::checkedCast<Ice::RouterFinderPrx>(base);
        auto router = finder->getRouter();
        base = communicator->stringToProxy("test")->ice_router(router);
        auto p = Ice::checkedCast<Test::MyClassPrx>(base);
        p->ice_ping();
    }
    cout << "ok" << endl;

    cout << "testing heartbeats... " << flush;
    {
        test(cl->getHeartbeatCount() == 0); // No heartbeats enabled by default

        auto p = cl->ice_connectionId("heartbeat");
        p->ice_getConnection()->setACM(1, Ice::nullopt, Ice::ACMHeartbeat::HeartbeatAlways);

        auto p2 = cl->ice_connectionId("heartbeat2");
        atomic_int counter = 0;
        p2->ice_getConnection()->setHeartbeatCallback([&counter](const auto&)
                                                      {
                                                          counter++;
                                                      });
        p2->enableHeartbeats();

        int nRetry = 20;
        while((p->getHeartbeatCount() < 1 || counter.load() < 1) && --nRetry > 0)
        {
            this_thread::sleep_for(500ms); // TODO: check sleep time
        }
        test(p->getHeartbeatCount() > 0 && counter.load() > 0);
    }
    cout << "ok" << endl;

    cout << "testing server shutdown... " << flush;
    cl->shutdown();
    cout << "ok" << endl;

    cout << "testing bridge shutdown... " << flush;
    auto admin = communicator->stringToProxy("IceBridge/admin:" + helper->getTestEndpoint(2, "tcp"));
    auto process = Ice::checkedCast<Ice::ProcessPrx>(admin->ice_facet("Process"));
    process->shutdown();
    cout << "ok" << endl;
}