001: //========================================================================
002: //$Id: TimeoutTest.java,v 1.1 2005/10/05 14:09:42 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.thread;
017:
018: import junit.framework.TestCase;
019:
020: public class TimeoutTest extends TestCase {
021: Timeout timeout = new Timeout();
022: Timeout.Task[] tasks;
023:
024: /* ------------------------------------------------------------ */
025: /*
026: * @see junit.framework.TestCase#setUp()
027: */
028: protected void setUp() throws Exception {
029: super .setUp();
030:
031: timeout = new Timeout();
032: timeout.setDuration(1000000);
033: tasks = new Timeout.Task[10];
034:
035: for (int i = 0; i < tasks.length; i++) {
036: tasks[i] = new Timeout.Task();
037: timeout.setNow(1000 + i * 100);
038: timeout.schedule(tasks[i]);
039: }
040: timeout.setNow(100);
041: }
042:
043: /* ------------------------------------------------------------ */
044: /*
045: * @see junit.framework.TestCase#tearDown()
046: */
047: protected void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: /* ------------------------------------------------------------ */
052: public void testExpiry() {
053: timeout.setDuration(200);
054: timeout.setNow(1500);
055: timeout.tick();
056:
057: for (int i = 0; i < tasks.length; i++) {
058: assertEquals("isExpired " + i, i < 4, tasks[i].isExpired());
059: }
060: }
061:
062: /* ------------------------------------------------------------ */
063: public void testCancel() {
064: timeout.setDuration(200);
065: timeout.setNow(1700);
066:
067: for (int i = 0; i < tasks.length; i++)
068: if (i % 2 == 1)
069: tasks[i].cancel();
070:
071: timeout.tick();
072:
073: for (int i = 0; i < tasks.length; i++) {
074: assertEquals("isExpired " + i, i % 2 == 0 && i < 6,
075: tasks[i].isExpired());
076: }
077: }
078:
079: /* ------------------------------------------------------------ */
080: public void testTouch() {
081: timeout.setDuration(200);
082: timeout.setNow(1350);
083: tasks[2].reschedule();
084:
085: timeout.setNow(1500);
086: timeout.tick();
087: for (int i = 0; i < tasks.length; i++) {
088: assertEquals("isExpired " + i, i != 2 && i < 4, tasks[i]
089: .isExpired());
090: }
091:
092: timeout.setNow(1550);
093: timeout.tick();
094: for (int i = 0; i < tasks.length; i++) {
095: assertEquals("isExpired " + i, i < 4, tasks[i].isExpired());
096: }
097: }
098:
099: /* ------------------------------------------------------------ */
100: public void testDelay() {
101: Timeout.Task task = new Timeout.Task();
102:
103: timeout.setNow(1100);
104: timeout.schedule(task, 300);
105: timeout.setDuration(200);
106:
107: timeout.setNow(1300);
108: timeout.tick();
109: assertEquals("delay", false, task.isExpired());
110:
111: timeout.setNow(1500);
112: timeout.tick();
113: assertEquals("delay", false, task.isExpired());
114:
115: timeout.setNow(1700);
116: timeout.tick();
117: assertEquals("delay", true, task.isExpired());
118: }
119:
120: }
|