1 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 40 public class TimeoutFactoryTestCase extends JBossTestCase 41 { 42 public TimeoutFactoryTestCase(String name) 43 { 44 super(name); 45 } 46 47 WaitableInt count = new WaitableInt(0); 48 49 public void testBlocking() throws Exception 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 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 )tt); 68 } 69 count.whenEqual(times, null); 70 assertEquals(times, count.get()); 71 } 72 73 public void testConsecutiveTimeouts() throws Exception 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 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 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 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 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 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 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 ("Fail"); 183 184 try 185 { 186 Thread.sleep(10); 187 } 188 catch (InterruptedException e) 189 { 190 } 191 count.increment(); 192 } 193 } 194 } 195 | Popular Tags |