1 25 26 package org.objectweb.jonas.jtests.clients.timer; 27 28 import javax.ejb.TimerHandle ; 29 import org.objectweb.jonas.jtests.beans.transacted.Simple; 30 import org.objectweb.jonas.jtests.util.JTestCase; 31 32 38 public abstract class A_Timer extends JTestCase { 39 40 44 public A_Timer(String name) { 45 super(name); 46 } 47 48 52 protected void setUp() { 53 super.setUp(); 54 useBeans("transacted", true); 55 } 56 57 63 public abstract Simple getSimple(int i) throws Exception ; 64 65 69 72 public void testTimer1() throws Exception { 73 int duration = 5; 74 Simple s = getSimple(10); 75 try { 76 int oldval = s.getTimerCount(); 77 int id = s.setTimer(duration, 0); 78 sleep(2000); 79 assertEquals("timer expired too quickly", oldval, s.getTimerCount()); 80 sleep(4000); 81 assertEquals("timer did not expired", oldval + 1, s.getTimerCount()); 82 } finally { 83 s.remove(); 84 } 85 } 86 87 90 public void testTimerHandle1() throws Exception { 91 int duration = 5; 92 Simple s = getSimple(12); 93 try { 94 int oldval = s.getTimerCount(); 95 int id = s.setTimerGetHandle(duration, 0); 96 sleep(2000); 97 assertEquals("timer expired too quickly", oldval, s.getTimerCount()); 98 sleep(4000); 99 assertEquals("timer did not expired", oldval + 1, s.getTimerCount()); 100 } finally { 101 s.remove(); 102 } 103 } 104 105 108 public void testPeriodicTimerHandle1() throws Exception { 109 int duration = 5; 110 int period = 200; 111 Simple s = getSimple(12); 112 try { 113 int oldval = s.getTimerCount(); 114 int id = s.setTimerGetHandle(duration, period); 115 sleep(2000); 116 assertEquals("timer expired too quickly", oldval, s.getTimerCount()); 117 sleep(4000); 118 assertEquals("timer did not expired", oldval + 1, s.getTimerCount()); 119 } finally { 120 s.remove(); 121 } 122 } 123 124 127 public void testTimeRemaining() throws Exception { 128 int duration1 = 5000; 129 int duration2 = 2000; 130 Simple s = getSimple(11); 131 try { 132 int id1 = s.setTimer(duration1, 0); 133 int id2 = s.setTimer(duration2, 0); 134 long t1 = s.getTimeRemaining(id1) / 1000; 135 long t2 = s.getTimeRemaining(id2) / 1000; 136 sleep(1000); 137 assertTrue(t1 < duration1); 138 assertTrue(t2 < duration2); 139 assertTrue(t1 > duration1 - 2); 140 assertTrue(t2 > duration2 - 2); 141 } finally { 142 s.remove(); 143 } 144 } 145 146 149 public void testCancel1() throws Exception { 150 int duration = 2; 151 Simple s = getSimple(12); 152 try { 153 int oldval = s.getTimerCount(); 154 int id = s.setTimer(duration, 0); 155 assertEquals("timer expired too quickly", oldval, s.getTimerCount()); 156 s.cancelTimer(id); 157 sleep(1000 * (duration + 1)); 158 assertEquals("timer did expired", oldval, s.getTimerCount()); 159 } finally { 160 s.remove(); 161 } 162 } 163 164 167 public void testGetTimers() throws Exception { 168 Simple s = getSimple(20); 169 int [] ids = new int[10]; 170 int before = s.getTimerNumber(); 171 try { 172 for (int i = 0; i < 10; i++) { 173 ids[i] = s.setTimer(i + 1, 100); 174 } 175 sleep(1000); 176 assertEquals("Bad number of timers", 10, s.getTimerNumber() - before); 177 for (int i = 0; i < 10; i++) { 178 s.cancelTimer(ids[i]); 179 } 180 } finally { 181 s.remove(); 182 } 183 } 184 185 189 public void testGetTimerCancelled() throws Exception { 190 Simple s = getSimple(20); 191 int [] ids = new int[10]; 192 int before = s.getTimerNumber(); 193 try { 194 for (int i = 0; i < 10; i++) { 195 ids[i] = s.setTimer(i + 1, 100); 196 } 197 sleep(1000); 198 s.cancelTimer(ids[0]); 199 assertEquals("Bad number of timers", 9, s.getTimerNumber() - before); 200 for (int i = 1; i < 10; i++) { 201 s.cancelTimer(ids[i]); 202 } 203 } finally { 204 s.remove(); 205 } 206 } 207 208 212 public void testGetTimerCancelledInTx() throws Exception { 213 Simple s = getSimple(20); 214 int [] ids = new int[10]; 215 int before = s.getTimerNumber(); 216 utx.begin(); 218 try { 219 for (int i = 0; i < 10; i++) { 220 ids[i] = s.setTimer(i + 1, 100); 221 } 222 sleep(1000); 223 s.cancelTimer(ids[0]); 224 assertEquals("Bad number of timers", 9, s.getTimerNumber() - before); 225 for (int i = 1; i < 10; i++) { 226 s.cancelTimer(ids[i]); 227 } 228 } finally { 229 utx.commit(); 230 s.remove(); 231 } 232 } 233 234 237 public void testGetHandle1() throws Exception { 238 int duration = 2; 239 Simple s = getSimple(30); 240 try { 241 int id = s.setTimer(duration, 0); 242 TimerHandle th = s.getTimerHandle(id); 243 assertTrue(th != null); 244 } finally { 245 s.remove(); 246 } 247 } 248 249 252 public void testGetExpiredHandle1() throws Exception { 253 int duration = 1; 254 Simple s = getSimple(30); 255 try { 256 int id = s.setTimer(duration, 0); 257 sleep(3000); 258 TimerHandle th = s.getTimerHandle(id); 259 assertTrue(th == null); 260 } finally { 261 s.remove(); 262 } 263 } 264 } 265 | Popular Tags |