001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Timer;
021: import java.util.TimerTask;
022:
023: public class TimerTaskTest extends junit.framework.TestCase {
024: Object sync = new Object(), start = new Object();
025:
026: /**
027: * Warning: These tests have the possibility to leave a VM hanging if the
028: * Timer is not cancelled.
029: */
030: class TimerTestTask extends TimerTask {
031: private int wasRun = 0;
032:
033: // Set this to true to see normal tests fail (or hang possibly)
034: // The default is false and needs to be set by some tests
035: private boolean sleepInRun = false;
036:
037: public void run() {
038: synchronized (this ) {
039: wasRun++;
040: }
041: synchronized (start) {
042: start.notify();
043: }
044: if (sleepInRun) {
045:
046: try {
047: Thread.sleep(200);
048: } catch (InterruptedException e) {
049: }
050: }
051: synchronized (sync) {
052: sync.notify();
053: }
054: }
055:
056: public synchronized int wasRun() {
057: return wasRun;
058: }
059:
060: public void sleepInRun(boolean value) {
061: sleepInRun = value;
062: }
063: }
064:
065: /**
066: * @tests java.util.TimerTask#TimerTask()
067: */
068: public void test_Constructor() {
069: // Ensure the constructor does not fail
070: new TimerTestTask();
071: }
072:
073: /**
074: * @tests java.util.TimerTask#cancel()
075: */
076: public void test_cancel() {
077: Timer t = null;
078: try {
079: // Ensure cancel returns false if never scheduled
080: TimerTestTask testTask = new TimerTestTask();
081: assertTrue(
082: "Unsheduled tasks should return false for cancel()",
083: !testTask.cancel());
084:
085: // Ensure cancelled task never runs
086: t = new Timer();
087: testTask = new TimerTestTask();
088: t.schedule(testTask, 500);
089: assertTrue("TimerTask should not have run yet", testTask
090: .cancel());
091: t.cancel();
092:
093: // Ensure cancelling a task which has already run returns true
094: t = new Timer();
095: testTask = new TimerTestTask();
096: t.schedule(testTask, 50);
097: while (testTask.wasRun() == 0) {
098: try {
099: Thread.sleep(150);
100: } catch (InterruptedException e) {
101: }
102: }
103: assertFalse(
104: "TimerTask.cancel() should return false if task has run",
105: testTask.cancel());
106: assertFalse(
107: "TimerTask.cancel() should return false if called a second time",
108: testTask.cancel());
109: t.cancel();
110:
111: // Ensure cancelling a repeated execution task which has never run
112: // returns true
113: t = new Timer();
114: testTask = new TimerTestTask();
115: t.schedule(testTask, 500, 500); // should never run
116: assertTrue(
117: "TimerTask.cancel() should return true if sheduled for repeated execution even if not run",
118: testTask.cancel());
119: t.cancel();
120:
121: // Ensure cancelling a repeated execution task which HAS run returns
122: // true
123: t = new Timer();
124: testTask = new TimerTestTask();
125: t.schedule(testTask, 50, 50);
126: while (testTask.wasRun() == 0) {
127: try {
128: Thread.sleep(100);
129: } catch (InterruptedException e) {
130: }
131: }
132: assertTrue(
133: "TimerTask.cancel() should return true if sheduled for repeated execution and run",
134: testTask.cancel());
135: t.cancel();
136:
137: // Ensure calling cancel a second returns false
138: t = new Timer();
139: testTask = new TimerTestTask();
140: t.schedule(testTask, 5000); // Should never run
141: assertTrue(
142: "TimerTask.cancel() should return true if task has never run",
143: testTask.cancel());
144: assertFalse(
145: "TimerTask.cancel() should return false if called a second time",
146: testTask.cancel());
147: t.cancel();
148:
149: // Ensure cancelling a task won't cause deadlock
150: t = new Timer();
151: testTask = new TimerTestTask();
152: testTask.sleepInRun(true);
153: synchronized (start) {
154: t.schedule(testTask, 0);
155: try {
156: start.wait();
157: Thread.sleep(50);
158: } catch (InterruptedException e) {
159: }
160: }
161: assertFalse("TimerTask should have been cancelled",
162: testTask.cancel());
163: t.cancel();
164: } finally {
165: if (t != null)
166: t.cancel();
167: }
168: }
169:
170: /**
171: * @tests java.util.TimerTask#scheduledExecutionTime()
172: */
173: public void test_scheduledExecutionTime() {
174: Timer t = null;
175: try {
176: // Ensure scheduledExecutionTime is roughly right
177: t = new Timer();
178: TimerTestTask testTask = new TimerTestTask();
179: t.schedule(testTask, 100);
180: long time = System.currentTimeMillis() + 100;
181: synchronized (sync) {
182: try {
183: sync.wait(500);
184: } catch (InterruptedException e) {
185: }
186: }
187: long scheduledExecutionTime = testTask
188: .scheduledExecutionTime();
189: assertTrue(scheduledExecutionTime <= time);
190: t.cancel();
191:
192: // Ensure scheduledExecutionTime is the last scheduled time
193: t = new Timer();
194: testTask = new TimerTestTask();
195: t.schedule(testTask, 100, 500);
196: long estNow = System.currentTimeMillis() + 100;
197: // Will wake in 100, and every 500 run again
198: // We want to try to get it after it's run at least once but not
199: // twice
200: synchronized (sync) {
201: try {
202: sync.wait(500);
203: } catch (InterruptedException e) {
204: }
205: }
206: scheduledExecutionTime = testTask.scheduledExecutionTime();
207: assertTrue(scheduledExecutionTime <= estNow);
208: t.cancel();
209: } finally {
210: if (t != null)
211: t.cancel();
212: }
213:
214: }
215:
216: /**
217: * @tests java.util.TimerTask#run()
218: */
219: public void test_run() {
220: Timer t = null;
221: try {
222: // Ensure a new task is never run
223: TimerTestTask testTask = new TimerTestTask();
224: try {
225: Thread.sleep(200);
226: } catch (InterruptedException e) {
227: }
228: assertEquals(
229: "TimerTask.run() method should not have been called",
230: 0, testTask.wasRun());
231:
232: // Ensure a task is run
233: t = new Timer();
234: testTask = new TimerTestTask();
235: t.schedule(testTask, 200);
236: while (testTask.wasRun() < 1) {
237: try {
238: Thread.sleep(400);
239: } catch (InterruptedException e) {
240: }
241: }
242: assertFalse(testTask.cancel());
243: t.cancel();
244: } finally {
245: if (t != null)
246: t.cancel();
247: }
248:
249: }
250: }
|