File: T_DaemonService.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (353 lines) | stat: -rw-r--r-- 9,486 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*

   Derby - Class org.apache.derbyTesting.unitTests.services.T_DaemonService

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.services;

import org.apache.derbyTesting.unitTests.harness.T_Fail;
import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
import org.apache.derby.iapi.services.context.ContextService;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.services.daemon.*;

import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Random;
import java.util.Vector;
/**
	This test exercices the DaemonFactory and DaemonService implementation
*/
public class T_DaemonService extends T_MultiThreadedIterations 
{
	private static DaemonService testDaemon;
	private static Random random;

	/*
	 * fields for testing serviceable, one per test object
	 */
	private Vector<T_Serviceable> serviceRecord;

	public T_DaemonService()
	{
		super();
		serviceRecord = new Vector<T_Serviceable>(9, 1);
		random = new Random();
	}


	/*
	** Methods required by T_Generic
	*/

	protected String getModuleToTestProtocolName() {
		return org.apache.derby.iapi.reference.Module.DaemonFactory;
	}

	/**
	** Methods required by T_MultiIterations
	** @exception T_Fail unexpected behaviour from the API
	*/
	protected void setupTest() throws T_Fail
	{

		DaemonFactory daemonFactory;
		try {
			daemonFactory = (DaemonFactory)startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory);
		} catch (StandardException mse) {
			throw T_Fail.exceptionFail(mse);
		}
		if (daemonFactory == null)
			throw T_Fail.testFailMsg("cannot find daemon factory " + org.apache.derby.iapi.reference.Module.DaemonFactory);
			
		try
		{
			testDaemon = daemonFactory.createNewDaemon("testDaemon");
		}
		catch (StandardException se)
		{
			throw T_Fail.exceptionFail(se);
		}
		if (testDaemon == null)
			throw T_Fail.testFailMsg("cannot create new Daemon Service");


	}


	/**
	** Methods required by T_MultiThreadedIterations
	** @exception T_Fail unexpected behaviour from the API
	*/
	protected void joinSetupTest() throws T_Fail
	{
		if (testDaemon == null)
			throw T_Fail.testFailMsg("test deamon not set");
	}

	protected T_MultiThreadedIterations newTestObject()
	{
		return new T_DaemonService(); // clone myself
	}


	/**
		@exception T_Fail - test failed
	*/
	protected void runTestSet() throws T_Fail
	{
		// we don't want t_checkStatus() to hang because of
		// unsubscribed records from a previous, failed iteration
		// (DERBY-989)
		serviceRecord.clear();

		try
		{
			/* test basic DaemonService interface */
			T01(testDaemon);	// basic subscription
			T02(testDaemon);	// basic enqueue
			T03(testDaemon);	// mixture of everything

			t_checkStatus(testDaemon);	// make sure all serviceables I created got serviced
		}
		catch (StandardException se)
		{
			throw T_Fail.exceptionFail(se);			
		}

	}

	/*
	 *  tests
	 */

	/* test 1 - basic subscription */
	private void T01(DaemonService daemon) throws T_Fail, StandardException
	{
		// add a couple of subscriptions to the deamon
		T_Serviceable s1 = new T_Serviceable(false);  // not on demand
		serviceRecord.addElement(s1);
		int clientNumber1 = daemon.subscribe(s1, false);
		s1.setClientNumber(clientNumber1);

		T_Serviceable s2 = new T_Serviceable(true);  // on demand only
		serviceRecord.addElement(s2);
		int clientNumber2 = daemon.subscribe(s2, true);
		s2.setClientNumber(clientNumber2);

		daemon.serviceNow(clientNumber2); // s2 should be serviced exactly once

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

		randomSleep();

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

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

		PASS("T01");

		randomSleep();
	}

	/* test 1 - basic enqueue */
	private void T02(DaemonService daemon) throws T_Fail, StandardException
	{
		int requeue = 10;

		T_Serviceable e1 = new T_Serviceable(1); // service now and don't requeue
		serviceRecord.addElement(e1);
		daemon.enqueue(e1, true);

		T_Serviceable e2 = new T_Serviceable(requeue); // service now and requeue
		serviceRecord.addElement(e2);
		daemon.enqueue(e2, true);

		T_Serviceable e3 = new T_Serviceable(1); // don't requeue
		serviceRecord.addElement(e3);
		daemon.enqueue(e3, false);

		T_Serviceable e4 = new T_Serviceable(requeue); // requeue
		serviceRecord.addElement(e4);
		daemon.enqueue(e4, false);

		randomSleep();

		e1.t_wait(1);				// make sure they are all serviced at least once
		e2.t_wait(1);
		e3.t_wait(1);
		e4.t_wait(1);

		e2.t_wait(requeue);	// e2 and e4 are requeued
		e4.t_wait(requeue);	// e2 and e4 are requeued

		// meanwhile, e1 and e3 should not be service more than once
		e1.t_check(1);
		e3.t_check(1);

		PASS("T02");

		randomSleep();
	}

	/* test 4 - mixture */
	private void T03(DaemonService daemon) throws T_Fail, StandardException
	{
		T_Serviceable s1 = new T_Serviceable(false);  // unsubscribe this laster
		serviceRecord.addElement(s1);
		int sub1 = daemon.subscribe(s1, false);

		T_Serviceable e1 = new T_Serviceable(1); 
		serviceRecord.addElement(e1);
		daemon.enqueue(e1, false); // enqueue the same thing 5 times
		daemon.enqueue(e1, false);
		daemon.enqueue(e1, false);
		daemon.enqueue(e1, false);
		daemon.enqueue(e1, false);

		T_Serviceable s2 = new T_Serviceable(false); // not on demand
		serviceRecord.addElement(s2);
		int sub2 = daemon.subscribe(s2, false); 
		int realsub2 = daemon.subscribe(s2, false); 
		s2.setClientNumber(realsub2);

		daemon.unsubscribe(sub1);
		daemon.unsubscribe(sub2); // it has another subscriptions

		int save;
		synchronized(s1)
		{
			save = s1.timesServiced;
		}
		daemon.serviceNow(sub1); // should be silently igored

		randomSleep();

		e1.t_wait(5);			// it is enqueued 5 times, it should be serviced 5 times

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

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

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

		synchronized(s1)
		{
			// DERBY-989: The client should not be serviced after it
			// unsubscribes. However, it might have been in the
			// process of being serviced when unsubscribe() was
			// called. Therefore, performWork() can run even after the
			// save variable was initialized, but only once.
			int diff = s1.timesServiced - save;
			// Check that the client has not been serviced more than
			// once after it unsubscribed.
			T_Fail.T_ASSERT((diff == 0 || diff == 1),
							"unsubscribed continue to get serviced");

			// unsubscribed can subscribe again
			s1.timesServiced = 0;
		}
		
		sub1 = daemon.subscribe(s1, false); // resubscribe
		s1.setClientNumber(sub1);
		daemon.serviceNow(sub1);
		s1.t_wait(1);

		// e1 should not be serviced for > 5 times
		e1.t_check(5);

		PASS("T03");
		randomSleep();

	}

	private void t_checkStatus(DaemonService daemon) throws T_Fail
	{
		for (int i = 0; i < serviceRecord.size(); i++)
		{
			T_Serviceable check = (T_Serviceable)serviceRecord.elementAt(i);
			if (check != null)
			{
				if (check.subscribed)
				{
					if (check.onDemandOnly)
						check.t_check(1);
					else
						check.t_wait(10); // sooner or later, it will be serviced this many times

					daemon.unsubscribe(check.getClientNumber());
				}
				else			// enqueued
				{
					check.t_wait(check.timesRequeue);
				}
			}
		}
		PASS("T_CheckStatus");
	}

	private void randomSleep()
		 throws StandardException
	{
		// randomly sleep for a bit if this is a multi-threaded test to make it more interesting
		if (getNumThreads() > 1)
		{
			int nap = random.nextInt()%100;
			if (nap < 0) nap = -nap;
			try
			{
				Thread.sleep(nap);
			}
			catch (InterruptedException ie)
			{
				throw StandardException.interrupt(ie);
			}
		}	
	}

    
    /**
     * Privileged startup. Must be private so that user code
     * can't call this entry point.
     */
    private  static  Object  startSystemModule( final String factoryInterface )
        throws StandardException
    {
        try {
            return AccessController.doPrivileged
                (
                 new PrivilegedExceptionAction<Object>()
                 {
                     public Object run()
                         throws StandardException
                     {
                         return Monitor.startSystemModule( factoryInterface );
                     }
                 }
                 );
        } catch (PrivilegedActionException pae)
        {
            throw StandardException.plainWrapException( pae );
        }
    }

}