KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.timeout.Timeout;
26 import org.jboss.util.timeout.TimeoutFactory;
27 import org.jboss.util.timeout.TimeoutTarget;
28
29 import EDU.oswego.cs.dl.util.concurrent.WaitableLong;
30
31 /**
32  * TimeoutFactoryMixedStressTestCase.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 43750 $
36  */

37 public class TimeoutFactoryMixedStressTestCase extends JBossTestCase
38 {
39    WaitableLong count = new WaitableLong(0);
40    int iterationCount = getIterationCount();
41    int threadCount = getThreadCount();
42    long target = iterationCount * threadCount;
43    TimeoutFactory factory = TimeoutFactory.getSingleton();
44    
45    public void testStress() throws Exception JavaDoc
46    {
47       Thread JavaDoc[] threads = new Thread JavaDoc[threadCount];
48       for (int i = 0; i < threads.length; ++i)
49          threads[i] = new Thread JavaDoc(new MyRunnable(), "Test thread " + i);
50       for (int i = 0; i < threads.length; ++i)
51          threads[i].start();
52       count.whenEqual(target, null);
53    }
54
55    class MyRunnable implements Runnable JavaDoc
56    {
57       public void run()
58       {
59          for (int i = 0; i < iterationCount; ++i)
60          {
61             Timeout timeout = factory.createTimeout(System.currentTimeMillis() + 1, instance);
62             if (timeout.cancel())
63                count.increment();
64          }
65       }
66    }
67
68    MyTimeout instance = new MyTimeout();
69    
70    class MyTimeout implements TimeoutTarget
71    {
72       public void timedOut(Timeout timeout)
73       {
74          count.increment();
75       }
76    }
77
78    /**
79     * Create a new TimeoutFactoryStressTestCase.
80     *
81     * @param name the test name
82     */

83    public TimeoutFactoryMixedStressTestCase(String JavaDoc name)
84    {
85       super(name);
86    }
87 }
88
Popular Tags