1#!/usr/local/bin/python3.8
2#
3# Copyright (c) ZeroC, Inc. All rights reserved.
4#
5
6import os, sys, traceback, threading, time, concurrent.futures
7import Ice
8slice_dir = Ice.getSliceDir()
9if not slice_dir:
10    print(sys.argv[0] + ': Slice directory not found.')
11    sys.exit(1)
12
13Ice.loadSlice("'-I" + slice_dir + "' Test.ice")
14import Test
15
16def test(b):
17    if not b:
18        raise RuntimeError('test assertion failed')
19
20class FutureThread(threading.Thread):
21    def __init__(self, f, r):
22        threading.Thread.__init__(self)
23        self.future = f
24        self.result = r
25
26    def run(self):
27        time.sleep(0.05)
28        self.future.set_result(self.result)
29
30class MyDerivedClassI(Test.MyDerivedClass):
31    def __init__(self):
32        self.threads = []
33        self.threadLock = threading.Lock()
34        self.lock = threading.Lock()
35        self.opByteSOnewayCount = 0
36
37    def ice_isA(self, id, current=None):
38        test(current.mode == Ice.OperationMode.Nonmutating)
39        return Test.MyDerivedClass.ice_isA(self, id, current)
40
41    def ice_ping(self, current=None):
42        test(current.mode == Ice.OperationMode.Nonmutating)
43        Test.MyDerivedClass.ice_ping(self, current)
44
45    def ice_ids(self, current=None):
46        test(current.mode == Ice.OperationMode.Nonmutating)
47        return Test.MyDerivedClass.ice_ids(self, current)
48
49    def ice_id(self, current=None):
50        test(current.mode == Ice.OperationMode.Nonmutating)
51        return Test.MyDerivedClass.ice_id(self, current)
52
53    def shutdown(self, current=None):
54        with self.threadLock:
55            for thread in self.threads:
56                thread.join()
57            self.threads = []
58
59        current.adapter.getCommunicator().shutdown()
60
61    def supportsCompress(self, current=None):
62        return True
63
64    def opVoid(self, current=None):
65        test(current.mode == Ice.OperationMode.Normal)
66
67        f = Ice.Future()
68
69        with self.threadLock:
70            thread = FutureThread(f, None)
71            self.threads.append(thread)
72            thread.start()
73
74        return f
75
76    def opByte(self, p1, p2, current=None):
77        # Test the ability to use another Future type
78        f = concurrent.futures.Future()
79        with self.threadLock:
80            thread = FutureThread(f, (p1, p1 ^ p2))
81            self.threads.append(thread)
82            thread.start()
83        return f
84
85    def opBool(self, p1, p2, current=None):
86        return Ice.Future.completed((p2, p1))
87
88    # Test the ability to define a servant method as a coroutine
89    async def opShortIntLong(self, p1, p2, p3, current=None):
90        f = Ice.Future()
91
92        with self.threadLock:
93            thread = FutureThread(f, (p3, p1, p2, p3))
94            self.threads.append(thread)
95            thread.start()
96
97        return await f
98
99    def opFloatDouble(self, p1, p2, current=None):
100        return Ice.Future.completed((p2, p1, p2))
101
102    def opString(self, p1, p2, current=None):
103        return Ice.Future.completed((p1 + " " + p2, p2 + " " + p1))
104
105    def opMyEnum(self, p1, current=None):
106        return Ice.Future.completed((Test.MyEnum.enum3, p1))
107
108    def opMyClass(self, p1, current=None):
109        p2 = p1
110        p3 = Test.MyClassPrx.uncheckedCast(current.adapter.createProxy(Ice.stringToIdentity("noSuchIdentity")))
111        return Ice.Future.completed((Test.MyClassPrx.uncheckedCast(current.adapter.createProxy(current.id)), p2, p3))
112
113    def opStruct(self, p1, p2, current=None):
114        p1.s.s = "a new string"
115        return Ice.Future.completed((p2, p1))
116
117    def opByteS(self, p1, p2, current=None):
118        if sys.version_info[0] == 2:
119            # By default sequence<byte> maps to a string.
120            p3 = map(ord, p1)
121            p3.reverse()
122            r = map(ord, p1)
123            r.extend(map(ord, p2))
124        else:
125            p3 = bytes(reversed(p1))
126            r = p1 + p2
127        return Ice.Future.completed((r, p3))
128
129    def opBoolS(self, p1, p2, current=None):
130        p3 = p1[0:]
131        p3.extend(p2)
132        r = p1[0:]
133        r.reverse();
134        return Ice.Future.completed((r, p3))
135
136    def opShortIntLongS(self, p1, p2, p3, current=None):
137        p4 = p1[0:]
138        p5 = p2[0:]
139        p5.reverse()
140        p6 = p3[0:]
141        p6.extend(p3)
142        return Ice.Future.completed((p3, p4, p5, p6))
143
144    def opFloatDoubleS(self, p1, p2, current=None):
145        p3 = p1[0:]
146        p4 = p2[0:]
147        p4.reverse()
148        r = p2[0:]
149        r.extend(p1)
150        return Ice.Future.completed((r, p3, p4))
151
152    def opStringS(self, p1, p2, current=None):
153        p3 = p1[0:]
154        p3.extend(p2)
155        r = p1[0:]
156        r.reverse()
157        return Ice.Future.completed((r, p3))
158
159    def opByteSS(self, p1, p2, current=None):
160        p3 = p1[0:]
161        p3.reverse()
162        r = p1[0:]
163        r.extend(p2)
164        return Ice.Future.completed((r, p3))
165
166    def opBoolSS(self, p1, p2, current=None):
167        p3 = p1[0:]
168        p3.extend(p2)
169        r = p1[0:]
170        r.reverse()
171        return Ice.Future.completed((r, p3))
172
173    def opShortIntLongSS(self, p1, p2, p3, current=None):
174        p4 = p1[0:]
175        p5 = p2[0:]
176        p5.reverse()
177        p6 = p3[0:]
178        p6.extend(p3)
179        return Ice.Future.completed((p3, p4, p5, p6))
180
181    def opFloatDoubleSS(self, p1, p2, current=None):
182        p3 = p1[0:]
183        p4 = p2[0:]
184        p4.reverse()
185        r = p2[0:]
186        r.extend(p2)
187        return Ice.Future.completed((r, p3, p4))
188
189    def opStringSS(self, p1, p2, current=None):
190        p3 = p1[0:]
191        p3.extend(p2)
192        r = p2[0:]
193        r.reverse()
194        return Ice.Future.completed((r, p3))
195
196    def opStringSSS(self, p1, p2, current=None):
197        p3 = p1[0:]
198        p3.extend(p2)
199        r = p2[0:]
200        r.reverse()
201        return Ice.Future.completed((r, p3))
202
203    def opByteBoolD(self, p1, p2, current=None):
204        p3 = p1.copy()
205        r = p1.copy()
206        r.update(p2)
207        return Ice.Future.completed((r, p3))
208
209    def opShortIntD(self, p1, p2, current=None):
210        p3 = p1.copy()
211        r = p1.copy()
212        r.update(p2)
213        return Ice.Future.completed((r, p3))
214
215    def opLongFloatD(self, p1, p2, current=None):
216        p3 = p1.copy()
217        r = p1.copy()
218        r.update(p2)
219        return Ice.Future.completed((r, p3))
220
221    def opStringStringD(self, p1, p2, current=None):
222        p3 = p1.copy()
223        r = p1.copy()
224        r.update(p2)
225        return Ice.Future.completed((r, p3))
226
227    def opStringMyEnumD(self, p1, p2, current=None):
228        p3 = p1.copy()
229        r = p1.copy()
230        r.update(p2)
231        return Ice.Future.completed((r, p3))
232
233    def opMyEnumStringD(self, p1, p2, current=None):
234        p3 = p1.copy()
235        r = p1.copy()
236        r.update(p2)
237        return Ice.Future.completed((r, p3))
238
239    def opMyStructMyEnumD(self, p1, p2, current=None):
240        p3 = p1.copy()
241        r = p1.copy()
242        r.update(p2)
243        return Ice.Future.completed((r, p3))
244
245    def opByteBoolDS(self, p1, p2, current=None):
246        p3 = p2[0:]
247        p3.extend(p1)
248        r = p1[::-1]
249        return Ice.Future.completed((r, p3))
250
251    def opShortIntDS(self, p1, p2, current=None):
252        p3 = p2[0:]
253        p3.extend(p1)
254        r = p1[::-1]
255        return Ice.Future.completed((r, p3))
256
257    def opLongFloatDS(self, p1, p2, current=None):
258        p3 = p2[0:]
259        p3.extend(p1)
260        r = p1[::-1]
261        return Ice.Future.completed((r, p3))
262
263    def opStringStringDS(self, p1, p2, current=None):
264        p3 = p2[0:]
265        p3.extend(p1)
266        r = p1[::-1]
267        return Ice.Future.completed((r, p3))
268
269    def opStringMyEnumDS(self, p1, p2, current=None):
270        p3 = p2[0:]
271        p3.extend(p1)
272        r = p1[::-1]
273        return Ice.Future.completed((r, p3))
274
275    def opMyEnumStringDS(self, p1, p2, current=None):
276        p3 = p2[0:]
277        p3.extend(p1)
278        r = p1[::-1]
279        return Ice.Future.completed((r, p3))
280
281    def opMyStructMyEnumDS(self, p1, p2, current=None):
282        p3 = p2[0:]
283        p3.extend(p1)
284        r = p1[::-1]
285        return Ice.Future.completed((r, p3))
286
287    def opByteByteSD(self, p1, p2, current=None):
288        p3 = p2.copy()
289        r = p1.copy()
290        r.update(p2)
291        return Ice.Future.completed((r, p3))
292
293    def opBoolBoolSD(self, p1, p2, current=None):
294        p3 = p2.copy()
295        r = p1.copy()
296        r.update(p2)
297        return Ice.Future.completed((r, p3))
298
299    def opShortShortSD(self, p1, p2, current=None):
300        p3 = p2.copy()
301        r = p1.copy()
302        r.update(p2)
303        return Ice.Future.completed((r, p3))
304
305    def opIntIntSD(self, p1, p2, current=None):
306        p3 = p2.copy()
307        r = p1.copy()
308        r.update(p2)
309        return Ice.Future.completed((r, p3))
310
311    def opLongLongSD(self, p1, p2, current=None):
312        p3 = p2.copy()
313        r = p1.copy()
314        r.update(p2)
315        return Ice.Future.completed((r, p3))
316
317    def opStringFloatSD(self, p1, p2, current=None):
318        p3 = p2.copy()
319        r = p1.copy()
320        r.update(p2)
321        return Ice.Future.completed((r, p3))
322
323    def opStringDoubleSD(self, p1, p2, current=None):
324        p3 = p2.copy()
325        r = p1.copy()
326        r.update(p2)
327        return Ice.Future.completed((r, p3))
328
329    def opStringStringSD(self, p1, p2, current=None):
330        p3 = p2.copy()
331        r = p1.copy()
332        r.update(p2)
333        return Ice.Future.completed((r, p3))
334
335    def opMyEnumMyEnumSD(self, p1, p2, current=None):
336        p3 = p2.copy()
337        r = p1.copy()
338        r.update(p2)
339        return Ice.Future.completed((r, p3))
340
341    def opIntS(self, s, current=None):
342        return Ice.Future.completed([-x for x in s])
343
344    def opByteSOneway(self, s, current=None):
345        with self.lock:
346            self.opByteSOnewayCount += 1
347        return Ice.Future.completed(None)
348
349    def opByteSOnewayCallCount(self, current=None):
350        with self.lock:
351            count = self.opByteSOnewayCount
352            self.opByteSOnewayCount = 0
353        return Ice.Future.completed(count)
354
355    def opDoubleMarshaling(self, p1, p2, current=None):
356        d = 1278312346.0 / 13.0;
357        test(p1 == d)
358        for i in p2:
359            test(i == d)
360        return Ice.Future.completed(None)
361
362    def opContext(self, current=None):
363        return Ice.Future.completed(current.ctx)
364
365    def opIdempotent(self, current=None):
366        test(current.mode == Ice.OperationMode.Idempotent)
367        return Ice.Future.completed(None)
368
369    def opNonmutating(self, current=None):
370        test(current.mode == Ice.OperationMode.Nonmutating)
371        return Ice.Future.completed(None)
372
373    def opDerived(self, current=None):
374        return Ice.Future.completed(None)
375
376    def opByte1(self, value, current=None):
377        return Ice.Future.completed(value)
378
379    def opShort1(self, value, current=None):
380        return Ice.Future.completed(value)
381
382    def opInt1(self, value, current=None):
383        return Ice.Future.completed(value)
384
385    def opLong1(self, value, current=None):
386        return Ice.Future.completed(value)
387
388    def opFloat1(self, value, current=None):
389        return Ice.Future.completed(value)
390
391    def opDouble1(self, value, current=None):
392        return Ice.Future.completed(value)
393
394    def opString1(self, value, current=None):
395        return Ice.Future.completed(value)
396
397    def opStringS1(self, value, current=None):
398        return Ice.Future.completed(value)
399
400    def opByteBoolD1(self, value, current=None):
401        return Ice.Future.completed(value)
402
403    def opStringS2(self, value, current=None):
404        return Ice.Future.completed(value)
405
406    def opByteBoolD2(self, value, current=None):
407        return Ice.Future.completed(value)
408
409    def opMyClass1(self, value, current=None):
410        return Ice.Future.completed(value)
411
412    def opMyStruct1(self, value, current=None):
413        return Ice.Future.completed(value)
414
415    def opStringLiterals(self, current=None):
416        return Ice.Future.completed([
417                Test.s0, Test.s1, Test.s2, Test.s3, Test.s4, Test.s5, Test.s6, Test.s7, Test.s8, Test.s9, Test.s10,
418                Test.sw0, Test.sw1, Test.sw2, Test.sw3, Test.sw4, Test.sw5, Test.sw6, Test.sw7, Test.sw8, Test.sw9,
419                Test.sw10,
420                Test.ss0, Test.ss1, Test.ss2, Test.ss3, Test.ss4, Test.ss5,
421                Test.su0, Test.su1, Test.su2])
422
423    def opWStringLiterals(self, current=None):
424        return self.opStringLiterals(current)
425
426    def opMStruct1(self, current):
427        return Ice.Future.completed(Test.MyClass.OpMStruct1MarshaledResult(Test.Structure(), current))
428
429    def opMStruct2(self, p1, current):
430        return Ice.Future.completed(Test.MyClass.OpMStruct2MarshaledResult((p1, p1), current))
431
432    def opMSeq1(self, current):
433        return Ice.Future.completed(Test.MyClass.OpMSeq1MarshaledResult([], current))
434
435    def opMSeq2(self, p1, current):
436        return Ice.Future.completed(Test.MyClass.OpMSeq2MarshaledResult((p1, p1), current))
437
438    def opMDict1(self, current):
439        return Ice.Future.completed(Test.MyClass.OpMDict1MarshaledResult({}, current))
440
441    def opMDict2(self, p1, current):
442        return Ice.Future.completed(Test.MyClass.OpMDict2MarshaledResult((p1, p1), current))
443