@Test public void testThrottleLimit() throws Exception { template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println(items); // System.err.println("Frequency: " + frequency); assertEquals(total, items.size() - frequency); assertTrue(frequency > 1); assertTrue(frequency <= throttleLimit + 1); }
@Test public void testThrottleLimitWithError() throws Exception { error = 50; try { template.iterate(callback); fail("Expected planned exception"); } catch (Exception e) { assertEquals("Planned", e.getMessage()); } int frequency = Collections.frequency(items, "null"); assertEquals(0, frequency); }
@Test public void testThrottleLimitEarlyFinish() throws Exception { early = 2; template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); assertEquals(total, items.size() - frequency); assertTrue(frequency > 1); assertTrue(frequency <= throttleLimit + 1); }
@Test public void testThrottleLimitWithEarlyCompletion() throws Exception { early = 2; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); template.iterate(callback); int frequency = Collections.frequency(items, "null"); assertEquals(10, items.size() - frequency); // System.err.println("Frequency: " + frequency); assertEquals(0, frequency); }
template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception {
template.iterate(callback);
jobTemplate.iterate(jobCallback);
@Test public void testErrorThrownByCallback() throws Exception { callback = new RepeatCallback() { private volatile AtomicInteger count = new AtomicInteger(0); @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { int position = count.incrementAndGet(); if(position == 4) { throw new OutOfMemoryError("Planned"); } else { return RepeatStatus.CONTINUABLE; } } }; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); try { template.iterate(callback); fail("Expected planned exception"); } catch (OutOfMemoryError oome) { assertEquals("Planned", oome.getMessage()); } catch (Exception e) { e.printStackTrace(); fail("Wrong exception was thrown: " + e); } }
@Test public void testThrottleLimitEarlyFinishOneThread() throws Exception { early = 4; SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(1); // This is kind of slow with only one thread, so reduce size: throttleLimit = 10; total = 20; template.setThrottleLimit(throttleLimit); template.setTaskExecutor(taskExecutor); template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); assertEquals(total, items.size() - frequency); assertTrue(frequency <= throttleLimit + 1); }
@Test public void testThrottleLimitEarlyFinishThreadStarvation() throws Exception { early = 2; ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); // Set the concurrency limit below the throttle limit for possible // starvation condition taskExecutor.setMaxPoolSize(20); taskExecutor.setCorePoolSize(10); taskExecutor.setQueueCapacity(0); // This is the most sensible setting, otherwise the bookkeeping in // ResultHolderResultQueue gets out of whack when tasks are aborted. taskExecutor .setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); taskExecutor.afterPropertiesSet(); template.setTaskExecutor(taskExecutor); template.iterate(callback); int frequency = Collections.frequency(items, "null"); // System.err.println("Frequency: " + frequency); // System.err.println("Items: " + items); // Extra tasks will be submitted before the termination is detected assertEquals(total, items.size() - frequency); assertTrue(frequency <= throttleLimit + 1); taskExecutor.destroy(); }
@Test public void testEarlyCompletionWithException() throws Exception { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); template.setCompletionPolicy(new SimpleCompletionPolicy(20)); taskExecutor.setConcurrencyLimit(2); template.setTaskExecutor(taskExecutor); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; throw new IllegalStateException("foo!"); } }); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertEquals("foo!", e.getMessage()); } assertTrue("Too few attempts: " + count, count >= 1); assertTrue("Too many attempts: " + count, count <= 10); }
@Test public void testExceptionHandlerSwallowsException() throws Exception { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); template.setCompletionPolicy(new SimpleCompletionPolicy(4)); taskExecutor.setConcurrencyLimit(2); template.setTaskExecutor(taskExecutor); template.setExceptionHandler(new ExceptionHandler() { @Override public void handleException(RepeatContext context, Throwable throwable) throws Throwable { count++; } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { throw new IllegalStateException("foo!"); } }); assertTrue("Too few attempts: " + count, count >= 1); assertTrue("Too many attempts: " + count, count <= 10); }