summaryrefslogtreecommitdiff
path: root/py/test/Ice/retry/AllTests.py
blob: 2e2d3762bb6b7df9a732e4ffcfb2a4216a05d776 (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2007 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, 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(5.0)
            if self._called:
                self._called = False
                return True
            else:
                return False
        finally:
            self._cond.release()

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

class AMIRegular(CallbackBase):
    def __init__(self):
        CallbackBase.__init__(self)

    def ice_response(self):
        self.called()

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

class AMIException(CallbackBase):
    def __init__(self):
        CallbackBase.__init__(self)

    def ice_response(self):
        test(False)

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

def allTests(communicator):
    print "testing stringToProxy...",
    ref = "retry:default -p 12010 -t 10000"
    base1 = communicator.stringToProxy(ref)
    test(base1)
    base2 = communicator.stringToProxy(ref)
    test(base2)
    print "ok"

    print "testing checked cast...",
    retry1 = Test.RetryPrx.checkedCast(base1)
    test(retry1)
    test(retry1 == base1)
    retry2 = Test.RetryPrx.checkedCast(base2)
    test(retry2)
    test(retry2 == base2)
    print "ok"

    print "calling regular operation with first proxy...",
    retry1.op(False)
    print "ok"

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

    print "calling regular operation with first proxy again...",
    retry1.op(False)
    print "ok"

    cb1 = AMIRegular()
    cb2 = AMIException()

    print "calling regular AMI operation with first proxy...",
    retry1.op_async(cb1, False)
    test(cb1.check())
    print "ok"

    print "calling AMI operation to kill connection with second proxy...",
    retry2.op_async(cb2, True)
    test(cb2.check())
    print "ok"

    print "calling regular AMI operation with first proxy again...",
    retry1.op_async(cb1, False)
    test(cb1.check())
    print "ok"

    return retry1
    return retry1