summaryrefslogtreecommitdiff
path: root/cpp/test/Ice/retry/AllTests.cpp
blob: 8b748d9a9bb84384d0db93895700a77f6e2bb830 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <TestCommon.h>
#include <Test.h>
#include <InstrumentationI.h>
#include <SystemFailure.h>

using namespace std;
using namespace Test;

class CallbackBase : public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    CallbackBase() :
        _called(false)
    {
    }

    virtual ~CallbackBase()
    {
    }

    void check()
    {
        IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
        while(!_called)
        {
            wait();
        }
        _called = false;
    }

protected:

    void called()
    {
        IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
        assert(!_called);
        _called = true;
        notify();
    }

private:

    bool _called;
};

class CallbackSuccess : public IceUtil::Shared, public CallbackBase
{
public:

    void response()
    {
        called();
    }

    void exception(const ::Ice::Exception&)
    {
        test(false);
    }
};
typedef IceUtil::Handle<CallbackSuccess> CallbackSuccessPtr;

class CallbackFail : public IceUtil::Shared, public CallbackBase
{
public:

    void response()
    {
        test(false);
    }

    void exception(const ::Ice::Exception& ex)
    {
        test(dynamic_cast<const Ice::ConnectionLostException*>(&ex) ||
             dynamic_cast<const Ice::UnknownLocalException*>(&ex));
        called();
    }
};
typedef IceUtil::Handle<CallbackFail> CallbackFailPtr;

RetryPrxPtr
allTests(const Ice::CommunicatorPtr& communicator, const Ice::CommunicatorPtr& communicator2, const string& ref)
{
    cout << "testing stringToProxy... " << flush;
    Ice::ObjectPrxPtr base1 = communicator->stringToProxy(ref);
    test(base1);
    Ice::ObjectPrxPtr base2 = communicator->stringToProxy(ref);
    test(base2);
    cout << "ok" << endl;

    cout << "testing checked cast... " << flush;
    RetryPrxPtr retry1 = ICE_CHECKED_CAST(RetryPrx, base1);
    test(retry1);
#ifdef ICE_CPP11_MAPPING
    test(Ice::targetEquals(retry1, base1));
#else
    test(retry1 == base1);
#endif
    RetryPrxPtr retry2 = ICE_CHECKED_CAST(RetryPrx, base2);
    test(retry2);
#ifdef ICE_CPP11_MAPPING
    test(Ice::targetEquals(retry2, base2));
#else
    test(retry2 == base2);
#endif
    cout << "ok" << endl;

    cout << "calling regular operation with first proxy... " << flush;
    retry1->op(false);
    cout << "ok" << endl;

    testInvocationCount(3);

    cout << "calling operation to kill connection with second proxy... " << flush;
    try
    {
        retry2->op(true);
        test(false);
    }
    catch(const Ice::UnknownLocalException&)
    {
        // Expected with collocation
    }
    catch(const Ice::ConnectionLostException&)
    {
    }
    testInvocationCount(1);
    testFailureCount(1);
    testRetryCount(0);
    cout << "ok" << endl;

    cout << "calling regular operation with first proxy again... " << flush;
    retry1->op(false);
    testInvocationCount(1);
    testFailureCount(0);
    testRetryCount(0);
    cout << "ok" << endl;

    CallbackSuccessPtr cb1 = new CallbackSuccess();
    CallbackFailPtr cb2 = new CallbackFail();

    cout << "calling regular AMI operation with first proxy... " << flush;
#ifdef ICE_CPP11_MAPPING
    retry1->op_async(false, 
        [cb1]()
        {
            cb1->response();
        },
        [cb1](exception_ptr err)
        {
            try
            {
                rethrow_exception(err);
            }
            catch(const Ice::Exception& ex)
            {
                cb1->exception(ex);
            }
        });
#else
    retry1->begin_op(false, newCallback_Retry_op(cb1, &CallbackSuccess::response, &CallbackSuccess::exception));
#endif
    cb1->check();
    testInvocationCount(1);
    testFailureCount(0);
    testRetryCount(0);
    cout << "ok" << endl;

    cout << "calling AMI operation to kill connection with second proxy... " << flush;
#ifdef ICE_CPP11_MAPPING
    retry2->op_async(true, 
        [cb2]()
        {
            cb2->response();
        },
        [cb2](exception_ptr err)
        {
            try
            {
                rethrow_exception(err);
            }
            catch(const Ice::Exception& ex)
            {
                cb2->exception(ex);
            }
        });
#else
    retry2->begin_op(true, newCallback_Retry_op(cb2, &CallbackFail::response, &CallbackFail::exception));
#endif
    cb2->check();
    testInvocationCount(1);
    testFailureCount(1);
    testRetryCount(0);
    cout << "ok" << endl;

    cout << "calling regular AMI operation with first proxy again... " << flush;
#ifdef ICE_CPP11_MAPPING
    retry1->op_async(false, 
        [cb1]()
        {
            cb1->response();
        },
        [cb1](exception_ptr err)
        {
            try
            {
                rethrow_exception(err);
            }
            catch(const Ice::Exception& ex)
            {
                cb1->exception(ex);
            }
        });
#else
    retry1->begin_op(false, newCallback_Retry_op(cb1, &CallbackSuccess::response, &CallbackSuccess::exception));
#endif
    cb1->check();
    testInvocationCount(1);
    testFailureCount(0);
    testRetryCount(0);
    cout << "ok" << endl;
    
    cout << "testing idempotent operation... " << flush;
    test(retry1->opIdempotent(4) == 4);
    testInvocationCount(1);
    testFailureCount(0);
    testRetryCount(4);
#ifdef ICE_CPP11_MAPPING
    test(retry1->opIdempotent_async(4).get() == 4);
#else
    test(retry1->end_opIdempotent(retry1->begin_opIdempotent(4)) == 4);
#endif
    testInvocationCount(1);
    testFailureCount(0);
    testRetryCount(4);
    cout << "ok" << endl;

    cout << "testing non-idempotent operation... " << flush;
    try
    {
        retry1->opNotIdempotent();
        test(false);
    }
    catch(const Ice::LocalException&)
    {
    }
    testInvocationCount(1);
    testFailureCount(1);
    testRetryCount(0);
    try
    {
#ifdef ICE_CPP11_MAPPING
        retry1->opNotIdempotent_async().get();
#else
        retry1->end_opNotIdempotent(retry1->begin_opNotIdempotent());
#endif
        test(false);
    }
    catch(const Ice::LocalException&)
    {
    }
    testInvocationCount(1);
    testFailureCount(1);
    testRetryCount(0);
    cout << "ok" << endl;

    if(!retry1->ice_getConnection())
    {
        testInvocationCount(-1);
        cout << "testing system exception... " << flush;
        try
        {
            retry1->opSystemException();
            test(false);
        }
        catch(const SystemFailure&)
        {
        }
        testInvocationCount(1);
        testFailureCount(1);
        testRetryCount(0);
        try
        {
#ifdef ICE_CPP11_MAPPING
            retry1->opSystemException_async().get();
#else
            retry1->end_opSystemException(retry1->begin_opSystemException());
#endif
            test(false);
        }
        catch(const SystemFailure&)
        {
        }
        testInvocationCount(1);
        testFailureCount(1);
        testRetryCount(0);
        cout << "ok" << endl;
    }

    cout << "testing invocation timeout and retries... " << flush;
    retry2 = ICE_CHECKED_CAST(RetryPrx, communicator2->stringToProxy(retry1->ice_toString()));
    try
    {
        retry2->ice_invocationTimeout(500)->opIdempotent(4);  // No more than 2 retries before timeout kicks-in
        test(false);
    }
    catch(const Ice::InvocationTimeoutException&)
    {
        testRetryCount(2);
        retry2->opIdempotent(-1); // Reset the counter
        testRetryCount(-1);
    }
    try
    {
        // No more than 2 retries before timeout kicks-in
        RetryPrxPtr prx = retry2->ice_invocationTimeout(500);
#ifdef ICE_CPP11_MAPPING
        prx->opIdempotent_async(4).get();
#else
        prx->end_opIdempotent(prx->begin_opIdempotent(4));
#endif
        test(false);
    }
    catch(const Ice::InvocationTimeoutException&)
    {
        testRetryCount(2);
        retry2->opIdempotent(-1);
        testRetryCount(-1);
    }
    cout << "ok" << endl;

    return retry1;
}