@Override public void assertValid() { context.assertValid(); }
@Override public void run( final int iterations, final boolean stopOnFinish, final boolean initialize, final long runWait) { if (iterations < 1) { throw new IllegalArgumentException(); } context.assertValid(); context.enableExpressionValidation(); try { if (initialize) { try { ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, processor, context); } catch (final Exception e) { e.printStackTrace(); Assert.fail( "Could not invoke methods annotated with @OnScheduled annotation due to: " + e); } } final ExecutorService executorService = Executors.newFixedThreadPool(numThreads); @SuppressWarnings("unchecked") final Future<Throwable>[] futures = new Future[iterations]; for (int i = 0; i < iterations; i++) { final Future<Throwable> future = executorService.submit(new RunProcessor()); futures[i] = future; } executorService.shutdown(); try { executorService.awaitTermination(runWait, TimeUnit.MILLISECONDS); } catch (final InterruptedException e1) { } int finishedCount = 0; boolean unscheduledRun = false; for (final Future<Throwable> future : futures) { try { final Throwable thrown = future.get(); // wait for the result if (thrown != null) { throw new AssertionError(thrown); } if (++finishedCount == 1) { unscheduledRun = true; try { ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, processor, context); } catch (final Exception e) { Assert.fail( "Could not invoke methods annotated with @OnUnscheduled annotation due to: " + e); } } } catch (final Exception e) { } } if (!unscheduledRun) { try { ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, processor, context); } catch (final Exception e) { Assert.fail( "Could not invoke methods annotated with @OnUnscheduled annotation due to: " + e); } } if (stopOnFinish) { try { ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, processor, context); } catch (final Exception e) { Assert.fail("Could not invoke methods annotated with @OnStopped annotation due to: " + e); } } } finally { context.disableExpressionValidation(); } }