@Test @DirtiesContext public void testReplyAfterReceiveTimeout() throws Exception { this.asyncTemplate.setReceiveTimeout(100); RabbitConverterFuture<String> future = this.asyncTemplate.convertSendAndReceive("sleep"); TheCallback callback = new TheCallback(); future.addCallback(callback); assertEquals(1, TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class).size()); try { future.get(10, TimeUnit.SECONDS); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(AmqpReplyTimeoutException.class)); } assertEquals(0, TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class).size()); assertTrue(callback.latch.await(10, TimeUnit.SECONDS)); assertThat(callback.ex, instanceOf(AmqpReplyTimeoutException.class)); /* * Test there's no harm if the reply is received after the timeout. This * is unlikely to happen because the future is removed from the pending * map when it times out. However, there is a small race condition where * the reply arrives at the same time as the timeout. */ future.set("foo"); assertNull(callback.result); }
@Test @DirtiesContext public void testStopCancelled() throws Exception { this.asyncTemplate.setReceiveTimeout(5000); RabbitConverterFuture<String> future = this.asyncTemplate.convertSendAndReceive("noReply"); TheCallback callback = new TheCallback(); future.addCallback(callback); assertEquals(1, TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class).size()); this.asyncTemplate.stop(); // Second stop() to be sure that it is idempotent this.asyncTemplate.stop(); try { future.get(10, TimeUnit.SECONDS); fail("Expected CancellationException"); } catch (CancellationException e) { assertEquals("AsyncRabbitTemplate was stopped while waiting for reply", future.getNackCause()); } assertEquals(0, TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class).size()); assertTrue(callback.latch.await(10, TimeUnit.SECONDS)); assertTrue(future.isCancelled()); assertNull(TestUtils.getPropertyValue(this.asyncTemplate, "taskScheduler")); /* * Test there's no harm if the reply is received after the cancel. This * should never happen because the container is stopped before canceling * and the future is removed from the pending map. */ future.set("foo"); assertNull(callback.result); }
private void verifyFooBarBazQux(RabbitConverterFuture<Foo<Bar<Baz, Qux>>> future) throws Exception { verifyFooBarBazQux(future.get(10, TimeUnit.SECONDS)); }
@Test public void testAsyncListener() throws Exception { assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo")); RabbitConverterFuture<Object> future = this.asyncTemplate.convertSendAndReceive(this.queue1.getName(), "foo"); assertEquals("FOO", future.get(10, TimeUnit.SECONDS)); assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue2.getName(), "foo")); }