@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]); }
private void block() { await().atMost(20, TimeUnit.SECONDS).untilTrue(unblock); unblock.set(false); } }
@Test public void closedIfCancelled() { assertThat(call.isCancelled()).isFalse(); completionFuture.completeExceptionally(ClosedSessionException.get()); await().untilAsserted(() -> assertThat(call.isCancelled()).isTrue()); } }
@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())); }
@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 public void notReadyAfterClose() { assertThat(call.isReady()).isTrue(); call.close(Status.OK, new Metadata()); await().untilAsserted(() -> assertThat(call.isReady()).isFalse()); }
@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(); }
@Test public void expirationTest() { Message m = new Message("test", Collections.singleton("test"), "info", Duration.ofMillis(10)); container.addMessage(m); await().atMost(2, TimeUnit.SECONDS).until(() -> container.getMessages("info").isEmpty()); }
@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(); }
@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 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 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 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 assertionErrorIgnoringWorksForHamcrestMatchers() { new Asynch(fakeRepository).perform(); await().atMost(1000, MILLISECONDS).with().ignoreExceptionsMatching(instanceOf(AssertionError.class)).until(conditionsThatIsThrowingAnExceptionForATime(AssertionError.class)); }