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 test.Ice.operations.Test.MyClassPrx;
10 
11 class BatchOneways
12 {
test(boolean b)13     private static void test(boolean b)
14     {
15         if(!b)
16         {
17             throw new RuntimeException();
18         }
19     }
20 
21     static class BatchRequestInterceptorI implements com.zeroc.Ice.BatchRequestInterceptor
22     {
enqueue(com.zeroc.Ice.BatchRequest request, int count, int size)23         public void enqueue(com.zeroc.Ice.BatchRequest request, int count, int size)
24         {
25             test(request.getOperation().equals("opByteSOneway") || request.getOperation().equals("ice_ping"));
26             test(request.getProxy().ice_isBatchOneway());
27 
28             if(count > 0)
29             {
30                 test(_lastRequestSize + _size == size);
31             }
32             _count = count;
33             _size = size;
34 
35             if(_size + request.getSize() > 25000)
36             {
37                 request.getProxy().ice_flushBatchRequestsAsync();
38                 _size = 18; // header
39             }
40 
41             if(_enabled)
42             {
43                 _lastRequestSize = request.getSize();
44                 ++_count;
45                 request.enqueue();
46             }
47         }
48 
setEnqueue(boolean enabled)49         public void setEnqueue(boolean enabled)
50         {
51             _enabled = enabled;
52         }
53 
count()54         public int count()
55         {
56             return _count;
57         }
58 
59         private boolean _enabled;
60         private int _count;
61         private int _size;
62         private int _lastRequestSize;
63     }
64 
batchOneways(test.TestHelper helper, MyClassPrx p, PrintWriter out)65     static void batchOneways(test.TestHelper helper, MyClassPrx p, PrintWriter out)
66     {
67         final com.zeroc.Ice.Communicator communicator = helper.communicator();
68         final com.zeroc.Ice.Properties properties = communicator.getProperties();
69         final byte[] bs1 = new byte[10  * 1024];
70 
71         MyClassPrx batch = p.ice_batchOneway();
72         batch.ice_flushBatchRequests(); // Empty flush
73         if(batch.ice_getConnection() != null)
74         {
75             batch.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.BasedOnProxy);
76         }
77         communicator.flushBatchRequests(com.zeroc.Ice.CompressBatch.BasedOnProxy);
78 
79         p.opByteSOnewayCallCount(); // Reset the call count
80 
81         for(int i = 0 ; i < 30 ; ++i)
82         {
83             try
84             {
85                 batch.opByteSOneway(bs1);
86             }
87             catch(com.zeroc.Ice.MemoryLimitException ex)
88             {
89                 test(false);
90             }
91         }
92 
93         int count = 0;
94         while(count < 27) // 3 * 9 requests auto-flushed.
95         {
96             count += p.opByteSOnewayCallCount();
97             try
98             {
99                 Thread.sleep(10);
100             }
101             catch(InterruptedException ex)
102             {
103             }
104         }
105 
106         final boolean bluetooth = properties.getProperty("Ice.Default.Protocol").indexOf("bt") == 0;
107         if(batch.ice_getConnection() != null && !bluetooth)
108         {
109             MyClassPrx batch1 = p.ice_batchOneway();
110             MyClassPrx batch2 = p.ice_batchOneway();
111 
112             batch1.ice_ping();
113             batch2.ice_ping();
114             batch1.ice_flushBatchRequests();
115             batch1.ice_getConnection().close(com.zeroc.Ice.ConnectionClose.GracefullyWithWait);
116             batch1.ice_ping();
117             batch2.ice_ping();
118 
119             batch1.ice_getConnection();
120             batch2.ice_getConnection();
121 
122             batch1.ice_ping();
123             batch1.ice_getConnection().close(com.zeroc.Ice.ConnectionClose.GracefullyWithWait);
124             batch1.ice_ping();
125             batch2.ice_ping();
126         }
127 
128         com.zeroc.Ice.Identity identity = new com.zeroc.Ice.Identity();
129         identity.name = "invalid";
130         com.zeroc.Ice.ObjectPrx batch3 = batch.ice_identity(identity);
131         batch3.ice_ping();
132         batch3.ice_flushBatchRequests();
133 
134         // Make sure that a bogus batch request doesn't cause troubles to other ones.
135         batch3.ice_ping();
136         batch.ice_ping();
137         batch.ice_flushBatchRequests();
138         batch.ice_ping();
139 
140         if(batch.ice_getConnection() != null && !bluetooth)
141         {
142             com.zeroc.Ice.InitializationData initData = new com.zeroc.Ice.InitializationData();
143             initData.properties = properties._clone();
144             BatchRequestInterceptorI interceptor = new BatchRequestInterceptorI();
145             initData.batchRequestInterceptor = interceptor;
146             try(com.zeroc.Ice.Communicator ic = helper.initialize(initData))
147             {
148                 batch = MyClassPrx.uncheckedCast(ic.stringToProxy(p.toString())).ice_batchOneway();
149 
150                 test(interceptor.count() == 0);
151                 batch.ice_ping();
152                 batch.ice_ping();
153                 batch.ice_ping();
154                 test(interceptor.count() == 0);
155 
156                 interceptor.setEnqueue(true);
157                 batch.ice_ping();
158                 batch.ice_ping();
159                 batch.ice_ping();
160                 test(interceptor.count() == 3);
161 
162                 batch.ice_flushBatchRequests();
163                 batch.ice_ping();
164                 test(interceptor.count() == 1);
165 
166                 batch.opByteSOneway(bs1);
167                 test(interceptor.count() == 2);
168                 batch.opByteSOneway(bs1);
169                 test(interceptor.count() == 3);
170 
171                 batch.opByteSOneway(bs1); // This should trigger the flush
172                 batch.ice_ping();
173                 test(interceptor.count() == 2);
174             }
175         }
176 
177         boolean supportsCompress = true;
178         try
179         {
180             supportsCompress = p.supportsCompress();
181         }
182         catch(com.zeroc.Ice.OperationNotExistException ex)
183         {
184         }
185 
186         p.ice_ping();
187         if(supportsCompress && p.ice_getConnection() != null &&
188            properties.getProperty("Ice.Override.Compress").equals(""))
189         {
190             com.zeroc.Ice.ObjectPrx prx = p.ice_getConnection().createProxy(p.ice_getIdentity()).ice_batchOneway();
191 
192             MyClassPrx batchC1 = MyClassPrx.uncheckedCast(prx.ice_compress(false));
193             MyClassPrx batchC2 = MyClassPrx.uncheckedCast(prx.ice_compress(true));
194             MyClassPrx batchC3 = MyClassPrx.uncheckedCast(prx.ice_identity(identity));
195 
196             batchC1.opByteSOneway(bs1);
197             batchC1.opByteSOneway(bs1);
198             batchC1.opByteSOneway(bs1);
199             batchC1.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.Yes);
200 
201             batchC2.opByteSOneway(bs1);
202             batchC2.opByteSOneway(bs1);
203             batchC2.opByteSOneway(bs1);
204             batchC1.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.No);
205 
206             batchC1.opByteSOneway(bs1);
207             batchC1.opByteSOneway(bs1);
208             batchC1.opByteSOneway(bs1);
209             batchC1.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.BasedOnProxy);
210 
211             batchC1.opByteSOneway(bs1);
212             batchC2.opByteSOneway(bs1);
213             batchC1.opByteSOneway(bs1);
214             batchC1.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.BasedOnProxy);
215 
216             batchC1.opByteSOneway(bs1);
217             batchC3.opByteSOneway(bs1);
218             batchC1.opByteSOneway(bs1);
219             batchC1.ice_getConnection().flushBatchRequests(com.zeroc.Ice.CompressBatch.BasedOnProxy);
220         }
221     }
222 }
223