@Test public void getMultipleInstances() throws Exception { // Arrange TestBean[] beans = new TestBean[2]; Thread thread1 = new Thread(() -> beans[0] = applicationContext.getBean("threadScopedObject", TestBean.class)); Thread thread2 = new Thread(() -> beans[1] = applicationContext.getBean("threadScopedObject", TestBean.class)); // Act thread1.start(); thread2.start(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> (beans[0] != null) && (beans[1] != null)); assertNotSame(beans[0], beans[1]); }
@Test public void submitListenableCallable() throws Exception { TestCallable task = new TestCallable(1); // Act ListenableFuture<String> future = executor.submitListenable(task); future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> future.isDone() && outcome != null); assertEquals(THREAD_NAME_PREFIX, outcome.toString().substring(0, THREAD_NAME_PREFIX.length())); }
/** Returns a configured {@link ConditionFactory} that polls at a short interval. */ public static ConditionFactory await() { return Awaitility.with() .pollDelay(1, TimeUnit.MILLISECONDS).and() .pollInterval(1, TimeUnit.MILLISECONDS); } }
@Test public void asyncMethodListener() throws Exception { // Arrange originalThreadName = Thread.currentThread().getName(); listenerCalled = 0; GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodListener.class)); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); // Act context.refresh(); // Assert Awaitility.await() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> listenerCalled == 1); context.close(); }
@Test public void asyncClassListener() throws Exception { // Arrange originalThreadName = Thread.currentThread().getName(); listenerCalled = 0; listenerConstructed = 0; GenericApplicationContext context = new GenericApplicationContext(); context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncClassListener.class)); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); // Act context.refresh(); context.close(); // Assert Awaitility.await() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> listenerCalled == 2); assertEquals(1, listenerConstructed); }
@Test // SPR-14949 public void findOnInterfaceWithCglibProxy() throws InterruptedException { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class); AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class); // Act asyncBean.work(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(()-> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); }
@Test // SPR-14949 public void findOnInterfaceWithInterfaceProxy() throws InterruptedException { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class); AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class); // Act asyncBean.work(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); }
private void waitForTicks(int minTicks) { try { Awaitility.with() .pollInterval(1, TimeUnit.MILLISECONDS) .atMost(Testing.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS) .untilAsserted(() -> assertThat(tickTupleCount.get(), Matchers.greaterThanOrEqualTo(minTicks))); } catch (ConditionTimeoutException e) { throw new AssertionError(e.getMessage()); } }
@Test public void customExecutorBeanConfigWithThrowsException() { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomExecutorBeanConfig.class, ExecutorPostProcessor.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler) ctx.getBean("exceptionHandler"); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); Method method = ReflectionUtils.findMethod(AsyncBean.class, "fail"); // Act asyncBean.fail(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); ctx.close(); }
@Test public void asyncPrototypeClassListener() throws Exception { // Arrange originalThreadName = Thread.currentThread().getName(); listenerCalled = 0; listenerConstructed = 0; GenericApplicationContext context = new GenericApplicationContext(); RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class); listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); context.registerBeanDefinition("asyncTest", listenerDef); context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class)); context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class)); // Act context.refresh(); context.close(); // Assert Awaitility.await() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> listenerCalled == 2); assertEquals(2, listenerConstructed); }
@Test public void customExecutorConfigWithThrowsException() { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomExecutorConfig.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); Method method = ReflectionUtils.findMethod(AsyncBean.class, "fail"); TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler) ctx.getBean("exceptionHandler"); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); // Act asyncBean.fail(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); ctx.close(); }
public static void assertLoop(Predicate<Object> condition, Object... conditionParams) { try { Awaitility.with() .pollInterval(1, TimeUnit.MILLISECONDS) .atMost(Testing.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS) .untilAsserted(() -> assertThat(Arrays.asList(conditionParams), everyItem(matchesPredicate(condition)))); } catch (ConditionTimeoutException e) { throw new AssertionError(e.getMessage()); } }
@Test public void customExecutorBean() throws InterruptedException { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomExecutorBean.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); // Act asyncBean.work(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); }
@Test public void customExecutorConfig() { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomExecutorConfig.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); // Act asyncBean.work(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); }
@Test public void customExecutorBeanConfig() throws InterruptedException { // Arrange AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomExecutorBeanConfig.class, ExecutorPostProcessor.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); // Act asyncBean.work(); // Assert Awaitility.await() .atMost(500, TimeUnit.MILLISECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Post-")); ctx.close(); }
@Test public void submitFailingListenableRunnable() throws Exception { TestTask task = new TestTask(0); ListenableFuture<?> future = executor.submitListenable(task); future.addCallback(result -> outcome = result, ex -> outcome = ex); Awaitility.await() .dontCatchUncaughtExceptions() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> future.isDone() && outcome != null); assertSame(RuntimeException.class, outcome.getClass()); }
@Test public void submitFailingListenableCallable() throws Exception { TestCallable task = new TestCallable(0); // Act ListenableFuture<String> future = executor.submitListenable(task); future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() .dontCatchUncaughtExceptions() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(() -> future.isDone() && outcome != null); assertSame(RuntimeException.class, outcome.getClass()); }
@Test public void submitListenableRunnable() throws Exception { TestTask task = new TestTask(1); // Act ListenableFuture<?> future = executor.submitListenable(task); future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() .atMost(1, TimeUnit.SECONDS) .pollInterval(10, TimeUnit.MILLISECONDS) .until(future::isDone); assertNull(outcome); assertThreadNamePrefix(task); }
@Test(timeout = 2000) public void awaitOperationSupportsSpecifyingZeroAsPollDelay() { new Asynch(fakeRepository).perform(); with().pollDelay(Duration.ZERO).pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).then().await().until(fakeRepositoryValueEqualsOne()); assertEquals(1, fakeRepository.getValue()); }
@Test(timeout = 2000) public void awaitOperationDoesntSupportSpecifyingForeverAsPollInterval() { exception.expect(IllegalArgumentException.class); exception.expectMessage("Cannot use a fixed poll interval of length 'forever'"); new Asynch(fakeRepository).perform(); with().pollDelay(Duration.ONE_HUNDRED_MILLISECONDS).pollInterval(Duration.FOREVER).then().await().until(fakeRepositoryValueEqualsOne()); assertEquals(1, fakeRepository.getValue()); }