1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 package test.Ice.operations;
6 
7 import java.io.PrintWriter;
8 
9 import com.zeroc.Ice.Util;
10 import com.zeroc.Ice.LocalException;
11 
12 import test.Ice.operations.Test.MyClassPrx;
13 
14 class BatchOnewaysAMI
15 {
16     private static class Callback
17     {
Callback()18         Callback()
19         {
20             _called = false;
21         }
22 
check()23         public synchronized void check()
24         {
25             while(!_called)
26             {
27                 try
28                 {
29                     wait();
30                 }
31                 catch(InterruptedException ex)
32                 {
33                 }
34             }
35 
36             _called = false;
37         }
38 
called()39         public synchronized void called()
40         {
41             assert (!_called);
42             _called = true;
43             notify();
44         }
45 
46         private boolean _called;
47     }
48 
test(boolean b)49     private static void test(boolean b)
50     {
51         if(!b)
52         {
53             throw new RuntimeException();
54         }
55     }
56 
batchOneways(MyClassPrx p, PrintWriter out)57     static void batchOneways(MyClassPrx p, PrintWriter out)
58     {
59         final com.zeroc.Ice.Communicator communicator = p.ice_getCommunicator();
60         final com.zeroc.Ice.Properties properties = communicator.getProperties();
61         final byte[] bs1 = new byte[10 * 1024];
62 
63         MyClassPrx batch = p.ice_batchOneway();
64         batch.ice_flushBatchRequestsAsync().join(); // Empty flush
65 
66         {
67             test(batch.ice_flushBatchRequestsAsync().isDone()); // Empty flush
68             test(Util.getInvocationFuture(batch.ice_flushBatchRequestsAsync()).isSent()); // Empty flush
69             test(Util.getInvocationFuture(batch.ice_flushBatchRequestsAsync()).sentSynchronously()); // Empty flush
70         }
71 
72         for(int i = 0; i < 30; ++i)
73         {
74             batch.opByteSOnewayAsync(bs1).whenComplete((result, ex) ->
75                 {
76                     test(ex == null);
77                 });
78         }
79 
80         int count = 0;
81         while(count < 27) // 3 * 9 requests auto-flushed.
82         {
83             count += p.opByteSOnewayCallCount();
84             try
85             {
86                 Thread.sleep(10);
87             }
88             catch(InterruptedException ex)
89             {
90             }
91         }
92 
93         final boolean bluetooth = properties.getProperty("Ice.Default.Protocol").indexOf("bt") == 0;
94         if(batch.ice_getConnection() != null && !bluetooth)
95         {
96             MyClassPrx batch2 = p.ice_batchOneway();
97 
98             batch.ice_pingAsync();
99             batch2.ice_pingAsync();
100             batch.ice_flushBatchRequestsAsync().join();
101             batch.ice_getConnection().close(com.zeroc.Ice.ConnectionClose.GracefullyWithWait);
102             batch.ice_pingAsync();
103             batch2.ice_pingAsync();
104 
105             batch.ice_getConnection();
106             batch2.ice_getConnection();
107 
108             batch.ice_pingAsync();
109             batch.ice_getConnection().close(com.zeroc.Ice.ConnectionClose.GracefullyWithWait);
110             test(!batch.ice_pingAsync().isCompletedExceptionally());
111             test(!batch2.ice_pingAsync().isCompletedExceptionally());
112         }
113 
114         com.zeroc.Ice.Identity identity = new com.zeroc.Ice.Identity();
115         identity.name = "invalid";
116         com.zeroc.Ice.ObjectPrx batch3 = batch.ice_identity(identity);
117         batch3.ice_pingAsync();
118         batch3.ice_flushBatchRequestsAsync().join();
119 
120         // Make sure that a bogus batch request doesn't cause troubles to other ones.
121         batch3.ice_pingAsync();
122         batch.ice_pingAsync();
123         batch.ice_flushBatchRequestsAsync().join();
124         batch.ice_pingAsync();
125     }
126 }
127