1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 package test.Ice.ami.lambda;
6 
7 import java.io.PrintWriter;
8 
9 import test.Ice.ami.Test.TestIntfPrx;
10 import test.Ice.ami.Test.TestIntfPrxHelper;
11 import test.Ice.ami.Test.TestIntfControllerPrx;
12 import test.Ice.ami.Test.TestIntfControllerPrxHelper;
13 import test.Ice.ami.Test.PingReplyPrx;
14 import test.Ice.ami.Test.PingReplyPrxHelper;
15 import test.Ice.ami.Test.TestIntfException;
16 import test.Ice.ami.Test.Callback_TestIntf_op;
17 import test.Ice.ami.Test.Callback_TestIntf_opWithResult;
18 import test.Ice.ami.Test.Callback_TestIntf_opWithUE;
19 import test.Ice.ami.Test.Callback_TestIntf_opWithPayload;
20 
21 public class AMI
22 {
23     private static void
test(boolean b)24     test(boolean b)
25     {
26         if(!b)
27         {
28             throw new RuntimeException();
29         }
30     }
31 
32     public static class PingReplyI extends test.Ice.ami.Test._PingReplyDisp
33     {
34         @Override
reply(Ice.Current current)35         public void reply(Ice.Current current)
36         {
37             _received = true;
38         }
39 
checkReceived()40         public boolean checkReceived()
41         {
42             return _received;
43         }
44 
45         private boolean _received = false;
46     }
47 
48     private static class CallbackBase
49     {
CallbackBase()50         CallbackBase()
51         {
52             _called = false;
53         }
54 
check()55         public synchronized void check()
56         {
57             while(!_called)
58             {
59                 try
60                 {
61                     wait();
62                 }
63                 catch(InterruptedException ex)
64                 {
65                 }
66             }
67 
68             _called = false;
69         }
70 
called()71         public synchronized void called()
72         {
73             assert(!_called);
74             _called = true;
75             notify();
76         }
77 
78         private boolean _called;
79     }
80 
81     static class AsyncCallback extends CallbackBase
82     {
AsyncCallback()83         public AsyncCallback()
84         {
85         }
86 
87         public void
isA(Ice.AsyncResult result)88         isA(Ice.AsyncResult result)
89         {
90             test(result.getProxy().end_ice_isA(result));
91             called();
92         }
93 
94         public void
ping(Ice.AsyncResult result)95         ping(Ice.AsyncResult result)
96         {
97             result.getProxy().end_ice_ping(result);
98             called();
99         }
100 
101         public void
id(Ice.AsyncResult result)102         id(Ice.AsyncResult result)
103         {
104             test(result.getProxy().end_ice_id(result).equals("::Test::TestIntf"));
105             called();
106         }
107 
108         public void
ids(Ice.AsyncResult result)109         ids(Ice.AsyncResult result)
110         {
111             test(result.getProxy().end_ice_ids(result).length == 2);
112             called();
113         }
114 
115         public void
connection(Ice.AsyncResult result)116         connection(Ice.AsyncResult result)
117         {
118             test(result.getProxy().end_ice_getConnection(result) != null);
119             called();
120         }
121 
122         public void
op(Ice.AsyncResult result)123         op(Ice.AsyncResult result)
124         {
125             TestIntfPrxHelper.uncheckedCast(result.getProxy()).end_op(result);
126             called();
127         }
128 
129         public void
opWithResult(Ice.AsyncResult result)130         opWithResult(Ice.AsyncResult result)
131         {
132             test(TestIntfPrxHelper.uncheckedCast(result.getProxy()).end_opWithResult(result) == 15);
133             called();
134         }
135 
136         public void
opWithUE(Ice.AsyncResult result)137         opWithUE(Ice.AsyncResult result)
138         {
139             try
140             {
141                 TestIntfPrxHelper.uncheckedCast(result.getProxy()).end_opWithUE(result);
142                 test(false);
143             }
144             catch(TestIntfException ex)
145             {
146                 called();
147             }
148             catch(Ice.Exception ex)
149             {
150                 test(false);
151             }
152         }
153 
154         public void
isAEx(Ice.AsyncResult result)155         isAEx(Ice.AsyncResult result)
156         {
157             try
158             {
159                 result.getProxy().end_ice_isA(result);
160                 test(false);
161             }
162             catch(Ice.NoEndpointException ex)
163             {
164                 called();
165             }
166             catch(Ice.Exception ex)
167             {
168                 test(false);
169             }
170         }
171 
172         public void
pingEx(Ice.AsyncResult result)173         pingEx(Ice.AsyncResult result)
174         {
175             try
176             {
177                 result.getProxy().end_ice_ping(result);
178                 test(false);
179             }
180             catch(Ice.NoEndpointException ex)
181             {
182                 called();
183             }
184             catch(Ice.Exception ex)
185             {
186                 test(false);
187             }
188         }
189 
190         public void
idEx(Ice.AsyncResult result)191         idEx(Ice.AsyncResult result)
192         {
193             try
194             {
195                 result.getProxy().end_ice_id(result);
196                 test(false);
197             }
198             catch(Ice.NoEndpointException ex)
199             {
200                 called();
201             }
202             catch(Ice.Exception ex)
203             {
204                 test(false);
205             }
206         }
207 
208         public void
idsEx(Ice.AsyncResult result)209         idsEx(Ice.AsyncResult result)
210         {
211             try
212             {
213                 result.getProxy().end_ice_ids(result);
214                 test(false);
215             }
216             catch(Ice.NoEndpointException ex)
217             {
218                 called();
219             }
220             catch(Ice.Exception ex)
221             {
222                 test(false);
223             }
224         }
225 
226         public void
connectionEx(Ice.AsyncResult result)227         connectionEx(Ice.AsyncResult result)
228         {
229             try
230             {
231                 result.getProxy().end_ice_getConnection(result);
232                 test(false);
233             }
234             catch(Ice.NoEndpointException ex)
235             {
236                 called();
237             }
238             catch(Ice.Exception ex)
239             {
240                 test(false);
241             }
242         }
243 
244         public void
opEx(Ice.AsyncResult result)245         opEx(Ice.AsyncResult result)
246         {
247             try
248             {
249                 TestIntfPrxHelper.uncheckedCast(result.getProxy()).end_op(result);
250                 test(false);
251             }
252             catch(Ice.NoEndpointException ex)
253             {
254                 called();
255             }
256             catch(Ice.Exception ex)
257             {
258                 test(false);
259             }
260         }
261     }
262 
263     static class ResponseCallback extends CallbackBase
264     {
ResponseCallback()265         ResponseCallback()
266         {
267         }
268 
269         public void
isA(boolean r)270         isA(boolean r)
271         {
272             test(r);
273             called();
274         }
275 
276         public void
ping()277         ping()
278         {
279             called();
280         }
281 
282         public void
id(String id)283         id(String id)
284         {
285             test(id.equals("::Test::TestIntf"));
286             called();
287         }
288 
289         public void
ids(String[] ids)290         ids(String[] ids)
291         {
292             test(ids.length == 2);
293             called();
294         }
295 
296         public void
connection(Ice.Connection conn)297         connection(Ice.Connection conn)
298         {
299             test(conn != null);
300             called();
301         }
302 
303         public void
op()304         op()
305         {
306             called();
307         }
308 
309         public void
opWithResult(int r)310         opWithResult(int r)
311         {
312             test(r == 15);
313             called();
314         }
315 
316         public void
opWithUE(Ice.UserException e)317         opWithUE(Ice.UserException e)
318         {
319             try
320             {
321                 throw e;
322             }
323             catch(TestIntfException ex)
324             {
325                 called();
326             }
327             catch(Ice.UserException ex)
328             {
329                 test(false);
330             }
331         }
332     }
333 
334     static class ExceptionCallback extends CallbackBase
335     {
ExceptionCallback()336         public ExceptionCallback()
337         {
338         }
339 
340         public void
isA(boolean r)341         isA(boolean r)
342         {
343             test(false);
344         }
345 
346         public void
ping()347         ping()
348         {
349             test(false);
350         }
351 
352         public void
id(String id)353         id(String id)
354         {
355             test(false);
356         }
357 
358         public void
ids(String[] ids)359         ids(String[] ids)
360         {
361             test(false);
362         }
363 
364         public void
connection(Ice.Connection conn)365         connection(Ice.Connection conn)
366         {
367             test(false);
368         }
369 
370         public void
op()371         op()
372         {
373             test(false);
374         }
375 
376         public void
ex(Ice.Exception ex)377         ex(Ice.Exception ex)
378         {
379             test(ex instanceof Ice.NoEndpointException);
380             called();
381         }
382 
383         public void
noEx(Ice.Exception ex)384         noEx(Ice.Exception ex)
385         {
386             test(false);
387         }
388     }
389 
390     static class SentCallback extends CallbackBase
391     {
SentCallback()392         SentCallback()
393         {
394             _thread = Thread.currentThread().getId();
395         }
396 
397         public void
isA(boolean r)398         isA(boolean r)
399         {
400         }
401 
402         public void
ping()403         ping()
404         {
405         }
406 
407         public void
id(String s)408         id(String s)
409         {
410         }
411 
412         public void
ids(String[] s)413         ids(String[] s)
414         {
415         }
416 
417         public void
opAsync(Ice.AsyncResult r)418         opAsync(Ice.AsyncResult r)
419         {
420         }
421 
422         public void
op()423         op()
424         {
425         }
426 
427         public void
ex(Ice.Exception ex)428         ex(Ice.Exception ex)
429         {
430         }
431 
432         public void
sent(Ice.AsyncResult r)433         sent(Ice.AsyncResult r)
434         {
435             test(r.sentSynchronously() && _thread == Thread.currentThread().getId() ||
436                  !r.sentSynchronously() && _thread != Thread.currentThread().getId());
437             called();
438         }
439 
440         public void
sent(boolean ss)441         sent(boolean ss)
442         {
443             test(ss && _thread == Thread.currentThread().getId() ||
444                  !ss && _thread != Thread.currentThread().getId());
445             called();
446         }
447 
448         long _thread;
449     }
450 
451     static class FlushCallback extends CallbackBase
452     {
FlushCallback()453         FlushCallback()
454         {
455             _thread = Thread.currentThread().getId();
456         }
457 
458         public void
completedAsync(Ice.AsyncResult r)459         completedAsync(Ice.AsyncResult r)
460         {
461             test(false);
462         }
463 
464         public void
exception(Ice.Exception ex)465         exception(Ice.Exception ex)
466         {
467             test(false);
468         }
469 
470         public void
sentAsync(Ice.AsyncResult r)471         sentAsync(Ice.AsyncResult r)
472         {
473             test((r.sentSynchronously() && _thread == Thread.currentThread().getId()) ||
474                  (!r.sentSynchronously() && _thread != Thread.currentThread().getId()));
475             called();
476         }
477 
478         public void
sent(boolean sentSynchronously)479         sent(boolean sentSynchronously)
480         {
481             test((sentSynchronously && _thread == Thread.currentThread().getId()) ||
482                  (!sentSynchronously && _thread != Thread.currentThread().getId()));
483             called();
484         }
485 
486         long _thread;
487     }
488 
489     static class FlushExCallback extends CallbackBase
490     {
FlushExCallback()491         FlushExCallback()
492         {
493         }
494 
495         public void
completedAsync(Ice.AsyncResult r)496         completedAsync(Ice.AsyncResult r)
497         {
498             try
499             {
500                 if(r.getConnection() != null)
501                 {
502                     r.getConnection().end_flushBatchRequests(r);
503                 }
504                 else
505                 {
506                     r.getProxy().end_ice_flushBatchRequests(r);
507                 }
508                 test(false);
509             }
510             catch(Ice.Exception ex)
511             {
512                 called();
513             }
514         }
515 
516         public void
exception(Ice.Exception ex)517         exception(Ice.Exception ex)
518         {
519             called();
520         }
521 
522         public void
sentAsync(Ice.AsyncResult r)523         sentAsync(Ice.AsyncResult r)
524         {
525             test(false);
526         }
527 
528         public void
sent(boolean sentSynchronously)529         sent(boolean sentSynchronously)
530         {
531             test(false);
532         }
533     }
534 
535     enum ThrowType { LocalException, OtherException };
536 
537     static class Thrower extends CallbackBase
538     {
Thrower(ThrowType t)539         public Thrower(ThrowType t)
540         {
541             _t = t;
542         }
543 
544         public void
opAsync(Ice.AsyncResult r)545         opAsync(Ice.AsyncResult r)
546         {
547             called();
548             throwEx();
549         }
550 
551         public void
op()552         op()
553         {
554             called();
555             throwEx();
556         }
557 
558         public void
noOp()559         noOp()
560         {
561         }
562 
563         public void
ex(Ice.Exception ex)564         ex(Ice.Exception ex)
565         {
566             called();
567             throwEx();
568         }
569 
570         public void
sent(boolean ss)571         sent(boolean ss)
572         {
573             called();
574             throwEx();
575         }
576 
577         private void
throwEx()578         throwEx()
579         {
580             switch(_t)
581             {
582             case LocalException:
583             {
584                 throw new Ice.ObjectNotExistException();
585             }
586             case OtherException:
587             {
588                 throw new RuntimeException();
589             }
590             default:
591             {
592                 assert(false);
593                 break;
594             }
595             }
596         }
597 
598         ThrowType _t;
599     }
600 
601     public static void
run(test.TestHelper helper, Ice.Communicator communicator, boolean collocated, TestIntfPrx p, TestIntfControllerPrx testController)602     run(test.TestHelper helper, Ice.Communicator communicator, boolean collocated, TestIntfPrx p,
603         TestIntfControllerPrx testController)
604     {
605 
606         PrintWriter out = helper.getWriter();
607 
608         out.print("testing response callback... ");
609         out.flush();
610         {
611             final ResponseCallback cb = new ResponseCallback();
612             java.util.Map<String, String> ctx = new java.util.HashMap<String, String>();
613 
614             p.begin_ice_isA("::Test::TestIntf",
615                 (boolean r) -> cb.isA(r),
616                 (Ice.Exception ex) -> test(false));
617             cb.check();
618 
619             p.begin_ice_isA("::Test::TestIntf",
620                 (boolean r) -> cb.isA(r),
621                 (Ice.Exception ex) -> test(false));
622             cb.check();
623 
624             p.begin_ice_ping(
625                 () -> cb.ping(),
626                 (Ice.Exception ex) -> test(false));
627             cb.check();
628 
629             p.begin_ice_ping(ctx,
630                 () -> cb.ping(),
631                 (Ice.Exception ex) -> test(false));
632             cb.check();
633 
634             p.begin_ice_id(
635                 (String id) -> cb.id(id),
636                 (Ice.Exception ex) -> test(false));
637             cb.check();
638 
639             p.begin_ice_id(ctx,
640                 (String id) -> cb.id(id),
641                 (Ice.Exception ex) -> test(false));
642             cb.check();
643 
644             p.begin_ice_ids(
645                 (String[] ids) -> cb.ids(ids),
646                 (Ice.Exception ex) -> test(false));
647             cb.check();
648 
649             p.begin_ice_ids(ctx,
650                 (String[] ids) -> cb.ids(ids),
651                 (Ice.Exception ex) -> test(false));
652             cb.check();
653 
654             if(!collocated)
655             {
656                 p.begin_ice_getConnection(
657                     (Ice.Connection conn) -> cb.connection(conn),
658                     (Ice.Exception ex) -> test(false));
659                 cb.check();
660             }
661 
662             p.begin_op(
663                 () -> cb.op(),
664                 (Ice.Exception ex) -> test(false));
665             cb.check();
666 
667             p.begin_op(ctx,
668                 () -> cb.op(),
669                 (Ice.Exception ex) -> test(false));
670             cb.check();
671 
672             p.begin_opWithResult(
673                 (int r) -> cb.opWithResult(r),
674                 (Ice.Exception ex) -> test(false));
675             cb.check();
676 
677             p.begin_opWithResult(ctx,
678                 (int r) -> cb.opWithResult(r),
679                 (Ice.Exception ex) -> test(false));
680             cb.check();
681 
682             p.begin_opWithUE(
683                 () -> test(false),
684                 (Ice.UserException ex) -> cb.opWithUE(ex),
685                 (Ice.Exception ex) -> test(false));
686             cb.check();
687 
688             p.begin_opWithUE(ctx,
689                 () -> test(false),
690                 (Ice.UserException ex) -> cb.opWithUE(ex),
691                 (Ice.Exception ex) -> test(false));
692             cb.check();
693         }
694         out.println("ok");
695 
696         out.print("testing local exceptions with response callback... ");
697         out.flush();
698         {
699             TestIntfPrx i = TestIntfPrxHelper.uncheckedCast(p.ice_adapterId("dummy"));
700             final ExceptionCallback cb = new ExceptionCallback();
701 
702             i.begin_ice_isA("::Test::TestIntf",
703                 (boolean r) -> test(false),
704                 (Ice.Exception ex) -> cb.ex(ex));
705             cb.check();
706 
707             i.begin_ice_ping(
708                 () -> test(false),
709                 (Ice.Exception ex) -> cb.ex(ex));
710             cb.check();
711 
712             i.begin_ice_id(
713                 (String id) -> test(false),
714                 (Ice.Exception ex) -> cb.ex(ex));
715             cb.check();
716 
717             i.begin_ice_ids(
718                 (String[] ids) -> test(false),
719                 (Ice.Exception ex) -> cb.ex(ex));
720             cb.check();
721 
722             if(!collocated)
723             {
724                 i.begin_ice_getConnection(
725                     (Ice.Connection conn) -> test(false),
726                     (Ice.Exception ex) -> cb.ex(ex));
727                 cb.check();
728             }
729 
730             i.begin_op(
731                 () -> test(false),
732                 (Ice.Exception ex) -> cb.ex(ex));
733             cb.check();
734         }
735         out.println("ok");
736 
737         out.print("testing sent callback... ");
738         out.flush();
739         {
740             final SentCallback cb = new SentCallback();
741 
742             p.begin_ice_isA("",
743                 (boolean r) -> cb.isA(r),
744                 (Ice.Exception ex) -> cb.ex(ex),
745                 (boolean ss) -> cb.sent(ss));
746             cb.check();
747 
748             p.begin_ice_ping(
749                 () -> cb.ping(),
750                 (Ice.Exception ex) -> cb.ex(ex),
751                 (boolean ss) -> cb.sent(ss));
752             cb.check();
753 
754             p.begin_ice_id(
755                 (String id) -> cb.id(id),
756                 (Ice.Exception ex) -> cb.ex(ex),
757                 (boolean ss) -> cb.sent(ss));
758             cb.check();
759 
760             p.begin_ice_ids(
761                 (String[] ids) -> cb.ids(ids),
762                 (Ice.Exception ex) -> cb.ex(ex),
763                 (boolean ss) -> cb.sent(ss));
764             cb.check();
765 
766             p.begin_op(
767                 () -> cb.op(),
768                 (Ice.Exception ex) -> cb.ex(ex),
769                 (boolean ss) -> cb.sent(ss));
770             cb.check();
771 
772             java.util.List<SentCallback> cbs = new java.util.ArrayList<SentCallback>();
773             byte[] seq = new byte[10024];
774             new java.util.Random().nextBytes(seq); // Make sure the request doesn't compress too well.
775             Ice.AsyncResult r;
776             testController.holdAdapter();
777             try
778             {
779                 do
780                 {
781                     final SentCallback cb2 = new SentCallback();
782                     r = p.begin_opWithPayload(seq,
783                         () -> {},
784                         (Ice.Exception ex) -> cb2.ex(ex),
785                         (boolean ss) -> cb2.sent(ss));
786                     cbs.add(cb2);
787                 }
788                 while(r.sentSynchronously());
789             }
790             finally
791             {
792                 testController.resumeAdapter();
793             }
794             for(SentCallback cb3 : cbs)
795             {
796                 cb3.check();
797             }
798         }
799         out.println("ok");
800 
801         out.print("testing unexpected exceptions from callback... ");
802         out.flush();
803         {
804             TestIntfPrx q = TestIntfPrxHelper.uncheckedCast(p.ice_adapterId("dummy"));
805             ThrowType throwEx[] = { ThrowType.LocalException, ThrowType.OtherException };
806 
807             for(int i = 0; i < 2; ++i)
808             {
809                 final Thrower cb = new Thrower(throwEx[i]);
810 
811                 p.begin_op(
812                     () -> cb.op(),
813                     null);
814                 cb.check();
815 
816                 q.begin_op(
817                     () -> cb.op(),
818                     (Ice.Exception ex) -> cb.ex(ex));
819                 cb.check();
820 
821                 p.begin_op(
822                     () -> {},
823                     (Ice.Exception ex) -> {},
824                     (boolean ss) -> cb.sent(ss));
825                 cb.check();
826             }
827         }
828         out.println("ok");
829 
830         out.print("testing batch requests with proxy... ");
831         out.flush();
832         {
833             {
834                 //
835                 // Type-safe.
836                 //
837                 test(p.opBatchCount() == 0);
838                 TestIntfPrx b1 = (TestIntfPrx)p.ice_batchOneway();
839                 b1.opBatch();
840                 b1.opBatch();
841                 final FlushCallback cb = new FlushCallback();
842                 Ice.AsyncResult r = b1.begin_ice_flushBatchRequests(
843                     null,
844                     (Ice.Exception ex) -> cb.exception(ex),
845                     (boolean sentSynchronously) -> cb.sent(sentSynchronously));
846                 cb.check();
847                 test(r.isSent());
848                 test(r.isCompleted());
849                 test(p.waitForBatch(2));
850             }
851 
852             if(p.ice_getConnection() != null)
853             {
854                 //
855                 // Type-safe exception.
856                 //
857                 test(p.opBatchCount() == 0);
858                 TestIntfPrx b1 = (TestIntfPrx)p.ice_batchOneway();
859                 b1.opBatch();
860                 b1.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
861                 final FlushCallback cb = new FlushCallback();
862                 Ice.AsyncResult r = b1.begin_ice_flushBatchRequests(
863                     null,
864                     (Ice.Exception ex) -> cb.exception(ex),
865                     (boolean sentSynchronously) -> cb.sent(sentSynchronously));
866                 cb.check();
867                 test(r.isSent());
868                 test(r.isCompleted());
869                 test(p.waitForBatch(1));
870             }
871         }
872         out.println("ok");
873 
874         if(p.ice_getConnection() != null)
875         {
876             out.print("testing batch requests with connection... ");
877             out.flush();
878             {
879                 {
880                     //
881                     // Type-safe.
882                     //
883                     test(p.opBatchCount() == 0);
884                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
885                                                                          p.ice_getIdentity()).ice_batchOneway());
886                     b1.opBatch();
887                     b1.opBatch();
888                     final FlushCallback cb = new FlushCallback();
889                     Ice.AsyncResult r = b1.ice_getConnection().begin_flushBatchRequests(
890                         Ice.CompressBatch.BasedOnProxy,
891                         null,
892                         (Ice.Exception ex) -> cb.exception(ex),
893                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
894                     cb.check();
895                     test(r.isSent());
896                     test(r.isCompleted());
897                     test(p.waitForBatch(2));
898 
899                     final FlushCallback cb2 = new FlushCallback();
900                     Ice.AsyncResult r2 = b1.ice_getConnection().begin_flushBatchRequests(
901                         Ice.CompressBatch.BasedOnProxy,
902                         null,
903                         (Ice.Exception ex) -> cb2.exception(ex),
904                         (boolean sentSynchronously) -> cb2.sent(sentSynchronously));
905                     cb2.check();
906                     test(r2.isSent());
907                     test(r2.isCompleted());
908                 }
909 
910                 {
911                     //
912                     // Type-safe exception.
913                     //
914                     test(p.opBatchCount() == 0);
915                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
916                                                                          p.ice_getIdentity()).ice_batchOneway());
917                     b1.opBatch();
918                     b1.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
919                     final FlushExCallback cb = new FlushExCallback();
920                     Ice.AsyncResult r = b1.ice_getConnection().begin_flushBatchRequests(
921                         Ice.CompressBatch.BasedOnProxy,
922                         null,
923                         (Ice.Exception ex) -> cb.exception(ex),
924                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
925                     cb.check();
926                     test(!r.isSent());
927                     test(r.isCompleted());
928                     test(p.opBatchCount() == 0);
929                 }
930             }
931             out.println("ok");
932 
933             out.print("testing batch requests with communicator... ");
934             out.flush();
935             {
936                 {
937                     //
938                     // Type-safe - 1 connection.
939                     //
940                     test(p.opBatchCount() == 0);
941                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
942                                                                          p.ice_getIdentity()).ice_batchOneway());
943                     b1.opBatch();
944                     b1.opBatch();
945                     final FlushCallback cb = new FlushCallback();
946                     Ice.AsyncResult r = communicator.begin_flushBatchRequests(
947                         Ice.CompressBatch.BasedOnProxy,
948                         null,
949                         (Ice.Exception ex) -> cb.exception(ex),
950                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
951                     cb.check();
952                     test(r.isSent());
953                     test(r.isCompleted());
954                     test(p.waitForBatch(2));
955                 }
956 
957                 {
958                     //
959                     // Type-safe exception - 1 connection.
960                     //
961                     test(p.opBatchCount() == 0);
962                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
963                                                                          p.ice_getIdentity()).ice_batchOneway());
964                     b1.opBatch();
965                     b1.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
966                     final FlushCallback cb = new FlushCallback();
967                     Ice.AsyncResult r = communicator.begin_flushBatchRequests(
968                         Ice.CompressBatch.BasedOnProxy,
969                         null,
970                         (Ice.Exception ex) -> cb.exception(ex),
971                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
972                     cb.check();
973                     test(r.isSent()); // Exceptions are ignored!
974                     test(r.isCompleted());
975                     test(p.opBatchCount() == 0);
976                 }
977 
978                 {
979                     //
980                     // 2 connections.
981                     //
982                     test(p.opBatchCount() == 0);
983                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
984                                                                          p.ice_getIdentity()).ice_batchOneway());
985                     TestIntfPrx b2 = TestIntfPrxHelper.uncheckedCast(
986                         p.ice_connectionId("2").ice_getConnection().createProxy(p.ice_getIdentity()).ice_batchOneway());
987                     b2.ice_getConnection(); // Ensure connection is established.
988                     b1.opBatch();
989                     b1.opBatch();
990                     b2.opBatch();
991                     b2.opBatch();
992                     final FlushCallback cb = new FlushCallback();
993                     Ice.AsyncResult r = communicator.begin_flushBatchRequests(
994                         Ice.CompressBatch.BasedOnProxy,
995                         null,
996                         (Ice.Exception ex) -> cb.exception(ex),
997                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
998                     cb.check();
999                     test(r.isSent());
1000                     test(r.isCompleted());
1001                     test(p.waitForBatch(4));
1002                 }
1003 
1004                 {
1005                     //
1006                     // Exception - 2 connections - 1 failure.
1007                     //
1008                     // All connections should be flushed even if there are failures on some connections.
1009                     // Exceptions should not be reported.
1010                     //
1011                     test(p.opBatchCount() == 0);
1012                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
1013                                                                          p.ice_getIdentity()).ice_batchOneway());
1014                     TestIntfPrx b2 = TestIntfPrxHelper.uncheckedCast(
1015                         p.ice_connectionId("2").ice_getConnection().createProxy(p.ice_getIdentity()).ice_batchOneway());
1016                     b2.ice_getConnection(); // Ensure connection is established.
1017                     b1.opBatch();
1018                     b2.opBatch();
1019                     b1.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
1020                     final FlushCallback cb = new FlushCallback();
1021                     Ice.AsyncResult r = communicator.begin_flushBatchRequests(
1022                         Ice.CompressBatch.BasedOnProxy,
1023                         null,
1024                         (Ice.Exception ex) -> cb.exception(ex),
1025                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
1026                     cb.check();
1027                     test(r.isSent()); // Exceptions are ignored!
1028                     test(r.isCompleted());
1029                     test(p.waitForBatch(1));
1030                 }
1031 
1032                 {
1033                     //
1034                     // Exception - 2 connections - 2 failures.
1035                     //
1036                     // The sent callback should be invoked even if all connections fail.
1037                     //
1038                     test(p.opBatchCount() == 0);
1039                     TestIntfPrx b1 = TestIntfPrxHelper.uncheckedCast(p.ice_getConnection().createProxy(
1040                                                                          p.ice_getIdentity()).ice_batchOneway());
1041                     TestIntfPrx b2 = TestIntfPrxHelper.uncheckedCast(
1042                         p.ice_connectionId("2").ice_getConnection().createProxy(p.ice_getIdentity()).ice_batchOneway());
1043                     b2.ice_getConnection(); // Ensure connection is established.
1044                     b1.opBatch();
1045                     b2.opBatch();
1046                     b1.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
1047                     b2.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
1048                     final FlushCallback cb = new FlushCallback();
1049                     Ice.AsyncResult r = communicator.begin_flushBatchRequests(
1050                         Ice.CompressBatch.BasedOnProxy,
1051                         null,
1052                         (Ice.Exception ex) -> cb.exception(ex),
1053                         (boolean sentSynchronously) -> cb.sent(sentSynchronously));
1054                     cb.check();
1055                     test(r.isSent()); // Exceptions are ignored!
1056                     test(r.isCompleted());
1057                     test(p.opBatchCount() == 0);
1058                 }
1059             }
1060             out.println("ok");
1061         }
1062 
1063         out.print("testing null callbacks...");
1064         try
1065         {
1066             IceInternal.Functional_VoidCallback response = null;
1067             IceInternal.Functional_GenericCallback1<Ice.Exception> exception = null;
1068             p.begin_ice_ping(response, exception);
1069             test(false);
1070         }
1071         catch(IllegalArgumentException ex)
1072         {
1073             // Excepted when response and exception callback are both null.
1074         }
1075 
1076         try
1077         {
1078             p.begin_ice_ping(() -> {}, null);
1079 
1080         }
1081         catch(IllegalArgumentException ex)
1082         {
1083             test(false);
1084         }
1085 
1086         try
1087         {
1088             p.begin_ice_ping(null, (Ice.Exception ex) -> {});
1089 
1090         }
1091         catch(IllegalArgumentException ex)
1092         {
1093             test(false);
1094         }
1095 
1096         try
1097         {
1098             IceInternal.Functional_BoolCallback response = null;
1099             IceInternal.Functional_GenericCallback1<Ice.Exception> exception = null;
1100             p.begin_ice_isA("::Test::TestIntf", response, exception);
1101             test(false);
1102         }
1103         catch(IllegalArgumentException ex)
1104         {
1105             // Excepted when response and exception callback are both null.
1106         }
1107 
1108         try
1109         {
1110             p.begin_ice_isA("::Test::TestIntf", (boolean v) -> {}, null);
1111 
1112         }
1113         catch(IllegalArgumentException ex)
1114         {
1115             test(false);
1116         }
1117 
1118         try
1119         {
1120             p.begin_ice_isA("::Test::TestIntf", null, (Ice.Exception ex) -> {});
1121 
1122         }
1123         catch(IllegalArgumentException ex)
1124         {
1125             test(false);
1126         }
1127 
1128         try
1129         {
1130             IceInternal.Functional_VoidCallback response = null;
1131             p.begin_opWithUE(response, null, (Ice.Exception ex) -> {});
1132             test(false);
1133         }
1134         catch(IllegalArgumentException ex)
1135         {
1136             // Excepted when response and exception callback are both null, for
1137             // an operation that throws user exceptions both user exception callback
1138             // an local exception callback must be present.
1139         }
1140 
1141         try
1142         {
1143             IceInternal.Functional_VoidCallback response = null;
1144             p.begin_opWithUE(response, (Ice.UserException ex) -> {}, null);
1145             test(false);
1146         }
1147         catch(IllegalArgumentException ex)
1148         {
1149             // Excepted when response and exception callback are both null, for
1150             // an operation that throws user exceptions both user exception callback
1151             // an local exception callback must be present.
1152         }
1153 
1154         try
1155         {
1156             IceInternal.Functional_VoidCallback response = null;
1157             IceInternal.Functional_GenericCallback1<Ice.UserException> userException = null;
1158             IceInternal.Functional_GenericCallback1<Ice.Exception> exception = null;
1159             p.begin_opWithUE(response, userException, exception);
1160             test(false);
1161         }
1162         catch(IllegalArgumentException ex)
1163         {
1164             // Excepted when response and exception callback are both null.
1165         }
1166         out.println("ok");
1167 
1168         if(!collocated)
1169         {
1170             out.print("testing bidir...");
1171             Ice.ObjectAdapter adapter = communicator.createObjectAdapter("");
1172             PingReplyI replyI = new PingReplyI();
1173             PingReplyPrx reply = PingReplyPrxHelper.uncheckedCast(adapter.addWithUUID(replyI));
1174             adapter.activate();
1175 
1176             p.ice_getConnection().setAdapter(adapter);
1177             p.pingBiDir(reply);
1178             test(replyI.checkReceived());
1179             adapter.destroy();
1180             out.println("ok");
1181         }
1182     }
1183 }
1184