asyncTemplate.setReceiveTimeout(10); assertThat(error.getPayload(), instanceOf(MessagingException.class)); assertThat(error.getPayload().getCause(), instanceOf(AmqpReplyTimeoutException.class)); asyncTemplate.setReceiveTimeout(30000); receiver.setMessageListener(messageListener);
@Test public void testConvert1ArgDirect() throws Exception { this.latch.set(new CountDownLatch(1)); ListenableFuture<String> future1 = this.asyncDirectTemplate.convertSendAndReceive("foo"); ListenableFuture<String> future2 = this.asyncDirectTemplate.convertSendAndReceive("bar"); this.latch.get().countDown(); checkConverterResult(future1, "FOO"); checkConverterResult(future2, "BAR"); this.latch.set(null); waitForZeroInUseConsumers(); assertThat(TestUtils .getPropertyValue(this.asyncDirectTemplate, "directReplyToContainer.consumerCount", Integer.class), equalTo(2)); final String missingQueue = UUID.randomUUID().toString(); this.asyncDirectTemplate.convertSendAndReceive("", missingQueue, "foo"); // send to nowhere this.asyncDirectTemplate.stop(); // should clear the inUse channel map waitForZeroInUseConsumers(); this.asyncDirectTemplate.start(); this.asyncDirectTemplate.setReceiveTimeout(1); this.asyncDirectTemplate.convertSendAndReceive("", missingQueue, "foo"); // send to nowhere waitForZeroInUseConsumers(); this.asyncDirectTemplate.setReceiveTimeout(10000); this.asyncDirectTemplate.convertSendAndReceive("", missingQueue, "foo").cancel(true); waitForZeroInUseConsumers(); }
@Test @DirtiesContext public void testReceiveTimeout() throws Exception { this.asyncTemplate.setReceiveTimeout(500); ListenableFuture<String> future = this.asyncTemplate.convertSendAndReceive("noReply"); 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 @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); }