summaryrefslogtreecommitdiff
path: root/py/test/Ice/retry/AllTests.py
blob: 6e748465b2208f17ec0cbc5ceba4a4ecf91fd66d (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
# **********************************************************************
#
# Copyright (c) 2003-2014 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.
#
# **********************************************************************

import Ice, Test, sys, threading

def test(b):
    if not b:
        raise RuntimeError('test assertion failed')

class CallbackBase:
    def __init__(self):
        self._called = False
        self._cond = threading.Condition()

    def check(self):
        self._cond.acquire()
        try:
            while not self._called:
                self._cond.wait()
            self._called = False
        finally:
            self._cond.release()

    def called(self):
        self._cond.acquire()
        self._called = True
        self._cond.notify()
        self._cond.release()

class CallbackSuccess(CallbackBase):
    def response(self):
        self.called()

    def exception(self, ex):
        test(False)

class CallbackFail(CallbackBase):
    def response(self):
        test(False)

    def exception(self, ex):
        test(isinstance(ex, Ice.ConnectionLostException))
        self.called()

def allTests(communicator):
    sys.stdout.write("testing stringToProxy... ")
    sys.stdout.flush()
    ref = "retry:default -p 12010"
    base1 = communicator.stringToProxy(ref)
    test(base1)
    base2 = communicator.stringToProxy(ref)
    test(base2)
    print("ok")

    sys.stdout.write("testing checked cast... ")
    sys.stdout.flush()
    retry1 = Test.RetryPrx.checkedCast(base1)
    test(retry1)
    test(retry1 == base1)
    retry2 = Test.RetryPrx.checkedCast(base2)
    test(retry2)
    test(retry2 == base2)
    print("ok")

    sys.stdout.write("calling regular operation with first proxy... ")
    sys.stdout.flush()
    retry1.op(False)
    print("ok")

    sys.stdout.write("calling operation to kill connection with second proxy... ")
    sys.stdout.flush()
    try:
        retry2.op(True)
        test(False)
    except Ice.ConnectionLostException:
        print("ok")

    sys.stdout.write("calling regular operation with first proxy again... ")
    sys.stdout.flush()
    retry1.op(False)
    print("ok")

    cb1 = CallbackSuccess()
    cb2 = CallbackFail()

    sys.stdout.write("calling regular AMI operation with first proxy... ")
    sys.stdout.flush()
    retry1.begin_op(False, cb1.response, cb1.exception)
    cb1.check()
    print("ok")

    sys.stdout.write("calling AMI operation to kill connection with second proxy... ")
    sys.stdout.flush()
    retry2.begin_op(True, cb2.response, cb2.exception)
    cb2.check()
    print("ok")

    sys.stdout.write("calling regular AMI operation with first proxy again... ")
    sys.stdout.flush()
    retry1.begin_op(False, cb1.response, cb1.exception)
    cb1.check()
    print("ok")

    return retry1