KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > test > TimeoutFactoryTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.util.test;
23
24 import org.jboss.test.JBossTestCase;
25 import org.jboss.util.threadpool.BasicThreadPool;
26 import org.jboss.util.threadpool.BlockingMode;
27 import org.jboss.util.timeout.Timeout;
28 import org.jboss.util.timeout.TimeoutFactory;
29 import org.jboss.util.timeout.TimeoutTarget;
30
31 import EDU.oswego.cs.dl.util.concurrent.WaitableInt;
32
33 /**
34  * Unit tests for TimeoutFactory class.
35  *
36  * @author <a HREF="mailto:genman@noderunner.net">Elias Ross</a>
37  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
38  * @version $Revision: 43490 $
39  */

40 public class TimeoutFactoryTestCase extends JBossTestCase
41 {
42    public TimeoutFactoryTestCase(String JavaDoc name)
43    {
44       super(name);
45    }
46
47    WaitableInt count = new WaitableInt(0);
48
49    public void testBlocking() throws Exception JavaDoc
50    {
51       final int times = 5000;
52       TT tt = new TT();
53       for (int i = 0; i < times; i++)
54       {
55          TimeoutFactory.createTimeout(0, tt);
56       }
57       count.whenEqual(times, null);
58       assertEquals(times, count.get());
59    }
60    public void testDefaultCtr() throws Exception JavaDoc
61    {
62       final int times = 5000;
63       TT tt = new TT();
64       TimeoutFactory tf = new TimeoutFactory();
65       for (int i = 0; i < times; i++)
66       {
67          tf.schedule(0, (Runnable JavaDoc)tt);
68       }
69       count.whenEqual(times, null);
70       assertEquals(times, count.get());
71    }
72
73    public void testConsecutiveTimeouts() throws Exception JavaDoc
74    {
75       final int times = 1000;
76       TT tt = new TT();
77       TimeoutFactory tf = new TimeoutFactory();
78       long now = System.currentTimeMillis();
79       for (int i = 0; i < 10; i++)
80       {
81          for (int j = 0; j < 100; j++)
82          {
83             tf.schedule(now + i*50, (TimeoutTarget)tt);
84          }
85       }
86       count.whenEqual(times, null);
87       assertEquals(times, count.get());
88    }
89    
90    public void testCancel() throws Exception JavaDoc
91    {
92       final int times = 100;
93       TT tt = new TT();
94       TimeoutFactory tf = new TimeoutFactory();
95       long at = System.currentTimeMillis() + 300;
96       for (int i = 0; i < times; i++)
97       {
98          Timeout t = tf.schedule(at, (TimeoutTarget)tt);
99          t.cancel();
100       }
101       Thread.sleep(500);
102       assertEquals(0, count.get());
103    }
104
105    public void testCancelFactory() throws Exception JavaDoc
106    {
107       final int times = 100;
108       TT tt = new TT();
109       TimeoutFactory tf = new TimeoutFactory();
110       long at = System.currentTimeMillis() + 300;
111       for (int i = 0; i < times; i++)
112       {
113          tf.schedule(at, (TimeoutTarget)tt);
114       }
115       tf.cancel();
116       Thread.sleep(500);
117       assertEquals(0, count.get());
118    }
119
120    public void testBlockingSmallThreadPool() throws Exception JavaDoc
121    {
122       final int times = 100;
123       BasicThreadPool tp = new BasicThreadPool();
124       tp.setMaximumQueueSize(1);
125       tp.setMaximumPoolSize(1);
126       tp.setBlockingMode(BlockingMode.RUN);
127       TT tt = new TT();
128       TimeoutFactory tf = new TimeoutFactory(tp);
129       for (int i = 0; i < times; i++)
130       {
131          tf.schedule(0, (TimeoutTarget)tt);
132       }
133       count.whenEqual(times, null);
134       assertEquals(times, count.get());
135    }
136    
137    public void testAbortingSmallThreadPool() throws Exception JavaDoc
138    {
139       final int times = 50;
140       BasicThreadPool tp = new BasicThreadPool();
141       tp.setMaximumQueueSize(1);
142       tp.setMaximumPoolSize(1);
143       TT tt = new TT();
144       TimeoutFactory tf = new TimeoutFactory(tp);
145       for (int i = 0; i < times; i++)
146       {
147          tf.schedule(0, (TimeoutTarget)tt);
148       }
149       Thread.sleep(500);
150       getLog().debug("Executed: " + count.get() + ", scheduled: " + times);
151       assertTrue("Executed " + count.get() + " < scheduled " + times, count.get() < times);
152    }
153    
154    public void testFailedTarget() throws Exception JavaDoc
155    {
156       final int times = 50;
157       TimeoutFactory tf = new TimeoutFactory();
158       TT tt = new TT();
159       tt.fail = true;
160       for (int i = 0; i < times; i++)
161       {
162          tf.schedule(0, (TimeoutTarget)tt);
163       }
164       Thread.sleep(500);
165       assertEquals(count.get(), 0);
166    }
167
168    class TT implements TimeoutTarget, Runnable JavaDoc
169    {
170
171       boolean fail;
172
173       public void timedOut(Timeout timeout)
174       {
175          assertTrue(timeout != null);
176          run();
177       }
178
179       public void run()
180       {
181          if (fail)
182             throw new Error JavaDoc("Fail");
183          
184          try
185          {
186             Thread.sleep(10);
187          }
188          catch (InterruptedException JavaDoc e)
189          {
190          }
191          count.increment();
192       }
193    }
194 }
195
Popular Tags