001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_Serviceable
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derbyTesting.unitTests.harness.T_Generic;
025: import org.apache.derbyTesting.unitTests.harness.T_Fail;
026:
027: import org.apache.derby.iapi.services.context.Context;
028: import org.apache.derby.iapi.services.context.ContextManager;
029: import org.apache.derby.iapi.services.monitor.Monitor;
030: import org.apache.derby.iapi.error.StandardException;
031: import org.apache.derby.iapi.services.daemon.*;
032:
033: /**
034: This test implements serviceable for testing. To facility testing, when
035: this object is being serviced, it will synchronize on itself and notity all
036: waiters. Test driver may wait on this object and check timesServiced to
037: make sure the background daemon has run.
038: */
039: public class T_Serviceable implements Serviceable {
040: // synchronized on this to look at the number
041: // of times this object has been serviced
042: protected int timesServiced;
043:
044: // constant for checking
045: protected final int timesRequeue;
046: protected final boolean onDemandOnly;
047: protected final boolean subscribed;
048:
049: // use this to unsubscribe
050: protected int clientNumber;
051:
052: // test enqueueing, t = number of times to requeue
053: public T_Serviceable(int t) {
054: timesServiced = 0;
055: timesRequeue = t;
056: onDemandOnly = false; // not looked at
057: subscribed = false;
058: clientNumber = -1;
059: }
060:
061: // test subscription
062: public T_Serviceable(boolean onDemandOnly) {
063: timesServiced = 0;
064: timesRequeue = 0; // not looked at
065: this .onDemandOnly = onDemandOnly;
066: subscribed = true;
067: }
068:
069: protected void setClientNumber(int n) {
070: clientNumber = n;
071: }
072:
073: protected int getClientNumber() {
074: return clientNumber;
075: }
076:
077: /*
078: * Serviceable interface
079: */
080: public synchronized int performWork(ContextManager context) {
081: context.toString(); // make sure context manager is not null;
082:
083: timesServiced++;
084: notifyAll(); // notify anyone waiting for me to be serviced
085:
086: if (!subscribed && timesRequeue > timesServiced)
087: return Serviceable.REQUEUE;
088: else
089: return Serviceable.DONE;
090: }
091:
092: public boolean serviceASAP() {
093: return true;
094: }
095:
096: // @return true, if this work needs to be done on a user thread immediately
097: public boolean serviceImmediately() {
098: return false;
099: }
100:
101: /*
102: * test utilities
103: */
104:
105: protected synchronized void t_wait(int n) {
106: try {
107: while (timesServiced < n)
108: wait();
109: } catch (InterruptedException ie) {
110: }
111: }
112:
113: protected synchronized void t_check(int n) throws T_Fail {
114: if (timesServiced != n)
115: throw T_Fail.testFailMsg("Expect to be serviced " + n
116: + " times, instead serviced " + timesServiced);
117: }
118:
119: }
|