ICE 3.4.2
[php5-ice-freebsdport.git] / py / test / Ice / operations / TwowaysNewAMI.py
blobec510969a542f2f32ad500de4e2e7c4160a345d3
1 # **********************************************************************
3 # Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
5 # This copy of Ice is licensed to you under the terms described in the
6 # ICE_LICENSE file included in this distribution.
8 # **********************************************************************
10 import Ice, Test, math, threading
12 def test(b):
13 if not b:
14 raise RuntimeError('test assertion failed')
16 class CallbackBase:
17 def __init__(self):
18 self._called = False
19 self._cond = threading.Condition()
21 def check(self):
22 self._cond.acquire()
23 try:
24 while not self._called:
25 self._cond.wait()
26 self._called = False
27 finally:
28 self._cond.release()
30 def called(self):
31 self._cond.acquire()
32 self._called = True
33 self._cond.notify()
34 self._cond.release()
36 class Callback(CallbackBase):
37 def __init__(self, communicator=None):
38 CallbackBase.__init__(self)
39 self._communicator = communicator
41 def ping(self):
42 self.called()
44 def isA(self, r):
45 test(r)
46 self.called()
48 def id(self, id):
49 test(id == "::Test::MyDerivedClass")
50 self.called()
52 def ids(self, ids):
53 test(len(ids) == 3)
54 self.called()
56 def opVoid(self):
57 self.called()
59 def opByte(self, r, b):
60 test(b == 0xf0)
61 test(r == 0xff)
62 self.called()
64 def opBool(self, r, b):
65 test(b)
66 test(not r)
67 self.called()
69 def opShortIntLong(self, r, s, i, l):
70 test(s == 10)
71 test(i == 11)
72 test(l == 12)
73 test(r == 12)
74 self.called()
76 def opFloatDouble(self, r, f, d):
77 test(f - 3.14 < 0.001)
78 test(d == 1.1E10)
79 test(r == 1.1E10)
80 self.called()
82 def opString(self, r, s):
83 test(s == "world hello")
84 test(r == "hello world")
85 self.called()
87 def opMyEnum(self, r, e):
88 test(e == Test.MyEnum.enum2)
89 test(r == Test.MyEnum.enum3)
90 self.called()
92 def opMyClass(self, r, c1, c2):
93 test(c1.ice_getIdentity() == self._communicator.stringToIdentity("test"))
94 test(c2.ice_getIdentity() == self._communicator.stringToIdentity("noSuchIdentity"))
95 test(r.ice_getIdentity() == self._communicator.stringToIdentity("test"))
96 # We can't do the callbacks below in serialize mode
97 if self._communicator.getProperties().getPropertyAsInt("Ice.Client.ThreadPool.Serialize") == 0:
98 r.opVoid()
99 c1.opVoid()
100 try:
101 c2.opVoid()
102 test(False)
103 except Ice.ObjectNotExistException:
104 pass
105 self.called()
107 def opStruct(self, rso, so):
108 test(rso.p == None)
109 test(rso.e == Test.MyEnum.enum2)
110 test(rso.s.s == "def")
111 test(so.e == Test.MyEnum.enum3)
112 test(so.s.s == "a new string")
113 # We can't do the callbacks below in serialize mode.
114 if self._communicator.getProperties().getPropertyAsInt("Ice.ThreadPool.Client.Serialize") == 0:
115 so.p.opVoid()
116 self.called()
118 def opByteS(self, rso, bso):
119 test(len(bso) == 4)
120 test(bso[0] == '\x22')
121 test(bso[1] == '\x12')
122 test(bso[2] == '\x11')
123 test(bso[3] == '\x01')
124 test(len(rso) == 8)
125 test(rso[0] == '\x01')
126 test(rso[1] == '\x11')
127 test(rso[2] == '\x12')
128 test(rso[3] == '\x22')
129 test(rso[4] == '\xf1')
130 test(rso[5] == '\xf2')
131 test(rso[6] == '\xf3')
132 test(rso[7] == '\xf4')
133 self.called()
135 def opBoolS(self, rso, bso):
136 test(len(bso) == 4)
137 test(bso[0])
138 test(bso[1])
139 test(not bso[2])
140 test(not bso[3])
141 test(len(rso) == 3)
142 test(not rso[0])
143 test(rso[1])
144 test(rso[2])
145 self.called()
147 def opShortIntLongS(self, rso, sso, iso, lso):
148 test(len(sso) == 3)
149 test(sso[0] == 1)
150 test(sso[1] == 2)
151 test(sso[2] == 3)
152 test(len(iso) == 4)
153 test(iso[0] == 8)
154 test(iso[1] == 7)
155 test(iso[2] == 6)
156 test(iso[3] == 5)
157 test(len(lso) == 6)
158 test(lso[0] == 10)
159 test(lso[1] == 30)
160 test(lso[2] == 20)
161 test(lso[3] == 10)
162 test(lso[4] == 30)
163 test(lso[5] == 20)
164 test(len(rso) == 3)
165 test(rso[0] == 10)
166 test(rso[1] == 30)
167 test(rso[2] == 20)
168 self.called()
170 def opFloatDoubleS(self, rso, fso, dso):
171 test(len(fso) == 2)
172 test(fso[0] - 3.14 < 0.001)
173 test(fso[1] - 1.11 < 0.001)
174 test(len(dso) == 3)
175 test(dso[0] == 1.3E10)
176 test(dso[1] == 1.2E10)
177 test(dso[2] == 1.1E10)
178 test(len(rso) == 5)
179 test(rso[0] == 1.1E10)
180 test(rso[1] == 1.2E10)
181 test(rso[2] == 1.3E10)
182 test(rso[3] - 3.14 < 0.001)
183 test(rso[4] - 1.11 < 0.001)
184 self.called()
186 def opStringS(self, rso, sso):
187 test(len(sso) == 4)
188 test(sso[0] == "abc")
189 test(sso[1] == "de")
190 test(sso[2] == "fghi")
191 test(sso[3] == "xyz")
192 test(len(rso) == 3)
193 test(rso[0] == "fghi")
194 test(rso[1] == "de")
195 test(rso[2] == "abc")
196 self.called()
198 def opByteSS(self, rso, bso):
199 test(len(bso) == 2)
200 test(len(bso[0]) == 1)
201 test(bso[0][0] == '\xff')
202 test(len(bso[1]) == 3)
203 test(bso[1][0] == '\x01')
204 test(bso[1][1] == '\x11')
205 test(bso[1][2] == '\x12')
206 test(len(rso) == 4)
207 test(len(rso[0]) == 3)
208 test(rso[0][0] == '\x01')
209 test(rso[0][1] == '\x11')
210 test(rso[0][2] == '\x12')
211 test(len(rso[1]) == 1)
212 test(rso[1][0] == '\xff')
213 test(len(rso[2]) == 1)
214 test(rso[2][0] == '\x0e')
215 test(len(rso[3]) == 2)
216 test(rso[3][0] == '\xf2')
217 test(rso[3][1] == '\xf1')
218 self.called()
220 def opFloatDoubleSS(self, rso, fso, dso):
221 test(len(fso) == 3)
222 test(len(fso[0]) == 1)
223 test(fso[0][0] - 3.14 < 0.001)
224 test(len(fso[1]) == 1)
225 test(fso[1][0] - 1.11 < 0.001)
226 test(len(fso[2]) == 0)
227 test(len(dso) == 1)
228 test(len(dso[0]) == 3)
229 test(dso[0][0] == 1.1E10)
230 test(dso[0][1] == 1.2E10)
231 test(dso[0][2] == 1.3E10)
232 test(len(rso) == 2)
233 test(len(rso[0]) == 3)
234 test(rso[0][0] == 1.1E10)
235 test(rso[0][1] == 1.2E10)
236 test(rso[0][2] == 1.3E10)
237 test(len(rso[1]) == 3)
238 test(rso[1][0] == 1.1E10)
239 test(rso[1][1] == 1.2E10)
240 test(rso[1][2] == 1.3E10)
241 self.called()
243 def opStringSS(self, rso, sso):
244 test(len(sso) == 5)
245 test(len(sso[0]) == 1)
246 test(sso[0][0] == "abc")
247 test(len(sso[1]) == 2)
248 test(sso[1][0] == "de")
249 test(sso[1][1] == "fghi")
250 test(len(sso[2]) == 0)
251 test(len(sso[3]) == 0)
252 test(len(sso[4]) == 1)
253 test(sso[4][0] == "xyz")
254 test(len(rso) == 3)
255 test(len(rso[0]) == 1)
256 test(rso[0][0] == "xyz")
257 test(len(rso[1]) == 0)
258 test(len(rso[2]) == 0)
259 self.called()
261 def opByteBoolD(self, ro, do):
262 di1 = {10: True, 100: False}
263 test(do == di1)
264 test(len(ro) == 4)
265 test(ro[10])
266 test(not ro[11])
267 test(not ro[100])
268 test(ro[101])
269 self.called()
271 def opShortIntD(self, ro, do):
272 di1 = {110: -1, 1100: 123123}
273 test(do == di1)
274 test(len(ro) == 4)
275 test(ro[110] == -1)
276 test(ro[111] == -100)
277 test(ro[1100] == 123123)
278 test(ro[1101] == 0)
279 self.called()
281 def opLongFloatD(self, ro, do):
282 di1 = {999999110: -1.1, 999999111: 123123.2}
283 for k in do:
284 test(math.fabs(do[k] - di1[k]) < 0.01)
285 test(len(ro) == 4)
286 test(ro[999999110] - -1.1 < 0.01)
287 test(ro[999999120] - -100.4 < 0.01)
288 test(ro[999999111] - 123123.2 < 0.01)
289 test(ro[999999130] - 0.5 < 0.01)
290 self.called()
292 def opStringStringD(self, ro, do):
293 di1 = {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
294 test(do == di1)
295 test(len(ro) == 4)
296 test(ro["foo"] == "abc -1.1")
297 test(ro["FOO"] == "abc -100.4")
298 test(ro["bar"] == "abc 123123.2")
299 test(ro["BAR"] == "abc 0.5")
300 self.called()
302 def opStringMyEnumD(self, ro, do):
303 di1 = {'abc': Test.MyEnum.enum1, '': Test.MyEnum.enum2}
304 test(do == di1)
305 test(len(ro) == 4)
306 test(ro["abc"] == Test.MyEnum.enum1)
307 test(ro["qwerty"] == Test.MyEnum.enum3)
308 test(ro[""] == Test.MyEnum.enum2)
309 test(ro["Hello!!"] == Test.MyEnum.enum2)
310 self.called()
312 def opMyEnumStringD(self, ro, do):
313 di1 = {Test.MyEnum.enum1: 'abc'}
314 test(do == di1)
315 test(len(ro) == 3)
316 test(ro[Test.MyEnum.enum1] == "abc")
317 test(ro[Test.MyEnum.enum2] == "Hello!!")
318 test(ro[Test.MyEnum.enum3] == "qwerty")
319 self.called()
321 def opMyStructMyEnumD(self, ro, do):
322 s11 = Test.MyStruct()
323 s11.i = 1
324 s11.j = 1
325 s12 = Test.MyStruct()
326 s12.i = 1
327 s12.j = 2
328 s22 = Test.MyStruct()
329 s22.i = 2
330 s22.j = 2
331 s23 = Test.MyStruct()
332 s23.i = 2
333 s23.j = 3
334 di1 = {s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2}
335 test(do == di1)
336 test(len(ro) == 4)
337 test(ro[s11] == Test.MyEnum.enum1)
338 test(ro[s12] == Test.MyEnum.enum2)
339 test(ro[s22] == Test.MyEnum.enum3)
340 test(ro[s23] == Test.MyEnum.enum2)
341 self.called()
343 def opIntS(self, r):
344 for j in range(0, len(r)):
345 test(r[j] == -j)
346 self.called()
348 def opIdempotent(self):
349 self.called()
351 def opNonmutating(self):
352 self.called()
354 def opDerived(self):
355 self.called()
357 def exCB(self, ex):
358 test(False)
360 def twowaysNewAMI(communicator, p):
361 cb = Callback()
362 p.begin_ice_ping(cb.ping, cb.exCB)
363 cb.check()
365 cb = Callback()
366 p.begin_ice_isA(Test.MyClass.ice_staticId(), cb.isA, cb.exCB)
367 cb.check()
369 cb = Callback()
370 p.begin_ice_id(cb.id, cb.exCB)
371 cb.check()
373 cb = Callback()
374 p.begin_ice_ids(cb.ids, cb.exCB)
375 cb.check()
377 r = p.begin_opVoid()
378 p.end_opVoid(r)
380 cb = Callback()
381 p.begin_opVoid(cb.opVoid, cb.exCB)
382 cb.check()
384 r = p.begin_opByte(0xff, 0x0f)
385 (ret, p3) = p.end_opByte(r)
386 test(p3 == 0xf0)
387 test(ret == 0xff)
389 cb = Callback()
390 p.begin_opByte(0xff, 0x0f, cb.opByte, cb.exCB)
391 cb.check()
393 cb = Callback()
394 p.begin_opBool(True, False, cb.opBool, cb.exCB)
395 cb.check()
397 cb = Callback()
398 p.begin_opShortIntLong(10, 11, 12, cb.opShortIntLong, cb.exCB)
399 cb.check()
401 cb = Callback()
402 p.begin_opFloatDouble(3.14, 1.1E10, cb.opFloatDouble, cb.exCB)
403 cb.check()
405 cb = Callback()
406 p.begin_opString("hello", "world", cb.opString, cb.exCB)
407 cb.check()
409 cb = Callback()
410 p.begin_opMyEnum(Test.MyEnum.enum2, cb.opMyEnum, cb.exCB)
411 cb.check()
413 cb = Callback(communicator)
414 p.begin_opMyClass(p, cb.opMyClass, cb.exCB)
415 cb.check()
417 si1 = Test.Structure()
418 si1.p = p
419 si1.e = Test.MyEnum.enum3
420 si1.s = Test.AnotherStruct()
421 si1.s.s = "abc"
422 si2 = Test.Structure()
423 si2.p = None
424 si2.e = Test.MyEnum.enum2
425 si2.s = Test.AnotherStruct()
426 si2.s.s = "def"
428 cb = Callback(communicator)
429 p.begin_opStruct(si1, si2, cb.opStruct, cb.exCB)
430 cb.check()
432 bsi1 = (0x01, 0x11, 0x12, 0x22)
433 bsi2 = (0xf1, 0xf2, 0xf3, 0xf4)
435 cb = Callback()
436 p.begin_opByteS(bsi1, bsi2, cb.opByteS, cb.exCB)
437 cb.check()
439 bsi1 = (True, True, False)
440 bsi2 = (False,)
442 cb = Callback()
443 p.begin_opBoolS(bsi1, bsi2, cb.opBoolS, cb.exCB)
444 cb.check()
446 ssi = (1, 2, 3)
447 isi = (5, 6, 7, 8)
448 lsi = (10, 30, 20)
450 cb = Callback()
451 p.begin_opShortIntLongS(ssi, isi, lsi, cb.opShortIntLongS, cb.exCB)
452 cb.check()
454 fsi = (3.14, 1.11)
455 dsi = (1.1E10, 1.2E10, 1.3E10)
457 cb = Callback()
458 p.begin_opFloatDoubleS(fsi, dsi, cb.opFloatDoubleS, cb.exCB)
459 cb.check()
461 ssi1 = ('abc', 'de', 'fghi')
462 ssi2 = ('xyz',)
464 cb = Callback()
465 p.begin_opStringS(ssi1, ssi2, cb.opStringS, cb.exCB)
466 cb.check()
468 bsi1 = ((0x01, 0x11, 0x12), (0xff,))
469 bsi2 = ((0x0e,), (0xf2, 0xf1))
471 cb = Callback()
472 p.begin_opByteSS(bsi1, bsi2, cb.opByteSS, cb.exCB)
473 cb.check()
475 fsi = ((3.14,), (1.11,), ())
476 dsi = ((1.1E10, 1.2E10, 1.3E10),)
478 cb = Callback()
479 p.begin_opFloatDoubleSS(fsi, dsi, cb.opFloatDoubleSS, cb.exCB)
480 cb.check()
482 ssi1 = (('abc',), ('de', 'fghi'))
483 ssi2 = ((), (), ('xyz',))
485 cb = Callback()
486 p.begin_opStringSS(ssi1, ssi2, cb.opStringSS, cb.exCB)
487 cb.check()
489 di1 = {10: True, 100: False}
490 di2 = {10: True, 11: False, 101: True}
492 cb = Callback()
493 p.begin_opByteBoolD(di1, di2, cb.opByteBoolD, cb.exCB)
494 cb.check()
496 di1 = {110: -1, 1100: 123123}
497 di2 = {110: -1, 111: -100, 1101: 0}
499 cb = Callback()
500 p.begin_opShortIntD(di1, di2, cb.opShortIntD, cb.exCB)
501 cb.check()
503 di1 = {999999110: -1.1, 999999111: 123123.2}
504 di2 = {999999110: -1.1, 999999120: -100.4, 999999130: 0.5}
506 cb = Callback()
507 p.begin_opLongFloatD(di1, di2, cb.opLongFloatD, cb.exCB)
508 cb.check()
510 di1 = {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
511 di2 = {'foo': 'abc -1.1', 'FOO': 'abc -100.4', 'BAR': 'abc 0.5'}
513 cb = Callback()
514 p.begin_opStringStringD(di1, di2, cb.opStringStringD, cb.exCB)
515 cb.check()
517 di1 = {'abc': Test.MyEnum.enum1, '': Test.MyEnum.enum2}
518 di2 = {'abc': Test.MyEnum.enum1, 'qwerty': Test.MyEnum.enum3, 'Hello!!': Test.MyEnum.enum2}
520 cb = Callback()
521 p.begin_opStringMyEnumD(di1, di2, cb.opStringMyEnumD, cb.exCB)
522 cb.check()
524 di1 = {Test.MyEnum.enum1: 'abc'}
525 di2 = {Test.MyEnum.enum2: 'Hello!!', Test.MyEnum.enum3: 'qwerty'}
527 cb = Callback()
528 p.begin_opMyEnumStringD(di1, di2, cb.opMyEnumStringD, cb.exCB)
529 cb.check()
531 s11 = Test.MyStruct()
532 s11.i = 1
533 s11.j = 1
534 s12 = Test.MyStruct()
535 s12.i = 1
536 s12.j = 2
537 s22 = Test.MyStruct()
538 s22.i = 2
539 s22.j = 2
540 s23 = Test.MyStruct()
541 s23.i = 2
542 s23.j = 3
543 di1 = {s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2}
544 di2 = {s11: Test.MyEnum.enum1, s22: Test.MyEnum.enum3, s23: Test.MyEnum.enum2}
546 cb = Callback()
547 p.begin_opMyStructMyEnumD(di1, di2, cb.opMyStructMyEnumD, cb.exCB)
548 cb.check()
550 lengths = ( 0, 1, 2, 126, 127, 128, 129, 253, 254, 255, 256, 257, 1000 )
551 for l in lengths:
552 s = []
553 for i in range(l):
554 s.append(i)
555 cb = Callback(l)
556 p.begin_opIntS(s, cb.opIntS, cb.exCB)
557 cb.check()
559 ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'}
561 test(len(p.ice_getContext()) == 0)
562 r = p.begin_opContext()
563 c = p.end_opContext(r)
564 test(c != ctx)
566 test(len(p.ice_getContext()) == 0)
567 r = p.begin_opContext(_ctx=ctx)
568 c = p.end_opContext(r)
569 test(c == ctx)
571 p2 = Test.MyClassPrx.checkedCast(p.ice_context(ctx))
572 test(p2.ice_getContext() == ctx)
573 r = p2.begin_opContext()
574 c = p2.end_opContext(r)
575 test(c == ctx)
577 r = p2.begin_opContext(_ctx=ctx)
578 c = p2.end_opContext(r)
579 test(c == ctx)
582 # Test implicit context propagation
584 impls = ( 'Shared', 'PerThread' )
585 for i in impls:
586 initData = Ice.InitializationData()
587 initData.properties = communicator.getProperties().clone()
588 initData.properties.setProperty('Ice.ImplicitContext', i)
589 ic = Ice.initialize(data=initData)
591 ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'}
593 p3 = Test.MyClassPrx.uncheckedCast(ic.stringToProxy("test:default -p 12010"))
595 ic.getImplicitContext().setContext(ctx)
596 test(ic.getImplicitContext().getContext() == ctx)
597 r = p3.begin_opContext()
598 c = p3.end_opContext(r)
599 test(c == ctx)
601 ic.getImplicitContext().put('zero', 'ZERO')
603 ctx = ic.getImplicitContext().getContext()
604 r = p3.begin_opContext()
605 c = p3.end_opContext(r)
606 test(c == ctx)
608 prxContext = {'one': 'UN', 'four': 'QUATRE'}
610 combined = {}
611 combined.update(ctx)
612 combined.update(prxContext)
613 test(combined['one'] == 'UN')
615 p3 = Test.MyClassPrx.uncheckedCast(p3.ice_context(prxContext))
616 ic.getImplicitContext().setContext({})
617 r = p3.begin_opContext()
618 c = p3.end_opContext(r)
619 test(c == prxContext)
621 ic.getImplicitContext().setContext(ctx)
622 r = p3.begin_opContext()
623 c = p3.end_opContext(r)
624 test(c == combined)
626 ic.destroy()
628 cb = Callback()
629 p.begin_opIdempotent(cb.opIdempotent, cb.exCB)
630 cb.check()
632 cb = Callback()
633 p.begin_opNonmutating(cb.opNonmutating, cb.exCB)
634 cb.check()
636 derived = Test.MyDerivedClassPrx.checkedCast(p)
637 test(derived)
638 cb = Callback()
639 derived.begin_opDerived(cb.opDerived, cb.exCB)
640 cb.check()