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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# **********************************************************************
#
# Copyright (c) 2003-2018 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, time, traceback
def test(b):
if not b:
raise RuntimeError('test assertion failed')
class LoggerI(Ice.Logger):
def __init__(self):
self._started = False
self._messages = []
self.m = threading.Lock()
def start(self):
with self.m:
self._started = True
self.dump()
def _print(self, msg):
with self.m:
self._messages.append(msg)
if self._started:
self.dump()
def trace(self, category, msg):
with self.m:
self._messages.append("[" + category + "] " + msg)
if self._started:
self.dump()
def warning(self, msg):
with self.m:
self._messages.append("warning: " + msg)
if self._started:
self.dump()
def error(self, msg):
with self.m:
self._messages.append("error: " + msg)
if self._started:
self.dump()
def getPrefix(self):
return ""
def cloneWithPrefix(self, prefix):
return self
def dump(self):
for p in self._messages:
print(p)
self._messages = []
class TestCase(threading.Thread):
def __init__(self, name, com):
threading.Thread.__init__(self)
self._name = name
self._com = com
self._logger = LoggerI()
self._clientACMTimeout = -1
self._clientACMClose = -1
self._clientACMHeartbeat = -1
self._serverACMTimeout = -1
self._serverACMClose = -1
self._serverACMHeartbeat = -1
self._heartbeat = 0
self._closed = False
self._msg = ""
self.m = threading.Condition()
def init(self):
self._adapter = \
self._com.createObjectAdapter(self._serverACMTimeout, self._serverACMClose, self._serverACMHeartbeat)
initData = Ice.InitializationData()
initData.properties = self._com.ice_getCommunicator().getProperties().clone()
initData.logger = self._logger
initData.properties.setProperty("Ice.ACM.Timeout", "2")
if self._clientACMTimeout >= 0:
initData.properties.setProperty("Ice.ACM.Client.Timeout", str(self._clientACMTimeout))
if self._clientACMClose >= 0:
initData.properties.setProperty("Ice.ACM.Client.Close", str(self._clientACMClose))
if self._clientACMHeartbeat >= 0:
initData.properties.setProperty("Ice.ACM.Client.Heartbeat", str(self._clientACMHeartbeat))
#initData.properties.setProperty("Ice.Trace.Protocol", "2")
#initData.properties.setProperty("Ice.Trace.Network", "2")
self._communicator = Ice.initialize(initData)
def destroy(self):
self._adapter.deactivate()
self._communicator.destroy()
def joinWithThread(self):
sys.stdout.write("testing " + self._name + "... ")
sys.stdout.flush()
self._logger.start()
self.join()
if len(self._msg) == 0:
print("ok")
else:
print("failed!\n" + self._msg)
test(False)
def run(self):
proxy = Test.TestIntfPrx.uncheckedCast(self._communicator.stringToProxy(
self._adapter.getTestIntf().ice_toString()))
try:
proxy.ice_getConnection().setCloseCallback(lambda conn: self.closed(conn))
proxy.ice_getConnection().setHeartbeatCallback(lambda conn: self.heartbeat(conn))
self.runTestCase(self._adapter, proxy)
except Exception as ex:
self._msg = "unexpected exception:\n" + traceback.format_exc()
def heartbeat(self, con):
with self.m:
self._heartbeat = self._heartbeat + 1
def closed(self, con):
with self.m:
self._closed = True
self.m.notify()
def waitForClosed(self):
with self.m:
while not self._closed:
now = time.time()
self.m.wait(2.0) # Wait 2s
if time.time() - now > 2:
test(False)
def runTestCase(self, adapter, proxy):
test(False)
def setClientACM(self, timeout, close, heartbeat):
self._clientACMTimeout = timeout
self._clientACMClose = close
self._clientACMHeartbeat = heartbeat
def setServerACM(self, timeout, close, heartbeat):
self._serverACMTimeout = timeout
self._serverACMClose = close
self._serverACMHeartbeat = heartbeat
def allTests(helper, communicator):
ref = "communicator:{0}".format(helper.getTestEndpoint(num=0))
com = Test.RemoteCommunicatorPrx.uncheckedCast(communicator.stringToProxy(ref))
tests = []
class InvocationHeartbeatTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "invocation heartbeat", com)
self.setServerACM(1, -1, -1) # Faster ACM to make sure we receive enough ACM heartbeats
def runTestCase(self, adapter, proxy):
proxy.sleep(4)
with self.m:
test(self._heartbeat >= 4)
class InvocationHeartbeatOnHoldTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "invocation with heartbeat on hold", com)
# Use default ACM configuration.
def runTestCase(self, adapter, proxy):
try:
# When the OA is put on hold, connections shouldn't
# send heartbeats, the invocation should therefore
# fail.
proxy.sleepAndHold(10)
test(False)
except Ice.ConnectionTimeoutException:
adapter.activate()
proxy.interruptSleep()
self.waitForClosed()
class InvocationNoHeartbeatTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "invocation with no heartbeat", com)
self.setServerACM(2, 2, 0) # Disable heartbeat on invocations
def runTestCase(self, adapter, proxy):
try:
# Heartbeats are disabled on the server, the
# invocation should fail since heartbeats are
# expected.
proxy.sleep(10)
test(False)
except Ice.ConnectionTimeoutException:
proxy.interruptSleep()
self.waitForClosed()
with self.m:
test(self._heartbeat == 0)
class InvocationHeartbeatCloseOnIdleTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "invocation with no heartbeat and close on idle", com)
self.setClientACM(1, 1, 0) # Only close on idle.
self.setServerACM(1, 2, 0) # Disable heartbeat on invocations
def runTestCase(self, adapter, proxy):
# No close on invocation, the call should succeed this time.
proxy.sleep(3)
with self.m:
test(self._heartbeat == 0)
test(not self._closed)
class CloseOnIdleTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "close on idle", com)
self.setClientACM(1, 1, 0) # Only close on idle.
def runTestCase(self, adapter, proxy):
time.sleep(3) # Idle for 3 seconds
self.waitForClosed()
with self.m:
test(self._heartbeat == 0)
class CloseOnInvocationTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "close on invocation", com)
self.setClientACM(1, 2, 0) # Only close on invocation.
def runTestCase(self, adapter, proxy):
time.sleep(3) # Idle for 3 seconds
with self.m:
test(self._heartbeat == 0)
test(not self._closed)
class CloseOnIdleAndInvocationTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "close on idle and invocation", com)
self.setClientACM(1, 3, 0) # Only close on idle and invocation.
def runTestCase(self, adapter, proxy):
#
# Put the adapter on hold. The server will not respond to
# the graceful close. This allows to test whether or not
# the close is graceful or forceful.
#
adapter.hold()
time.sleep(3) # Idle for 3 seconds
with self.m:
test(self._heartbeat == 0)
test(not self._closed) # Not closed yet because of graceful close.
adapter.activate()
time.sleep(1)
self.waitForClosed()
class ForcefulCloseOnIdleAndInvocationTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "forceful close on idle and invocation", com)
self.setClientACM(1, 4, 0) # Only close on idle and invocation.
def runTestCase(self, adapter, proxy):
adapter.hold()
time.sleep(3) # Idle for 3 seconds
self.waitForClosed()
with self.m:
test(self._heartbeat == 0)
class HeartbeatOnIdleTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "heartbeat on idle", com)
self.setServerACM(1, -1, 2) # Enable server heartbeats.
def runTestCase(self, adapter, proxy):
time.sleep(3)
with self.m:
test(self._heartbeat >= 3)
class HeartbeatAlwaysTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "heartbeat always", com)
self.setServerACM(1, -1, 3) # Enable server heartbeats.
def runTestCase(self, adapter, proxy):
for i in range(0, 10):
proxy.ice_ping()
time.sleep(0.3)
with self.m:
test(self._heartbeat >= 3)
class HeartbeatManualTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "manual heartbeats", com)
#
# Disable heartbeats.
#
self.setClientACM(10, -1, 0)
self.setServerACM(10, -1, 0)
def runTestCase(self, adapter, proxy):
proxy.startHeartbeatCount()
con = proxy.ice_getConnection()
con.heartbeat()
con.heartbeat()
con.heartbeat()
con.heartbeat()
con.heartbeat()
proxy.waitForHeartbeatCount(5)
class SetACMTest(TestCase):
def __init__(self, com):
TestCase.__init__(self, "setACM/getACM", com)
self.setClientACM(15, 4, 0)
def runTestCase(self, adapter, proxy):
try:
proxy.ice_getCachedConnection().setACM(-19, Ice.Unset, Ice.Unset)
test(False)
except RuntimeError:
pass
acm = proxy.ice_getCachedConnection().getACM()
test(acm.timeout == 15)
test(acm.close == Ice.ACMClose.CloseOnIdleForceful)
test(acm.heartbeat == Ice.ACMHeartbeat.HeartbeatOff)
proxy.ice_getCachedConnection().setACM(Ice.Unset, Ice.Unset, Ice.Unset)
acm = proxy.ice_getCachedConnection().getACM()
test(acm.timeout == 15)
test(acm.close == Ice.ACMClose.CloseOnIdleForceful)
test(acm.heartbeat == Ice.ACMHeartbeat.HeartbeatOff)
proxy.ice_getCachedConnection().setACM(1, Ice.ACMClose.CloseOnInvocationAndIdle,
Ice.ACMHeartbeat.HeartbeatAlways)
acm = proxy.ice_getCachedConnection().getACM()
test(acm.timeout == 1)
test(acm.close == Ice.ACMClose.CloseOnInvocationAndIdle)
test(acm.heartbeat == Ice.ACMHeartbeat.HeartbeatAlways)
proxy.startHeartbeatCount()
proxy.waitForHeartbeatCount(2)
tests.append(InvocationHeartbeatTest(com))
tests.append(InvocationHeartbeatOnHoldTest(com))
tests.append(InvocationNoHeartbeatTest(com))
tests.append(InvocationHeartbeatCloseOnIdleTest(com))
tests.append(CloseOnIdleTest(com))
tests.append(CloseOnInvocationTest(com))
tests.append(CloseOnIdleAndInvocationTest(com))
tests.append(ForcefulCloseOnIdleAndInvocationTest(com))
tests.append(HeartbeatOnIdleTest(com))
tests.append(HeartbeatAlwaysTest(com))
tests.append(HeartbeatManualTest(com))
tests.append(SetACMTest(com))
for p in tests:
p.init()
for p in tests:
p.start()
for p in tests:
p.joinWithThread()
for p in tests:
p.destroy()
sys.stdout.write("shutting down... ")
sys.stdout.flush()
com.shutdown()
print("ok")
|