KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_Serviceable
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_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.iapi.services.context.Context;
28 import org.apache.derby.iapi.services.context.ContextManager;
29 import org.apache.derby.iapi.services.monitor.Monitor;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.services.daemon.*;
32
33 /**
34     This test implements serviceable for testing. To facility testing, when
35     this object is being serviced, it will synchronize on itself and notity all
36     waiters. Test driver may wait on this object and check timesServiced to
37     make sure the background daemon has run.
38 */

39 public class T_Serviceable implements Serviceable
40 {
41     // synchronized on this to look at the number
42
// of times this object has been serviced
43
protected int timesServiced;
44
45     // constant for checking
46
protected final int timesRequeue;
47     protected final boolean onDemandOnly;
48     protected final boolean subscribed;
49
50     // use this to unsubscribe
51
protected int clientNumber;
52
53     // test enqueueing, t = number of times to requeue
54
public T_Serviceable(int t)
55     {
56         timesServiced = 0;
57         timesRequeue = t;
58         onDemandOnly = false; // not looked at
59
subscribed = false;
60         clientNumber = -1;
61     }
62
63     // test subscription
64
public T_Serviceable(boolean onDemandOnly)
65     {
66         timesServiced = 0;
67         timesRequeue = 0; // not looked at
68
this.onDemandOnly = onDemandOnly;
69         subscribed = true;
70     }
71
72     protected void setClientNumber(int n)
73     {
74         clientNumber = n;
75     }
76
77     protected int getClientNumber()
78     {
79         return clientNumber;
80     }
81
82     /*
83      * Serviceable interface
84      */

85     public synchronized int performWork(ContextManager context)
86     {
87         context.toString(); // make sure context manager is not null;
88

89         timesServiced++;
90         notifyAll(); // notify anyone waiting for me to be serviced
91

92         if (!subscribed && timesRequeue > timesServiced)
93             return Serviceable.REQUEUE;
94         else
95             return Serviceable.DONE;
96     }
97     
98     public boolean serviceASAP()
99     {
100         return true;
101     }
102
103
104     // @return true, if this work needs to be done on a user thread immediately
105
public boolean serviceImmediately()
106     {
107         return false;
108     }
109
110
111     /*
112      * test utilities
113      */

114
115     protected synchronized void t_wait(int n)
116     {
117         try
118         {
119             while (timesServiced < n)
120                 wait();
121         }
122         catch (InterruptedException JavaDoc ie) {}
123     }
124
125     protected synchronized void t_check(int n) throws T_Fail
126     {
127         if (timesServiced != n)
128             throw T_Fail.testFailMsg("Expect to be serviced " + n + " times, instead serviced " + timesServiced);
129     }
130
131 }
132
Popular Tags