Search in sources :

Example 1 with LogLevels

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();
}
Also used : RabbitResourceHolder(org.springframework.amqp.rabbit.connection.RabbitResourceHolder) AmqpConnectException(org.springframework.amqp.AmqpConnectException) UncategorizedAmqpException(org.springframework.amqp.UncategorizedAmqpException) Channel(com.rabbitmq.client.Channel) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) Test(org.junit.jupiter.api.Test) LogLevels(org.springframework.amqp.rabbit.junit.LogLevels)

Example 2 with LogLevels

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Declarables(org.springframework.amqp.core.Declarables) Test(org.junit.jupiter.api.Test) LogLevels(org.springframework.amqp.rabbit.junit.LogLevels)

Example 3 with LogLevels

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;
    });
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) Channel(com.rabbitmq.client.Channel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) LogLevels(org.springframework.amqp.rabbit.junit.LogLevels)

Aggregations

Test (org.junit.jupiter.api.Test)3 LogLevels (org.springframework.amqp.rabbit.junit.LogLevels)3 Channel (com.rabbitmq.client.Channel)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AmqpConnectException (org.springframework.amqp.AmqpConnectException)1 UncategorizedAmqpException (org.springframework.amqp.UncategorizedAmqpException)1 Declarables (org.springframework.amqp.core.Declarables)1 RabbitResourceHolder (org.springframework.amqp.rabbit.connection.RabbitResourceHolder)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1