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
14 raise RuntimeError('test assertion failed')
19 self
._cond
= threading
.Condition()
24 while not self
._called
:
36 class AMI_MyClass_opVoidI(CallbackBase
):
38 CallbackBase
.__init
__(self
)
40 def ice_response(self
):
43 def ice_exception(self
, ex
):
46 class AMI_MyClass_opVoidExI(CallbackBase
):
48 CallbackBase
.__init
__(self
)
50 def ice_response(self
):
53 def ice_exception(self
, ex
):
54 test(isinstance(ex
, Ice
.NoEndpointException
))
57 class AMI_MyClass_opByteI(CallbackBase
):
59 CallbackBase
.__init
__(self
)
61 def ice_response(self
, r
, b
):
66 def ice_exception(self
, ex
):
69 class AMI_MyClass_opByteExI(CallbackBase
):
71 CallbackBase
.__init
__(self
)
73 def ice_response(self
, r
, b
):
76 def ice_exception(self
, ex
):
77 test(isinstance(ex
, Ice
.NoEndpointException
))
80 class AMI_MyClass_opBoolI(CallbackBase
):
82 CallbackBase
.__init
__(self
)
84 def ice_response(self
, r
, b
):
89 def ice_exception(self
, ex
):
92 class AMI_MyClass_opShortIntLongI(CallbackBase
):
94 CallbackBase
.__init
__(self
)
96 def ice_response(self
, r
, s
, i
, l
):
103 def ice_exception(self
, ex
):
106 class AMI_MyClass_opFloatDoubleI(CallbackBase
):
108 CallbackBase
.__init
__(self
)
110 def ice_response(self
, r
, f
, d
):
111 test(f
- 3.14 < 0.001)
116 def ice_exception(self
, ex
):
119 class AMI_MyClass_opStringI(CallbackBase
):
121 CallbackBase
.__init
__(self
)
123 def ice_response(self
, r
, s
):
124 test(s
== "world hello")
125 test(r
== "hello world")
128 def ice_exception(self
, ex
):
131 class AMI_MyClass_opMyEnumI(CallbackBase
):
133 CallbackBase
.__init
__(self
)
135 def ice_response(self
, r
, e
):
136 test(e
== Test
.MyEnum
.enum2
)
137 test(r
== Test
.MyEnum
.enum3
)
140 def ice_exception(self
, ex
):
143 class AMI_MyClass_opMyClassI(CallbackBase
):
144 def __init__(self
, communicator
):
145 CallbackBase
.__init
__(self
)
146 self
._communicator
= communicator
148 def ice_response(self
, r
, c1
, c2
):
149 test(c1
.ice_getIdentity() == self
._communicator
.stringToIdentity("test"))
150 test(c2
.ice_getIdentity() == self
._communicator
.stringToIdentity("noSuchIdentity"))
151 test(r
.ice_getIdentity() == self
._communicator
.stringToIdentity("test"))
152 # We can't do the callbacks below in serialize mode
153 if self
._communicator
.getProperties().getPropertyAsInt("Ice.Client.ThreadPool.Serialize") == 0:
159 except Ice
.ObjectNotExistException
:
163 def ice_exception(self
, ex
):
166 class AMI_MyClass_opStructI(CallbackBase
):
168 def __init__(self
, communicator
):
169 CallbackBase
.__init
__(self
)
170 self
._communicator
= communicator
172 def ice_response(self
, rso
, so
):
174 test(rso
.e
== Test
.MyEnum
.enum2
)
175 test(rso
.s
.s
== "def")
176 test(so
.e
== Test
.MyEnum
.enum3
)
177 test(so
.s
.s
== "a new string")
178 # We can't do the callbacks below in serialize mode.
179 if self
._communicator
.getProperties().getPropertyAsInt("Ice.ThreadPool.Client.Serialize") == 0:
183 def ice_exception(self
, ex
):
186 class AMI_MyClass_opByteSI(CallbackBase
):
188 CallbackBase
.__init
__(self
)
190 def ice_response(self
, rso
, bso
):
192 test(bso
[0] == '\x22')
193 test(bso
[1] == '\x12')
194 test(bso
[2] == '\x11')
195 test(bso
[3] == '\x01')
197 test(rso
[0] == '\x01')
198 test(rso
[1] == '\x11')
199 test(rso
[2] == '\x12')
200 test(rso
[3] == '\x22')
201 test(rso
[4] == '\xf1')
202 test(rso
[5] == '\xf2')
203 test(rso
[6] == '\xf3')
204 test(rso
[7] == '\xf4')
207 def ice_exception(self
, ex
):
210 class AMI_MyClass_opBoolSI(CallbackBase
):
212 CallbackBase
.__init
__(self
)
214 def ice_response(self
, rso
, bso
):
226 def ice_exception(self
, ex
):
229 class AMI_MyClass_opShortIntLongSI(CallbackBase
):
231 CallbackBase
.__init
__(self
)
233 def ice_response(self
, rso
, sso
, iso
, lso
):
256 def ice_exception(self
, ex
):
259 class AMI_MyClass_opFloatDoubleSI(CallbackBase
):
261 CallbackBase
.__init
__(self
)
263 def ice_response(self
, rso
, fso
, dso
):
265 test(fso
[0] - 3.14 < 0.001)
266 test(fso
[1] - 1.11 < 0.001)
268 test(dso
[0] == 1.3E10
)
269 test(dso
[1] == 1.2E10
)
270 test(dso
[2] == 1.1E10
)
272 test(rso
[0] == 1.1E10
)
273 test(rso
[1] == 1.2E10
)
274 test(rso
[2] == 1.3E10
)
275 test(rso
[3] - 3.14 < 0.001)
276 test(rso
[4] - 1.11 < 0.001)
279 def ice_exception(self
, ex
):
282 class AMI_MyClass_opStringSI(CallbackBase
):
284 CallbackBase
.__init
__(self
)
286 def ice_response(self
, rso
, sso
):
288 test(sso
[0] == "abc")
290 test(sso
[2] == "fghi")
291 test(sso
[3] == "xyz")
293 test(rso
[0] == "fghi")
295 test(rso
[2] == "abc")
298 def ice_exception(self
, ex
):
301 class AMI_MyClass_opByteSSI(CallbackBase
):
303 CallbackBase
.__init
__(self
)
305 def ice_response(self
, rso
, bso
):
307 test(len(bso
[0]) == 1)
308 test(bso
[0][0] == '\xff')
309 test(len(bso
[1]) == 3)
310 test(bso
[1][0] == '\x01')
311 test(bso
[1][1] == '\x11')
312 test(bso
[1][2] == '\x12')
314 test(len(rso
[0]) == 3)
315 test(rso
[0][0] == '\x01')
316 test(rso
[0][1] == '\x11')
317 test(rso
[0][2] == '\x12')
318 test(len(rso
[1]) == 1)
319 test(rso
[1][0] == '\xff')
320 test(len(rso
[2]) == 1)
321 test(rso
[2][0] == '\x0e')
322 test(len(rso
[3]) == 2)
323 test(rso
[3][0] == '\xf2')
324 test(rso
[3][1] == '\xf1')
327 def ice_exception(self
, ex
):
330 class AMI_MyClass_opFloatDoubleSSI(CallbackBase
):
332 CallbackBase
.__init
__(self
)
334 def ice_response(self
, rso
, fso
, dso
):
336 test(len(fso
[0]) == 1)
337 test(fso
[0][0] - 3.14 < 0.001)
338 test(len(fso
[1]) == 1)
339 test(fso
[1][0] - 1.11 < 0.001)
340 test(len(fso
[2]) == 0)
342 test(len(dso
[0]) == 3)
343 test(dso
[0][0] == 1.1E10
)
344 test(dso
[0][1] == 1.2E10
)
345 test(dso
[0][2] == 1.3E10
)
347 test(len(rso
[0]) == 3)
348 test(rso
[0][0] == 1.1E10
)
349 test(rso
[0][1] == 1.2E10
)
350 test(rso
[0][2] == 1.3E10
)
351 test(len(rso
[1]) == 3)
352 test(rso
[1][0] == 1.1E10
)
353 test(rso
[1][1] == 1.2E10
)
354 test(rso
[1][2] == 1.3E10
)
357 def ice_exception(self
, ex
):
360 class AMI_MyClass_opStringSSI(CallbackBase
):
362 CallbackBase
.__init
__(self
)
364 def ice_response(self
, rso
, sso
):
366 test(len(sso
[0]) == 1)
367 test(sso
[0][0] == "abc")
368 test(len(sso
[1]) == 2)
369 test(sso
[1][0] == "de")
370 test(sso
[1][1] == "fghi")
371 test(len(sso
[2]) == 0)
372 test(len(sso
[3]) == 0)
373 test(len(sso
[4]) == 1)
374 test(sso
[4][0] == "xyz")
376 test(len(rso
[0]) == 1)
377 test(rso
[0][0] == "xyz")
378 test(len(rso
[1]) == 0)
379 test(len(rso
[2]) == 0)
382 def ice_exception(self
, ex
):
385 class AMI_MyClass_opByteBoolDI(CallbackBase
):
387 CallbackBase
.__init
__(self
)
389 def ice_response(self
, ro
, do
):
390 di1
= {10: True, 100: False}
399 def ice_exception(self
, ex
):
402 class AMI_MyClass_opShortIntDI(CallbackBase
):
404 CallbackBase
.__init
__(self
)
406 def ice_response(self
, ro
, do
):
407 di1
= {110: -1, 1100: 123123}
411 test(ro
[111] == -100)
412 test(ro
[1100] == 123123)
416 def ice_exception(self
, ex
):
419 class AMI_MyClass_opLongFloatDI(CallbackBase
):
421 CallbackBase
.__init
__(self
)
423 def ice_response(self
, ro
, do
):
424 di1
= {999999110: -1.1, 999999111: 123123.2}
426 test(math
.fabs(do
[k
] - di1
[k
]) < 0.01)
428 test(ro
[999999110] - -1.1 < 0.01)
429 test(ro
[999999120] - -100.4 < 0.01)
430 test(ro
[999999111] - 123123.2 < 0.01)
431 test(ro
[999999130] - 0.5 < 0.01)
434 def ice_exception(self
, ex
):
437 class AMI_MyClass_opStringStringDI(CallbackBase
):
439 CallbackBase
.__init
__(self
)
441 def ice_response(self
, ro
, do
):
442 di1
= {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
445 test(ro
["foo"] == "abc -1.1")
446 test(ro
["FOO"] == "abc -100.4")
447 test(ro
["bar"] == "abc 123123.2")
448 test(ro
["BAR"] == "abc 0.5")
451 def ice_exception(self
, ex
):
454 class AMI_MyClass_opStringMyEnumDI(CallbackBase
):
456 CallbackBase
.__init
__(self
)
458 def ice_response(self
, ro
, do
):
459 di1
= {'abc': Test
.MyEnum
.enum1
, '': Test
.MyEnum
.enum2
}
462 test(ro
["abc"] == Test
.MyEnum
.enum1
)
463 test(ro
["qwerty"] == Test
.MyEnum
.enum3
)
464 test(ro
[""] == Test
.MyEnum
.enum2
)
465 test(ro
["Hello!!"] == Test
.MyEnum
.enum2
)
468 def ice_exception(self
, ex
):
471 class AMI_MyClass_opMyEnumStringDI(CallbackBase
):
473 CallbackBase
.__init
__(self
)
475 def ice_response(self
, ro
, do
):
476 di1
= {Test
.MyEnum
.enum1
: 'abc'}
479 test(ro
[Test
.MyEnum
.enum1
] == "abc")
480 test(ro
[Test
.MyEnum
.enum2
] == "Hello!!")
481 test(ro
[Test
.MyEnum
.enum3
] == "qwerty")
484 def ice_exception(self
, ex
):
487 class AMI_MyClass_opMyStructMyEnumDI(CallbackBase
):
489 CallbackBase
.__init
__(self
)
491 def ice_response(self
, ro
, do
):
492 s11
= Test
.MyStruct()
495 s12
= Test
.MyStruct()
498 s22
= Test
.MyStruct()
501 s23
= Test
.MyStruct()
504 di1
= {s11
: Test
.MyEnum
.enum1
, s12
: Test
.MyEnum
.enum2
}
507 test(ro
[s11
] == Test
.MyEnum
.enum1
)
508 test(ro
[s12
] == Test
.MyEnum
.enum2
)
509 test(ro
[s22
] == Test
.MyEnum
.enum3
)
510 test(ro
[s23
] == Test
.MyEnum
.enum2
)
513 def ice_exception(self
, ex
):
516 class AMI_MyClass_opIntSI(CallbackBase
):
517 def __init__(self
, l
):
518 CallbackBase
.__init
__(self
)
521 def ice_response(self
, r
):
522 test(len(r
) == self
._l)
523 for j
in range(0, self
._l):
527 def ice_exception(self
, ex
):
530 class AMI_MyClass_opContextEqualI(CallbackBase
):
531 def __init__(self
, d
):
532 CallbackBase
.__init
__(self
)
535 def ice_response(self
, r
):
539 def ice_exception(self
, ex
):
542 class AMI_MyClass_opContextNotEqualI(CallbackBase
):
543 def __init__(self
, d
):
544 CallbackBase
.__init
__(self
)
547 def ice_response(self
, r
):
551 def ice_exception(self
, ex
):
554 class AMI_MyClass_opIdempotentI(CallbackBase
):
556 CallbackBase
.__init
__(self
)
558 def ice_response(self
):
561 def ice_exception(self
, ex
):
564 class AMI_MyClass_opNonmutatingI(CallbackBase
):
566 CallbackBase
.__init
__(self
)
568 def ice_response(self
):
571 def ice_exception(self
, ex
):
574 class AMI_MyDerivedClass_opDerivedI(CallbackBase
):
576 CallbackBase
.__init
__(self
)
578 def ice_response(self
):
581 def ice_exception(self
, ex
):
584 def twowaysAMI(communicator
, p
):
585 # Check that a call to a void operation raises NoEndpointException
586 # in the ice_exception() callback instead of at the point of call.
587 indirect
= Test
.MyClassPrx
.uncheckedCast(p
.ice_adapterId("dummy"))
588 cb
= AMI_MyClass_opVoidExI()
590 test(not indirect
.opVoid_async(cb
))
591 except Ice
.Exception:
595 # Check that a call to a twoway operation raises NoEndpointException
596 # in the ice_exception() callback instead of at the point of call.
597 indirect
= Test
.MyClassPrx
.uncheckedCast(p
.ice_adapterId("dummy"))
598 cb
= AMI_MyClass_opByteExI()
600 test(not indirect
.opByte_async(cb
, 0, 0))
601 except Ice
.Exception:
608 cb
= AMI_MyClass_opVoidI()
611 # Let's check if we can reuse the same callback object for another call.
615 # Check that CommunicatorDestroyedException is raised directly.
616 initData
= Ice
.InitializationData()
617 initData
.properties
= communicator
.getProperties().clone()
618 ic
= Ice
.initialize(initData
)
619 obj
= ic
.stringToProxy(p
.ice_toString())
620 p2
= Test
.MyClassPrx
.checkedCast(obj
)
624 cb
= AMI_MyClass_opVoidI()
626 test(not p2
.opVoid_async(cb
))
628 except Ice
.CommunicatorDestroyedException
:
634 cb
= AMI_MyClass_opByteI()
635 p
.opByte_async(cb
, 0xff, 0x0f)
641 cb
= AMI_MyClass_opBoolI()
642 p
.opBool_async(cb
, True, False)
648 cb
= AMI_MyClass_opShortIntLongI()
649 p
.opShortIntLong_async(cb
, 10, 11, 12)
655 cb
= AMI_MyClass_opFloatDoubleI()
656 p
.opFloatDouble_async(cb
, 3.14, 1.1E10
)
658 # Let's check if we can reuse the same callback object for another call.
659 p
.opFloatDouble_async(cb
, 3.14, 1.1E10
)
665 cb
= AMI_MyClass_opStringI()
666 p
.opString_async(cb
, "hello", "world")
672 cb
= AMI_MyClass_opMyEnumI()
673 p
.opMyEnum_async(cb
, Test
.MyEnum
.enum2
)
679 cb
= AMI_MyClass_opMyClassI(communicator
)
680 p
.opMyClass_async(cb
, p
)
686 si1
= Test
.Structure()
688 si1
.e
= Test
.MyEnum
.enum3
689 si1
.s
= Test
.AnotherStruct()
691 si2
= Test
.Structure()
693 si2
.e
= Test
.MyEnum
.enum2
694 si2
.s
= Test
.AnotherStruct()
697 cb
= AMI_MyClass_opStructI(communicator
)
698 p
.opStruct_async(cb
, si1
, si2
)
704 bsi1
= (0x01, 0x11, 0x12, 0x22)
705 bsi2
= (0xf1, 0xf2, 0xf3, 0xf4)
707 cb
= AMI_MyClass_opByteSI()
708 p
.opByteS_async(cb
, bsi1
, bsi2
)
714 bsi1
= (True, True, False)
717 cb
= AMI_MyClass_opBoolSI()
718 p
.opBoolS_async(cb
, bsi1
, bsi2
)
728 cb
= AMI_MyClass_opShortIntLongSI()
729 p
.opShortIntLongS_async(cb
, ssi
, isi
, lsi
)
736 dsi
= (1.1E10
, 1.2E10
, 1.3E10
)
738 cb
= AMI_MyClass_opFloatDoubleSI()
739 p
.opFloatDoubleS_async(cb
, fsi
, dsi
)
745 ssi1
= ('abc', 'de', 'fghi')
748 cb
= AMI_MyClass_opStringSI()
749 p
.opStringS_async(cb
, ssi1
, ssi2
)
755 bsi1
= ((0x01, 0x11, 0x12), (0xff,))
756 bsi2
= ((0x0e,), (0xf2, 0xf1))
758 cb
= AMI_MyClass_opByteSSI()
759 p
.opByteSS_async(cb
, bsi1
, bsi2
)
765 fsi
= ((3.14,), (1.11,), ())
766 dsi
= ((1.1E10
, 1.2E10
, 1.3E10
),)
768 cb
= AMI_MyClass_opFloatDoubleSSI()
769 p
.opFloatDoubleSS_async(cb
, fsi
, dsi
)
775 ssi1
= (('abc',), ('de', 'fghi'))
776 ssi2
= ((), (), ('xyz',))
778 cb
= AMI_MyClass_opStringSSI()
779 p
.opStringSS_async(cb
, ssi1
, ssi2
)
785 di1
= {10: True, 100: False}
786 di2
= {10: True, 11: False, 101: True}
788 cb
= AMI_MyClass_opByteBoolDI()
789 p
.opByteBoolD_async(cb
, di1
, di2
)
795 di1
= {110: -1, 1100: 123123}
796 di2
= {110: -1, 111: -100, 1101: 0}
798 cb
= AMI_MyClass_opShortIntDI()
799 p
.opShortIntD_async(cb
, di1
, di2
)
805 di1
= {999999110: -1.1, 999999111: 123123.2}
806 di2
= {999999110: -1.1, 999999120: -100.4, 999999130: 0.5}
808 cb
= AMI_MyClass_opLongFloatDI()
809 p
.opLongFloatD_async(cb
, di1
, di2
)
815 di1
= {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
816 di2
= {'foo': 'abc -1.1', 'FOO': 'abc -100.4', 'BAR': 'abc 0.5'}
818 cb
= AMI_MyClass_opStringStringDI()
819 p
.opStringStringD_async(cb
, di1
, di2
)
825 di1
= {'abc': Test
.MyEnum
.enum1
, '': Test
.MyEnum
.enum2
}
826 di2
= {'abc': Test
.MyEnum
.enum1
, 'qwerty': Test
.MyEnum
.enum3
, 'Hello!!': Test
.MyEnum
.enum2
}
828 cb
= AMI_MyClass_opStringMyEnumDI()
829 p
.opStringMyEnumD_async(cb
, di1
, di2
)
835 di1
= {Test
.MyEnum
.enum1
: 'abc'}
836 di2
= {Test
.MyEnum
.enum2
: 'Hello!!', Test
.MyEnum
.enum3
: 'qwerty'}
838 cb
= AMI_MyClass_opMyEnumStringDI()
839 p
.opMyEnumStringD_async(cb
, di1
, di2
)
845 s11
= Test
.MyStruct()
848 s12
= Test
.MyStruct()
851 s22
= Test
.MyStruct()
854 s23
= Test
.MyStruct()
857 di1
= {s11
: Test
.MyEnum
.enum1
, s12
: Test
.MyEnum
.enum2
}
858 di2
= {s11
: Test
.MyEnum
.enum1
, s22
: Test
.MyEnum
.enum3
, s23
: Test
.MyEnum
.enum2
}
860 cb
= AMI_MyClass_opMyStructMyEnumDI()
861 p
.opMyStructMyEnumD_async(cb
, di1
, di2
)
867 lengths
= ( 0, 1, 2, 126, 127, 128, 129, 253, 254, 255, 256, 257, 1000 )
872 cb
= AMI_MyClass_opIntSI(l
)
873 p
.opIntS_async(cb
, s
)
879 ctx
= {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'}
881 test(len(p
.ice_getContext()) == 0)
882 cb
= AMI_MyClass_opContextNotEqualI(ctx
)
883 p
.opContext_async(cb
)
886 test(len(p
.ice_getContext()) == 0)
887 cb
= AMI_MyClass_opContextEqualI(ctx
)
888 p
.opContext_async(cb
, ctx
)
891 p2
= Test
.MyClassPrx
.checkedCast(p
.ice_context(ctx
))
892 test(p2
.ice_getContext() == ctx
)
893 cb
= AMI_MyClass_opContextEqualI(ctx
)
894 p2
.opContext_async(cb
)
897 p2
= Test
.MyClassPrx
.checkedCast(p
.ice_context(ctx
))
898 test(p2
.ice_getContext() == ctx
)
899 cb
= AMI_MyClass_opContextEqualI(ctx
)
900 p2
.opContext_async(cb
, ctx
)
904 # Test implicit context propagation
906 impls
= ( 'Shared', 'PerThread' )
908 initData
= Ice
.InitializationData()
909 initData
.properties
= communicator
.getProperties().clone()
910 initData
.properties
.setProperty('Ice.ImplicitContext', i
)
911 ic
= Ice
.initialize(data
=initData
)
913 ctx
= {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'}
915 p3
= Test
.MyClassPrx
.uncheckedCast(ic
.stringToProxy("test:default -p 12010"))
917 ic
.getImplicitContext().setContext(ctx
)
918 test(ic
.getImplicitContext().getContext() == ctx
)
920 cb
= AMI_MyClass_opContextEqualI(ctx
)
921 p3
.opContext_async(cb
)
924 ic
.getImplicitContext().put('zero', 'ZERO')
925 ctx
= ic
.getImplicitContext().getContext()
927 cb
= AMI_MyClass_opContextEqualI(ctx
)
928 p3
.opContext_async(cb
)
931 prxContext
= {'one': 'UN', 'four': 'QUATRE'}
934 combined
.update(prxContext
)
935 test(combined
['one'] == 'UN')
937 p3
= Test
.MyClassPrx
.uncheckedCast(p3
.ice_context(prxContext
))
938 ic
.getImplicitContext().setContext({})
940 cb
= AMI_MyClass_opContextEqualI(prxContext
)
941 p3
.opContext_async(cb
)
944 ic
.getImplicitContext().setContext(ctx
)
946 cb
= AMI_MyClass_opContextEqualI(combined
)
947 p3
.opContext_async(cb
)
955 cb
= AMI_MyClass_opIdempotentI()
956 p
.opIdempotent_async(cb
)
962 cb
= AMI_MyClass_opNonmutatingI()
963 p
.opNonmutating_async(cb
)
966 derived
= Test
.MyDerivedClassPrx
.checkedCast(p
)
968 cb
= AMI_MyDerivedClass_opDerivedI()
969 derived
.opDerived_async(cb
)