1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 package test.Ice.hold;
6 
7 import java.io.PrintWriter;
8 
9 import test.Ice.hold.Test.HoldPrx;
10 import test.Ice.hold.Test.HoldPrxHelper;
11 import test.Ice.hold.Test.Callback_Hold_set;
12 
13 public class AllTests
14 {
15     private static void
test(boolean b)16     test(boolean b)
17     {
18         if(!b)
19         {
20             throw new RuntimeException();
21         }
22     }
23 
24     static class Condition
25     {
Condition(boolean value)26         public Condition(boolean value)
27         {
28             _value = value;
29         }
30 
31         synchronized public void
set(boolean value)32         set(boolean value)
33         {
34             _value = value;
35         }
36 
37         synchronized boolean
value()38         value()
39         {
40             return _value;
41         }
42 
43         private boolean _value;
44     };
45 
46     static class AMICheckSetValue extends Callback_Hold_set
47     {
48         public
AMICheckSetValue(Condition condition, int expected)49         AMICheckSetValue(Condition condition, int expected)
50         {
51             _condition = condition;
52             _expected = expected;
53         }
54 
55         @Override
56         public void
response(int value)57         response(int value)
58         {
59             if(value != _expected)
60             {
61                 _condition.set(false);
62             }
63         }
64 
65         @Override
66         public void
exception(Ice.LocalException ex)67         exception(Ice.LocalException ex)
68         {
69         }
70 
71         @Override
72         synchronized public void
sent(boolean sync)73         sent(boolean sync)
74         {
75         }
76 
77         private Condition _condition;
78         private int _expected;
79     };
80 
81     public static void
allTests(test.TestHelper helper)82     allTests(test.TestHelper helper)
83     {
84         Ice.Communicator communicator = helper.communicator();
85         PrintWriter out = helper.getWriter();
86 
87         out.print("testing stringToProxy... ");
88         out.flush();
89         String ref = "hold:" + helper.getTestEndpoint(0);
90         Ice.ObjectPrx base = communicator.stringToProxy(ref);
91         test(base != null);
92         String refSerialized = "hold:" + helper.getTestEndpoint(1);
93         Ice.ObjectPrx baseSerialized = communicator.stringToProxy(refSerialized);
94         test(baseSerialized != null);
95         out.println("ok");
96 
97         out.print("testing checked cast... ");
98         out.flush();
99         HoldPrx hold = HoldPrxHelper.checkedCast(base);
100         HoldPrx holdOneway = HoldPrxHelper.uncheckedCast(base.ice_oneway());
101         test(hold != null);
102         test(hold.equals(base));
103         HoldPrx holdSerialized = HoldPrxHelper.checkedCast(baseSerialized);
104         HoldPrx holdSerializedOneway = HoldPrxHelper.uncheckedCast(baseSerialized.ice_oneway());
105         test(holdSerialized != null);
106         test(holdSerialized.equals(baseSerialized));
107         out.println("ok");
108 
109         out.print("changing state between active and hold rapidly... ");
110         out.flush();
111         for(int i = 0; i < 100; ++i)
112         {
113             hold.putOnHold(0);
114         }
115         for(int i = 0; i < 100; ++i)
116         {
117             holdOneway.putOnHold(0);
118         }
119         for(int i = 0; i < 100; ++i)
120         {
121             holdSerialized.putOnHold(0);
122         }
123         for(int i = 0; i < 100; ++i)
124         {
125             holdSerializedOneway.putOnHold(0);
126         }
127         out.println("ok");
128 
129         out.print("testing without serialize mode... ");
130         out.flush();
131         java.util.Random random = new java.util.Random();
132         {
133             Condition cond = new Condition(true);
134             int value = 0;
135             Ice.AsyncResult result = null;
136             while(cond.value())
137             {
138                 result = hold.begin_set(value + 1, random.nextInt(5), new AMICheckSetValue(cond, value));
139                 ++value;
140                 if(value % 100 == 0)
141                 {
142                     result.waitForSent();
143                 }
144 
145                 if(value > 1000000)
146                 {
147                     // Don't continue, it's possible that out-of-order dispatch doesn't occur
148                     // after 100000 iterations and we don't want the test to last for too long
149                     // when this occurs.
150                     break;
151                 }
152             }
153             test(value > 100000 || !cond.value());
154             result.waitForCompleted();
155         }
156         out.println("ok");
157 
158         out.print("testing with serialize mode... ");
159         out.flush();
160         {
161             Condition cond = new Condition(true);
162             int value = 0;
163             Ice.AsyncResult result = null;
164             while(value < 3000 && cond.value())
165             {
166                 result = holdSerialized.begin_set(value + 1, random.nextInt(1), new AMICheckSetValue(cond, value));
167                 ++value;
168                 if(value % 100 == 0)
169                 {
170                     result.waitForSent();
171                 }
172             }
173             result.waitForCompleted();
174             test(cond.value());
175 
176             for(int i = 0; i < 10000; ++i)
177             {
178                 holdSerializedOneway.setOneway(value + 1, value);
179                 ++value;
180                 if((i % 100) == 0)
181                 {
182                     holdSerializedOneway.putOnHold(1);
183                 }
184             }
185         }
186         out.println("ok");
187 
188         out.print("testing serialization... ");
189         out.flush();
190         {
191             int value = 0;
192             holdSerialized.set(value, 0);
193             Ice.AsyncResult result = null;
194             int max = helper.isAndroid() ? 5000 : 10000;
195             for(int i = 0; i < max; ++i)
196             {
197                 // Create a new proxy for each request
198                 result = ((HoldPrx)holdSerialized.ice_oneway()).begin_setOneway(value + 1, value);
199                 ++value;
200                 if((i % 100) == 0)
201                 {
202                     result.waitForSent();
203                     holdSerialized.ice_ping(); // Ensure everything's dispatched
204                     holdSerialized.ice_getConnection().close(Ice.ConnectionClose.GracefullyWithWait);
205                 }
206             }
207             result.waitForCompleted();
208         }
209         out.println("ok");
210 
211         out.print("testing waitForHold... ");
212         out.flush();
213         {
214             hold.waitForHold();
215             hold.waitForHold();
216             for(int i = 0; i < 1000; ++i)
217             {
218                 holdOneway.ice_ping();
219                 if((i % 20) == 0)
220                 {
221                     hold.putOnHold(0);
222                 }
223             }
224             hold.putOnHold(-1);
225             hold.ice_ping();
226             hold.putOnHold(-1);
227             hold.ice_ping();
228         }
229         out.println("ok");
230 
231         out.print("changing state to hold and shutting down server... ");
232         out.flush();
233         hold.shutdown();
234         out.println("ok");
235     }
236 }
237