use of org.springframework.amqp.rabbit.junit.LogLevels in project spring-amqp by spring-projects.
the class RabbitTemplateIntegrationTests method testChannelCloseInTx.
@Test
@LogLevels(classes = RabbitTemplate.class, categories = "foo", level = "DEBUG")
public void testChannelCloseInTx() throws Exception {
this.connectionFactory.setPublisherReturns(false);
Channel channel = this.connectionFactory.createConnection().createChannel(true);
RabbitResourceHolder holder = new RabbitResourceHolder(channel, true);
TransactionSynchronizationManager.bindResource(this.connectionFactory, holder);
try {
this.template.setChannelTransacted(true);
this.template.convertAndSend(ROUTE, "foo");
// force channel close
this.template.convertAndSend(UUID.randomUUID().toString(), ROUTE, "xxx");
await().until(() -> !channel.isOpen());
try {
this.template.convertAndSend(ROUTE, "bar");
fail("Expected Exception");
} catch (UncategorizedAmqpException e) {
if (e.getCause() instanceof IllegalStateException) {
assertThat(e.getCause().getMessage()).isEqualTo("Channel closed during transaction");
} else {
fail("Expected IllegalStateException not" + e.getCause());
}
} catch (AmqpConnectException e) {
assertThat(e.getCause()).isInstanceOf(AlreadyClosedException.class);
}
} finally {
TransactionSynchronizationManager.unbindResource(this.connectionFactory);
}
channel.close();
}
use of org.springframework.amqp.rabbit.junit.LogLevels in project spring-amqp by spring-projects.
the class RabbitAdminTests method testMultiEntities.
@Test
@LogLevels(classes = RabbitAdmin.class, level = "DEBUG")
public void testMultiEntities() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("e1", "k1", "foo");
template.convertAndSend("e2", "k2", "bar");
template.convertAndSend("e3", "k3", "baz");
template.convertAndSend("e4", "k4", "qux");
assertThat(template.receiveAndConvert("q1")).isEqualTo("foo");
assertThat(template.receiveAndConvert("q2")).isEqualTo("bar");
assertThat(template.receiveAndConvert("q3")).isEqualTo("baz");
assertThat(template.receiveAndConvert("q4")).isEqualTo("qux");
RabbitAdmin admin = ctx.getBean(RabbitAdmin.class);
admin.deleteQueue("q1");
admin.deleteQueue("q2");
admin.deleteQueue("q3");
admin.deleteQueue("q4");
admin.deleteExchange("e1");
admin.deleteExchange("e2");
admin.deleteExchange("e3");
admin.deleteExchange("e4");
assertThat(admin.getQueueProperties(ctx.getBean(Config.class).prototypeQueueName)).isNull();
Declarables mixedDeclarables = ctx.getBean("ds", Declarables.class);
assertThat(mixedDeclarables.getDeclarablesByType(Queue.class)).hasSize(1).extracting(Queue::getName).contains("q4");
assertThat(mixedDeclarables.getDeclarablesByType(Exchange.class)).hasSize(1).extracting(Exchange::getName).contains("e4");
assertThat(mixedDeclarables.getDeclarablesByType(Binding.class)).hasSize(1).extracting(Binding::getDestination).contains("q4");
ctx.close();
}
use of org.springframework.amqp.rabbit.junit.LogLevels in project spring-amqp by spring-projects.
the class PublisherCallbackChannelTests method testNotCached.
@Test
@LogLevels(classes = { CachingConnectionFactory.class, AbstractConnectionFactory.class, PublisherCallbackChannelImpl.class })
void testNotCached() throws Exception {
CachingConnectionFactory cf = new CachingConnectionFactory(RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
cf.setPublisherConfirmType(ConfirmType.CORRELATED);
cf.setChannelCacheSize(2);
cf.afterPropertiesSet();
RabbitTemplate template = new RabbitTemplate(cf);
CountDownLatch confirmLatch = new CountDownLatch(2);
template.setConfirmCallback((correlationData, ack, cause) -> {
try {
Thread.sleep(50);
confirmLatch.countDown();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
CountDownLatch openedLatch = new CountDownLatch(2);
CountDownLatch closeLatch = new CountDownLatch(1);
CountDownLatch closedLatch = new CountDownLatch(2);
CountDownLatch waitForOtherLatch = new CountDownLatch(1);
SimpleAsyncTaskExecutor exec = new SimpleAsyncTaskExecutor();
IntStream.range(0, 2).forEach(i -> {
// this will open 3 or 4 channels
exec.execute(() -> {
template.execute(chann -> {
openedLatch.countDown();
template.convertAndSend("", "foo", "msg", msg -> {
if (i == 0) {
try {
waitForOtherLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
waitForOtherLatch.countDown();
}
return msg;
}, new CorrelationData("" + i));
closeLatch.await();
return null;
});
closedLatch.countDown();
});
});
assertThat(openedLatch.await(10, TimeUnit.SECONDS)).isTrue();
Connection conn = cf.createConnection();
Channel chan1 = conn.createChannel(false);
Channel chan2 = conn.createChannel(false);
chan1.close();
chan2.close();
await().until(() -> {
Properties props = cf.getCacheProperties();
return Integer.parseInt(props.getProperty("idleChannelsNotTx")) == 2;
});
closeLatch.countDown();
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(closedLatch.await(10, TimeUnit.SECONDS)).isTrue();
await().until(() -> {
Properties props = cf.getCacheProperties();
return Integer.parseInt(props.getProperty("idleChannelsNotTx")) == 2;
});
}
Aggregations