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 Callback(CallbackBase
):
37 def __init__(self
, communicator
=None):
38 CallbackBase
.__init
__(self
)
39 self
._communicator
= communicator
49 test(id == "::Test::MyDerivedClass")
59 def opByte(self
, r
, b
):
64 def opBool(self
, r
, b
):
69 def opShortIntLong(self
, r
, s
, i
, l
):
76 def opFloatDouble(self
, r
, f
, d
):
77 test(f
- 3.14 < 0.001)
82 def opString(self
, r
, s
):
83 test(s
== "world hello")
84 test(r
== "hello world")
87 def opMyEnum(self
, r
, e
):
88 test(e
== Test
.MyEnum
.enum2
)
89 test(r
== Test
.MyEnum
.enum3
)
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:
103 except Ice
.ObjectNotExistException
:
107 def opStruct(self
, rso
, so
):
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:
118 def opByteS(self
, rso
, bso
):
120 test(bso
[0] == '\x22')
121 test(bso
[1] == '\x12')
122 test(bso
[2] == '\x11')
123 test(bso
[3] == '\x01')
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')
135 def opBoolS(self
, rso
, bso
):
147 def opShortIntLongS(self
, rso
, sso
, iso
, lso
):
170 def opFloatDoubleS(self
, rso
, fso
, dso
):
172 test(fso
[0] - 3.14 < 0.001)
173 test(fso
[1] - 1.11 < 0.001)
175 test(dso
[0] == 1.3E10
)
176 test(dso
[1] == 1.2E10
)
177 test(dso
[2] == 1.1E10
)
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)
186 def opStringS(self
, rso
, sso
):
188 test(sso
[0] == "abc")
190 test(sso
[2] == "fghi")
191 test(sso
[3] == "xyz")
193 test(rso
[0] == "fghi")
195 test(rso
[2] == "abc")
198 def opByteSS(self
, rso
, bso
):
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')
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')
220 def opFloatDoubleSS(self
, rso
, fso
, dso
):
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)
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
)
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
)
243 def opStringSS(self
, rso
, sso
):
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")
255 test(len(rso
[0]) == 1)
256 test(rso
[0][0] == "xyz")
257 test(len(rso
[1]) == 0)
258 test(len(rso
[2]) == 0)
261 def opByteBoolD(self
, ro
, do
):
262 di1
= {10: True, 100: False}
271 def opShortIntD(self
, ro
, do
):
272 di1
= {110: -1, 1100: 123123}
276 test(ro
[111] == -100)
277 test(ro
[1100] == 123123)
281 def opLongFloatD(self
, ro
, do
):
282 di1
= {999999110: -1.1, 999999111: 123123.2}
284 test(math
.fabs(do
[k
] - di1
[k
]) < 0.01)
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)
292 def opStringStringD(self
, ro
, do
):
293 di1
= {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
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")
302 def opStringMyEnumD(self
, ro
, do
):
303 di1
= {'abc': Test
.MyEnum
.enum1
, '': Test
.MyEnum
.enum2
}
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
)
312 def opMyEnumStringD(self
, ro
, do
):
313 di1
= {Test
.MyEnum
.enum1
: 'abc'}
316 test(ro
[Test
.MyEnum
.enum1
] == "abc")
317 test(ro
[Test
.MyEnum
.enum2
] == "Hello!!")
318 test(ro
[Test
.MyEnum
.enum3
] == "qwerty")
321 def opMyStructMyEnumD(self
, ro
, do
):
322 s11
= Test
.MyStruct()
325 s12
= Test
.MyStruct()
328 s22
= Test
.MyStruct()
331 s23
= Test
.MyStruct()
334 di1
= {s11
: Test
.MyEnum
.enum1
, s12
: Test
.MyEnum
.enum2
}
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
)
344 for j
in range(0, len(r
)):
348 def opIdempotent(self
):
351 def opNonmutating(self
):
360 def twowaysNewAMI(communicator
, p
):
362 p
.begin_ice_ping(cb
.ping
, cb
.exCB
)
366 p
.begin_ice_isA(Test
.MyClass
.ice_staticId(), cb
.isA
, cb
.exCB
)
370 p
.begin_ice_id(cb
.id, cb
.exCB
)
374 p
.begin_ice_ids(cb
.ids
, cb
.exCB
)
381 p
.begin_opVoid(cb
.opVoid
, cb
.exCB
)
384 r
= p
.begin_opByte(0xff, 0x0f)
385 (ret
, p3
) = p
.end_opByte(r
)
390 p
.begin_opByte(0xff, 0x0f, cb
.opByte
, cb
.exCB
)
394 p
.begin_opBool(True, False, cb
.opBool
, cb
.exCB
)
398 p
.begin_opShortIntLong(10, 11, 12, cb
.opShortIntLong
, cb
.exCB
)
402 p
.begin_opFloatDouble(3.14, 1.1E10
, cb
.opFloatDouble
, cb
.exCB
)
406 p
.begin_opString("hello", "world", cb
.opString
, cb
.exCB
)
410 p
.begin_opMyEnum(Test
.MyEnum
.enum2
, cb
.opMyEnum
, cb
.exCB
)
413 cb
= Callback(communicator
)
414 p
.begin_opMyClass(p
, cb
.opMyClass
, cb
.exCB
)
417 si1
= Test
.Structure()
419 si1
.e
= Test
.MyEnum
.enum3
420 si1
.s
= Test
.AnotherStruct()
422 si2
= Test
.Structure()
424 si2
.e
= Test
.MyEnum
.enum2
425 si2
.s
= Test
.AnotherStruct()
428 cb
= Callback(communicator
)
429 p
.begin_opStruct(si1
, si2
, cb
.opStruct
, cb
.exCB
)
432 bsi1
= (0x01, 0x11, 0x12, 0x22)
433 bsi2
= (0xf1, 0xf2, 0xf3, 0xf4)
436 p
.begin_opByteS(bsi1
, bsi2
, cb
.opByteS
, cb
.exCB
)
439 bsi1
= (True, True, False)
443 p
.begin_opBoolS(bsi1
, bsi2
, cb
.opBoolS
, cb
.exCB
)
451 p
.begin_opShortIntLongS(ssi
, isi
, lsi
, cb
.opShortIntLongS
, cb
.exCB
)
455 dsi
= (1.1E10
, 1.2E10
, 1.3E10
)
458 p
.begin_opFloatDoubleS(fsi
, dsi
, cb
.opFloatDoubleS
, cb
.exCB
)
461 ssi1
= ('abc', 'de', 'fghi')
465 p
.begin_opStringS(ssi1
, ssi2
, cb
.opStringS
, cb
.exCB
)
468 bsi1
= ((0x01, 0x11, 0x12), (0xff,))
469 bsi2
= ((0x0e,), (0xf2, 0xf1))
472 p
.begin_opByteSS(bsi1
, bsi2
, cb
.opByteSS
, cb
.exCB
)
475 fsi
= ((3.14,), (1.11,), ())
476 dsi
= ((1.1E10
, 1.2E10
, 1.3E10
),)
479 p
.begin_opFloatDoubleSS(fsi
, dsi
, cb
.opFloatDoubleSS
, cb
.exCB
)
482 ssi1
= (('abc',), ('de', 'fghi'))
483 ssi2
= ((), (), ('xyz',))
486 p
.begin_opStringSS(ssi1
, ssi2
, cb
.opStringSS
, cb
.exCB
)
489 di1
= {10: True, 100: False}
490 di2
= {10: True, 11: False, 101: True}
493 p
.begin_opByteBoolD(di1
, di2
, cb
.opByteBoolD
, cb
.exCB
)
496 di1
= {110: -1, 1100: 123123}
497 di2
= {110: -1, 111: -100, 1101: 0}
500 p
.begin_opShortIntD(di1
, di2
, cb
.opShortIntD
, cb
.exCB
)
503 di1
= {999999110: -1.1, 999999111: 123123.2}
504 di2
= {999999110: -1.1, 999999120: -100.4, 999999130: 0.5}
507 p
.begin_opLongFloatD(di1
, di2
, cb
.opLongFloatD
, cb
.exCB
)
510 di1
= {'foo': 'abc -1.1', 'bar': 'abc 123123.2'}
511 di2
= {'foo': 'abc -1.1', 'FOO': 'abc -100.4', 'BAR': 'abc 0.5'}
514 p
.begin_opStringStringD(di1
, di2
, cb
.opStringStringD
, cb
.exCB
)
517 di1
= {'abc': Test
.MyEnum
.enum1
, '': Test
.MyEnum
.enum2
}
518 di2
= {'abc': Test
.MyEnum
.enum1
, 'qwerty': Test
.MyEnum
.enum3
, 'Hello!!': Test
.MyEnum
.enum2
}
521 p
.begin_opStringMyEnumD(di1
, di2
, cb
.opStringMyEnumD
, cb
.exCB
)
524 di1
= {Test
.MyEnum
.enum1
: 'abc'}
525 di2
= {Test
.MyEnum
.enum2
: 'Hello!!', Test
.MyEnum
.enum3
: 'qwerty'}
528 p
.begin_opMyEnumStringD(di1
, di2
, cb
.opMyEnumStringD
, cb
.exCB
)
531 s11
= Test
.MyStruct()
534 s12
= Test
.MyStruct()
537 s22
= Test
.MyStruct()
540 s23
= Test
.MyStruct()
543 di1
= {s11
: Test
.MyEnum
.enum1
, s12
: Test
.MyEnum
.enum2
}
544 di2
= {s11
: Test
.MyEnum
.enum1
, s22
: Test
.MyEnum
.enum3
, s23
: Test
.MyEnum
.enum2
}
547 p
.begin_opMyStructMyEnumD(di1
, di2
, cb
.opMyStructMyEnumD
, cb
.exCB
)
550 lengths
= ( 0, 1, 2, 126, 127, 128, 129, 253, 254, 255, 256, 257, 1000 )
556 p
.begin_opIntS(s
, cb
.opIntS
, cb
.exCB
)
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
)
566 test(len(p
.ice_getContext()) == 0)
567 r
= p
.begin_opContext(_ctx
=ctx
)
568 c
= p
.end_opContext(r
)
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
)
577 r
= p2
.begin_opContext(_ctx
=ctx
)
578 c
= p2
.end_opContext(r
)
582 # Test implicit context propagation
584 impls
= ( 'Shared', 'PerThread' )
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
)
601 ic
.getImplicitContext().put('zero', 'ZERO')
603 ctx
= ic
.getImplicitContext().getContext()
604 r
= p3
.begin_opContext()
605 c
= p3
.end_opContext(r
)
608 prxContext
= {'one': 'UN', 'four': 'QUATRE'}
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
)
629 p
.begin_opIdempotent(cb
.opIdempotent
, cb
.exCB
)
633 p
.begin_opNonmutating(cb
.opNonmutating
, cb
.exCB
)
636 derived
= Test
.MyDerivedClassPrx
.checkedCast(p
)
639 derived
.begin_opDerived(cb
.opDerived
, cb
.exCB
)