summaryrefslogtreecommitdiff
path: root/python/test/Ice/acm/TestI.py
blob: 920cbd44c7eac534ee2b4e49b830dc037c96ce7b (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
# **********************************************************************
#
# 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.
#
# **********************************************************************

import Ice, Test, threading

class RemoteCommunicatorI(Test.RemoteCommunicator):
    def createObjectAdapter(self, timeout, close, heartbeat, current=None):
        com = current.adapter.getCommunicator()
        properties = com.getProperties()
        protocol = properties.getPropertyWithDefault("Ice.Default.Protocol", "tcp");

        name = Ice.generateUUID()
        if timeout >= 0:
            properties.setProperty(name + ".ACM.Timeout", str(timeout))
        if close >= 0:
            properties.setProperty(name + ".ACM.Close", str(close))
        if heartbeat >= 0:
            properties.setProperty(name + ".ACM.Heartbeat", str(heartbeat))
        properties.setProperty(name + ".ThreadPool.Size", "2")
        adapter = com.createObjectAdapterWithEndpoints(name, protocol + " -h 127.0.0.1")
        return Test.RemoteObjectAdapterPrx.uncheckedCast(current.adapter.addWithUUID(RemoteObjectAdapterI(adapter)))

    def shutdown(self, current=None):
        current.adapter.getCommunicator().shutdown()

class RemoteObjectAdapterI(Test.RemoteObjectAdapter):
    def __init__(self, adapter):
        self._adapter = adapter
        self._testIntf = Test.TestIntfPrx.uncheckedCast(adapter.add(TestIntfI(),
                                                        adapter.getCommunicator().stringToIdentity("test")))
        adapter.activate()

    def getTestIntf(self, current=None):
        return self._testIntf

    def activate(self, current=None):
        self._adapter.activate()

    def hold(self, current=None):
        self._adapter.hold()

    def deactivate(self, current=None):
        try:
            self._adapter.destroy()
        except Ice.ObjectAdapterDeactivatedException:
            pass

class TestIntfI(Test.TestIntf):
    def __init__(self):
        self.m = threading.Condition()

    def sleep(self, delay, current=None):
        self.m.acquire()
        try:
            self.m.wait(delay)
        finally:
            self.m.release()

    def sleepAndHold(self, delay, current=None):
        self.m.acquire()
        try:
            current.adapter.hold()
            self.m.wait(delay)
        finally:
            self.m.release()

    def interruptSleep(self, delay, current=None):
        self.m.acquire()
        try:
            self.m.notifyAll()
        finally:
            self.m.release()

    def waitForHeartbeat(self, count, current=None):

        class ConnectionCallbackI():

            def __init__(self):
                self.m = threading.Condition()
                self.count = 0

            def heartbeat(self, con):
                self.m.acquire()
                try:
                    self.count -= 1
                    self.m.notifyAll()
                finally:
                    self.m.release()

            def waitForCount(self, count):
                self.m.acquire()
                self.count = count
                try:
                    while self.count > 0:
                        self.m.wait()
                finally:
                    self.m.release()

        callback = ConnectionCallbackI()
        current.con.setHeartbeatCallback(lambda con: callback.heartbeat(con))
        callback.waitForCount(2)