| 1 17 18 package org.sape.carbon.services.scheduler.test; 19 20 import org.sape.carbon.core.component.Component; 21 import org.sape.carbon.core.component.FunctionalInterface; 22 import org.sape.carbon.core.component.Lookup; 23 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 24 import org.sape.carbon.core.config.Config; 25 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor; 26 import org.sape.carbon.core.util.calendar.DayOfWeekEnum; 27 import org.sape.carbon.core.util.calendar.MonthEnum; 28 import org.sape.carbon.services.scheduler.FixedDelayTaskConfiguration; 29 import org.sape.carbon.services.scheduler.FixedRateTaskConfiguration; 30 import org.sape.carbon.services.scheduler.SchedulerServiceConfiguration; 31 import org.sape.carbon.services.threadpool.ThreadPool; 32 33 import junit.extensions.ActiveTestSuite; 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 47 public class SchedulerServiceTest 48 extends TestCase 49 implements SchedulerServiceTestListener { 50 51 private int count = 0; 52 private int secondaryCount = 0; 53 private int lastCount = 0; 54 55 58 public synchronized void incrementCount() { 59 count++; 60 notify(); 61 } 62 63 public synchronized void incrementSecondaryCount() { 64 secondaryCount++; 65 notify(); 66 } 67 68 public SchedulerServiceTest(String name) { 69 super(name); 70 } 71 72 public void testFixedRateTask() throws InterruptedException { 73 SchedulableComponent component = 74 (SchedulableComponent) Lookup.getInstance().fetchComponent( 75 SCHEDULABLE_SERVICE_NAME); 76 77 component.setListener(this); 78 79 setFixedRateTask(null, null, null, null, null, component, null); 80 81 synchronized (this) { 82 this.lastCount = this.count; 83 wait(120000); 85 if (this.count == this.lastCount) { 86 TestCase.fail( 87 "Waited 2 minutes and scheduled task was not executed: " 88 + "last call count [" 89 + this.lastCount 90 + "] current call count [ " 91 + this.count 92 + "] expected current count != last count"); 93 } 94 } 95 96 Lookup.getInstance().getComponentKeeper().destroyComponent( 97 TEST_SCHEDULER_SERVICE_NAME); 98 } 99 100 101 public void testReflectionBasedFixedRateTask() throws InterruptedException { 102 SchedulableComponent component = 103 (SchedulableComponent) Lookup.getInstance().fetchComponent( 104 SCHEDULABLE_SERVICE_NAME); 105 106 component.setListener(this); 107 108 setFixedRateTask(null, null, null, null, null, component, "doSpecializeTask"); 109 110 synchronized (this) { 111 this.lastCount = this.secondaryCount; 112 wait(120000); 114 assertTrue( 115 "Waited 2 minutes and scheduled task was not executed: " 116 + "last call count [" 117 + this.lastCount 118 + "] current call count [ " 119 + this.secondaryCount 120 + "] expected current count != last count", 121 this.secondaryCount != this.lastCount); 122 123 } 124 125 Lookup.getInstance().getComponentKeeper().destroyComponent( 126 TEST_SCHEDULER_SERVICE_NAME); 127 } 128 129 public void testSchedulerWithThreadPool() throws InterruptedException { 130 Component scheduler = 131 Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME); 132 133 ThreadPool threadPool = 134 (ThreadPool) Lookup.getInstance().fetchComponent(THREAD_POOL); 135 136 assertTrue( 137 "Thread pool was not empty at start of test", 138 threadPool.getPoolSize().equals(new Integer (0))); 139 140 ((SchedulerServiceConfiguration) scheduler).setThreadPool(threadPool); 141 ((ConfigurationInterceptor) scheduler).applyConfiguration(); 142 143 SchedulableComponent component = 144 (SchedulableComponent) Lookup.getInstance().fetchComponent( 145 SCHEDULABLE_SERVICE_NAME); 146 147 component.setListener(this); 148 149 setFixedRateTask(null, null, null, null, null, component, null); 150 151 synchronized (this) { 152 this.lastCount = this.count; 153 wait(120000); 155 if (this.count == this.lastCount) { 156 TestCase.fail( 157 "Waited 2 minutes and scheduled task was not executed: " 158 + "last call count [" 159 + this.lastCount 160 + "] current call count [ " 161 + this.count 162 + "] expected current count != last count"); 163 } 164 165 assertTrue( 166 "task was not executed on thread pool", 167 threadPool.getPoolSize().equals(new Integer (1))); 168 } 169 170 Lookup.getInstance().getComponentKeeper().destroyComponent( 171 TEST_SCHEDULER_SERVICE_NAME); 172 } 173 174 175 public void testFixedDelayTask() throws InterruptedException { 176 SchedulableComponent component = 177 (SchedulableComponent) Lookup.getInstance().fetchComponent( 178 SCHEDULABLE_SERVICE_NAME); 179 180 component.setListener(this); 181 182 setFixedDelayTask(0, 500, component,null); 183 184 synchronized (this) { 185 this.lastCount = this.count; 186 wait(1000); 188 if (this.count == this.lastCount) { 189 TestCase.fail( 190 "Waited 1 second and scheduled task was not executed: " 191 + "last call count [" 192 + this.lastCount 193 + "] current call count [ " 194 + this.count 195 + "] expected current count != last count"); 196 } 197 } 198 199 Lookup.getInstance().getComponentKeeper().destroyComponent( 200 TEST_SCHEDULER_SERVICE_NAME); 201 } 202 203 public void testInvalidConfigurations() { 204 SchedulableComponent component = 205 (SchedulableComponent) Lookup.getInstance().fetchComponent( 206 SCHEDULABLE_SERVICE_NAME); 207 208 testInvalidFixedRateTask(new Integer (0), null, null, null, null, null,null); 209 210 testInvalidFixedRateTask( 211 null, 212 new Integer (0), 213 null, 214 null, 215 null, 216 component, 217 null); 218 219 testInvalidFixedRateTask( 220 new Integer (0), 221 null, 222 new Integer (0), 223 null, 224 null, 225 component, 226 null); 227 228 testInvalidFixedRateTask( 229 new Integer (0), 230 new Integer (0), 231 null, 232 MonthEnum.JANUARY, 233 null, 234 component, 235 null); 236 237 testInvalidFixedRateTask( 238 new Integer (0), 239 new Integer (0), 240 new Integer (1), 241 null, 242 DayOfWeekEnum.MONDAY, 243 component, 244 null); 245 246 testInvalidFixedRateTask( 247 new Integer (0), 248 new Integer (0), 249 null, 250 MonthEnum.JANUARY, 251 DayOfWeekEnum.MONDAY, 252 component, 253 null); 254 255 testInvalidFixedRateTask( 256 null, null, null, null, null, 257 component, 258 "nonExistantMethod"); 259 260 testInvalidFixedDelayTask(-1, 100, component); 261 testInvalidFixedDelayTask(0, -1, component); 262 } 263 264 public void testRecoverableExceptions() throws InterruptedException { 265 SchedulableComponent component = 266 (SchedulableComponent) Lookup.getInstance().fetchComponent( 267 SchedulerServiceTest.RECOVERABLE_SCHEDULABLE_SERVICE_NAME); 268 269 component.setListener(this); 270 271 setFixedDelayTask(0, 500, component, null); 272 273 synchronized (this) { 274 this.lastCount = this.count; 275 wait(1000); 278 wait(1000); 279 if (this.count <= this.lastCount + 1) { 280 TestCase.fail( 281 "Recoverable exception caused timer to stop: " 282 + "last call count [" 283 + this.lastCount 284 + "] current call count [ " 285 + this.count 286 + "] expected <= last count + 1"); 287 } 288 } 289 290 Lookup.getInstance().getComponentKeeper().destroyComponent( 291 TEST_SCHEDULER_SERVICE_NAME); 292 } 293 294 public void testUnrecoverableExceptions() throws InterruptedException { 295 SchedulableComponent component = 296 (SchedulableComponent) Lookup.getInstance().fetchComponent( 297 SchedulerServiceTest.UNRECOVERABLE_SCHEDULABLE_SERVICE_NAME); 298 299 component.setListener(this); 300 301 setFixedDelayTask(0, 500, component, null); 302 303 synchronized (this) { 304 this.lastCount = this.count; 305 wait(1000); 308 wait(1000); 309 if (this.count != this.lastCount + 1) { 310 TestCase.fail( 311 "Unrecoverable exception did not caused task to be canceled: " 312 + "last call count [" 313 + this.lastCount 314 + "] current call count [ " 315 + this.count 316 + "] expected current count == last count + 1"); 317 } 318 } 319 320 Lookup.getInstance().getComponentKeeper().destroyComponent( 321 TEST_SCHEDULER_SERVICE_NAME); 322 } 323 324 private void testInvalidFixedRateTask( 325 Integer minute, 326 Integer hour, 327 Integer dayOfMonth, 328 MonthEnum month, 329 DayOfWeekEnum dayOfWeek, 330 FunctionalInterface component, 331 String method) { 332 333 try { 334 setFixedRateTask( 335 minute, 336 hour, 337 dayOfMonth, 338 month, 339 dayOfWeek, 340 component, 341 method); 342 fail( 343 "Did not catch expected StateTransitionException for " 344 + "configuration values: Minute [" 345 + minute 346 + "], Hour [" 347 + hour 348 + "], DayOfMonth [" 349 + dayOfMonth 350 + "], Month [" 351 + month 352 + "], DayOfWeek [" 353 + dayOfWeek 354 + "], SchedulableComponent [" 355 + component 356 + "]"); 357 358 } catch (StateTransitionException ste) { 359 } 361 362 } 363 364 private void testInvalidFixedDelayTask( 365 long delay, 366 long period, 367 FunctionalInterface component) { 368 369 try { 370 setFixedDelayTask(delay, period, component, null); 371 fail( 372 "Did catch expected StateTransitionException for " 373 + "configuration values: InitialDelay [" 374 + delay 375 + "], Period [" 376 + period 377 + "], SchedulableComponent [" 378 + component 379 + "]"); 380 381 } catch (StateTransitionException ste) { 382 } 384 385 } 386 387 private void setFixedRateTask( 388 Integer minute, 389 Integer hour, 390 Integer dayOfMonth, 391 MonthEnum month, 392 DayOfWeekEnum dayOfWeek, 393 FunctionalInterface component, 394 String methodName) { 395 396 Component scheduler = 397 Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME); 398 399 FixedRateTaskConfiguration newConfig = 400 (FixedRateTaskConfiguration) Config 401 .getInstance() 402 .createConfiguration( 403 FixedRateTaskConfiguration.class); 404 newConfig.setSchedulableComponent((FunctionalInterface) component); 405 406 newConfig.setMinute(minute); 407 newConfig.setHour(hour); 408 newConfig.setDayOfMonth(dayOfMonth); 409 newConfig.setMonth(month); 410 newConfig.setDayOfWeek(dayOfWeek); 411 if (methodName != null) 412 newConfig.setScheduledMethod(methodName); 413 414 ((SchedulerServiceConfiguration) scheduler).setFixedRateTask( 415 new FixedRateTaskConfiguration[] { newConfig }); 416 ((SchedulerServiceConfiguration) scheduler).setFixedDelayTask(null); 417 ((ConfigurationInterceptor) scheduler).applyConfiguration(); 418 } 419 420 private void setFixedDelayTask( 421 long delay, 422 long period, 423 FunctionalInterface component, 424 String methodName) { 425 426 Component scheduler = 427 Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME); 428 429 FixedDelayTaskConfiguration newConfig = 430 (FixedDelayTaskConfiguration) Config 431 .getInstance() 432 .createConfiguration( 433 FixedDelayTaskConfiguration.class); 434 newConfig.setSchedulableComponent((FunctionalInterface) component); 435 436 if (methodName != null) 437 newConfig.setScheduledMethod(methodName); 438 439 newConfig.setInitialDelay(delay); 440 newConfig.setPeriod(period); 441 442 ((SchedulerServiceConfiguration) scheduler).setFixedRateTask(null); 443 ((SchedulerServiceConfiguration) scheduler).setFixedDelayTask( 444 new FixedDelayTaskConfiguration[] { newConfig }); 445 ((ConfigurationInterceptor) scheduler).applyConfiguration(); 446 } 447 448 459 460 private static final String SCHEDULABLE_SERVICE_NAME = 461 "/scheduler/test/TestSchedulableComponent"; 462 private static final String UNRECOVERABLE_SCHEDULABLE_SERVICE_NAME = 463 "/scheduler/test/TestUnrecoverableSchedulableComponent"; 464 private static final String RECOVERABLE_SCHEDULABLE_SERVICE_NAME = 465 "/scheduler/test/TestRecoverableSchedulableComponent"; 466 private static final String TEST_SCHEDULER_SERVICE_NAME = 467 "/scheduler/test/TestScheduler"; 468 private static final String THREAD_POOL = 469 "/scheduler/test/SchedulerThreadPool"; 470 471 475 public static Test suite() { 476 TestSuite masterSuite = new TestSuite(); 477 Test singleThreadedTests = getSingleThreadedTests(); 479 if (singleThreadedTests != null) { 480 masterSuite.addTest(singleThreadedTests); 481 } 482 Test multiThreadedTests = getMultiThreadedTests(); 484 if (multiThreadedTests != null) { 485 masterSuite.addTest(multiThreadedTests); 486 } 487 return masterSuite; 488 } 489 490 496 private static Test getSingleThreadedTests() { 497 TestSuite suite = new TestSuite(); 498 499 suite.addTest(new SchedulerServiceTest("testFixedRateTask")); 500 suite.addTest(new SchedulerServiceTest("testReflectionBasedFixedRateTask")); 501 suite.addTest(new SchedulerServiceTest("testSchedulerWithThreadPool")); 502 suite.addTest(new SchedulerServiceTest("testFixedDelayTask")); 503 suite.addTest(new SchedulerServiceTest("testRecoverableExceptions")); 504 suite.addTest(new SchedulerServiceTest("testUnrecoverableExceptions")); 505 suite.addTest(new SchedulerServiceTest("testInvalidConfigurations")); 506 507 return suite; 508 } 509 510 515 private static Test getMultiThreadedTests() { 516 TestSuite suite = new ActiveTestSuite(); 517 518 524 525 return suite; 526 } 527 528 536 private static void addTest(TestSuite suite, String testName, int number) { 537 for (int count = 0; count < number; count++) { 538 suite.addTest(new SchedulerServiceTest(testName)); 539 } 540 } 541 } | Popular Tags |