KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_DaemonService


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_DaemonService
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Fail;
25 import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
26 import org.apache.derby.iapi.services.context.ContextService;
27 import org.apache.derby.iapi.services.monitor.Monitor;
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.iapi.services.daemon.*;
30
31 import java.util.Random JavaDoc;
32 import java.util.Vector JavaDoc;
33 /**
34     This test exercices the DaemonFactory and DaemonService implementation
35 */

36 public class T_DaemonService extends T_MultiThreadedIterations
37 {
38     private static DaemonService testDaemon;
39     private static Random JavaDoc random;
40
41     /*
42      * fields for testing serviceable, one per test object
43      */

44     private Vector JavaDoc serviceRecord; // a vectory of T_Serviceable
45

46     public T_DaemonService()
47     {
48         super();
49         serviceRecord = new Vector JavaDoc(9, 1);
50         random = new Random JavaDoc();
51     }
52
53
54     /*
55     ** Methods required by T_Generic
56     */

57
58     protected String JavaDoc getModuleToTestProtocolName() {
59         return org.apache.derby.iapi.reference.Module.DaemonFactory;
60     }
61
62     /**
63     ** Methods required by T_MultiIterations
64     ** @exception T_Fail unexpected behaviour from the API
65     */

66     protected void setupTest() throws T_Fail
67     {
68
69         DaemonFactory daemonFactory;
70         try {
71             daemonFactory = (DaemonFactory)Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory);
72         } catch (StandardException mse) {
73             throw T_Fail.exceptionFail(mse);
74         }
75         if (daemonFactory == null)
76             throw T_Fail.testFailMsg("cannot find daemon factory " + org.apache.derby.iapi.reference.Module.DaemonFactory);
77             
78         try
79         {
80             testDaemon = daemonFactory.createNewDaemon("testDaemon");
81         }
82         catch (StandardException se)
83         {
84             throw T_Fail.exceptionFail(se);
85         }
86         if (testDaemon == null)
87             throw T_Fail.testFailMsg("cannot create new Daemon Service");
88
89
90     }
91
92
93     /**
94     ** Methods required by T_MultiThreadedIterations
95     ** @exception T_Fail unexpected behaviour from the API
96     */

97     protected void joinSetupTest() throws T_Fail
98     {
99         if (testDaemon == null)
100             throw T_Fail.testFailMsg("test deamon not set");
101     }
102
103     protected T_MultiThreadedIterations newTestObject()
104     {
105         return new T_DaemonService(); // clone myself
106
}
107
108
109     /**
110         @exception T_Fail - test failed
111     */

112     protected void runTestSet() throws T_Fail
113     {
114         // we don't want t_checkStatus() to hang because of
115
// unsubscribed records from a previous, failed iteration
116
// (DERBY-989)
117
serviceRecord.clear();
118
119         try
120         {
121             /* test basic DaemonService interface */
122             T01(testDaemon); // basic subscription
123
T02(testDaemon); // basic enqueue
124
T03(testDaemon); // mixture of everything
125

126             t_checkStatus(testDaemon); // make sure all serviceables I created got serviced
127
}
128         catch (StandardException se)
129         {
130             throw T_Fail.exceptionFail(se);
131         }
132
133     }
134
135     /*
136      * tests
137      */

138
139     /* test 1 - basic subscription */
140     private void T01(DaemonService daemon) throws T_Fail, StandardException
141     {
142         // add a couple of subscriptions to the deamon
143
T_Serviceable s1 = new T_Serviceable(false); // not on demand
144
serviceRecord.addElement(s1);
145         int clientNumber1 = daemon.subscribe(s1, false);
146         s1.setClientNumber(clientNumber1);
147
148         T_Serviceable s2 = new T_Serviceable(true); // on demand only
149
serviceRecord.addElement(s2);
150         int clientNumber2 = daemon.subscribe(s2, true);
151         s2.setClientNumber(clientNumber2);
152
153         daemon.serviceNow(clientNumber2); // s2 should be serviced exactly once
154

155         s2.t_wait(1); // wait for s2 to be serviced
156

157         randomSleep();
158
159         // don't demand service, let daemon service it by itself
160
s1.t_wait(1); // wait for s1 to be serviced
161

162         s2.t_check(1); // s2 should be serviced exactly once
163

164         PASS("T01");
165
166         randomSleep();
167     }
168
169     /* test 1 - basic enqueue */
170     private void T02(DaemonService daemon) throws T_Fail, StandardException
171     {
172         int requeue = 10;
173
174         T_Serviceable e1 = new T_Serviceable(1); // service now and don't requeue
175
serviceRecord.addElement(e1);
176         daemon.enqueue(e1, true);
177
178         T_Serviceable e2 = new T_Serviceable(requeue); // service now and requeue
179
serviceRecord.addElement(e2);
180         daemon.enqueue(e2, true);
181
182         T_Serviceable e3 = new T_Serviceable(1); // don't requeue
183
serviceRecord.addElement(e3);
184         daemon.enqueue(e3, false);
185
186         T_Serviceable e4 = new T_Serviceable(requeue); // requeue
187
serviceRecord.addElement(e4);
188         daemon.enqueue(e4, false);
189
190         randomSleep();
191
192         e1.t_wait(1); // make sure they are all serviced at least once
193
e2.t_wait(1);
194         e3.t_wait(1);
195         e4.t_wait(1);
196
197         e2.t_wait(requeue); // e2 and e4 are requeued
198
e4.t_wait(requeue); // e2 and e4 are requeued
199

200         // meanwhile, e1 and e3 should not be service more than once
201
e1.t_check(1);
202         e3.t_check(1);
203
204         PASS("T02");
205
206         randomSleep();
207     }
208
209     /* test 4 - mixture */
210     private void T03(DaemonService daemon) throws T_Fail, StandardException
211     {
212         T_Serviceable s1 = new T_Serviceable(false); // unsubscribe this laster
213
serviceRecord.addElement(s1);
214         int sub1 = daemon.subscribe(s1, false);
215
216         T_Serviceable e1 = new T_Serviceable(1);
217         serviceRecord.addElement(e1);
218         daemon.enqueue(e1, false); // enqueue the same thing 5 times
219
daemon.enqueue(e1, false);
220         daemon.enqueue(e1, false);
221         daemon.enqueue(e1, false);
222         daemon.enqueue(e1, false);
223
224         T_Serviceable s2 = new T_Serviceable(false); // not on demand
225
serviceRecord.addElement(s2);
226         int sub2 = daemon.subscribe(s2, false);
227         int realsub2 = daemon.subscribe(s2, false);
228         s2.setClientNumber(realsub2);
229
230         daemon.unsubscribe(sub1);
231         daemon.unsubscribe(sub2); // it has another subscriptions
232

233         int save;
234         synchronized(s1)
235         {
236             save = s1.timesServiced;
237         }
238         daemon.serviceNow(sub1); // should be silently igored
239

240         randomSleep();
241
242         e1.t_wait(5); // it is enqueued 5 times, it should be serviced 5 times
243

244         daemon.serviceNow(sub1); // should be silently igored
245

246         s2.t_wait(3); // wait long enough for it to be serviced at least 3 times
247

248         daemon.serviceNow(sub1); // should be silently igored
249

250         synchronized(s1)
251         {
252             // DERBY-989: The client should not be serviced after it
253
// unsubscribes. However, it might have been in the
254
// process of being serviced when unsubscribe() was
255
// called. Therefore, performWork() can run even after the
256
// save variable was initialized, but only once.
257
int diff = s1.timesServiced - save;
258             // Check that the client has not been serviced more than
259
// once after it unsubscribed.
260
T_Fail.T_ASSERT((diff == 0 || diff == 1),
261                             "unsubscribed continue to get serviced");
262
263             // unsubscribed can subscribe again
264
s1.timesServiced = 0;
265         }
266         
267         sub1 = daemon.subscribe(s1, false); // resubscribe
268
s1.setClientNumber(sub1);
269         daemon.serviceNow(sub1);
270         s1.t_wait(1);
271
272         // e1 should not be serviced for > 5 times
273
e1.t_check(5);
274
275         PASS("T03");
276         randomSleep();
277
278     }
279
280     private void t_checkStatus(DaemonService daemon) throws T_Fail
281     {
282         for (int i = 0; i < serviceRecord.size(); i++)
283         {
284             T_Serviceable check = (T_Serviceable)serviceRecord.elementAt(i);
285             if (check != null)
286             {
287                 if (check.subscribed)
288                 {
289                     if (check.onDemandOnly)
290                         check.t_check(1);
291                     else
292                         check.t_wait(10); // sooner or later, it will be serviced this many times
293

294                     daemon.unsubscribe(check.getClientNumber());
295                 }
296                 else // enqueued
297
{
298                     check.t_wait(check.timesRequeue);
299                 }
300             }
301         }
302         PASS("T_CheckStatus");
303     }
304
305     private void randomSleep()
306          throws StandardException
307     {
308         // randomly sleep for a bit if this is a multi-threaded test to make it more interesting
309
if (getNumThreads() > 1)
310         {
311             int nap = random.nextInt()%100;
312             if (nap < 0) nap = -nap;
313             try
314             {
315                 Thread.sleep(nap);
316             }
317             catch (InterruptedException JavaDoc ie)
318             {
319                 throw StandardException.interrupt(ie);
320             }
321         }
322     }
323
324 }
325
Popular Tags