|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 20 |
|
21 |
| -import org.apache.logging.log4j.Level; |
22 |
| -import org.junit.After; |
23 |
| -import org.junit.Before; |
24 |
| -import org.junit.Rule; |
25 |
| -import org.junit.Test; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.CountDownLatch; |
| 24 | +import java.util.concurrent.ExecutorService; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.RepeatedTest; |
26 | 32 |
|
27 | 33 | import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
28 |
| -import org.springframework.amqp.rabbit.junit.BrokerRunning; |
29 | 34 | import org.springframework.amqp.rabbit.junit.BrokerTestUtils;
|
30 |
| -import org.springframework.amqp.rabbit.junit.LogLevelAdjuster; |
31 |
| -import org.springframework.amqp.rabbit.junit.LongRunningIntegrationTest; |
32 |
| -import org.springframework.amqp.rabbit.test.RepeatProcessor; |
33 |
| -import org.springframework.test.annotation.Repeat; |
| 35 | +import org.springframework.amqp.rabbit.junit.LogLevels; |
| 36 | +import org.springframework.amqp.rabbit.junit.RabbitAvailable; |
34 | 37 | import org.springframework.transaction.TransactionDefinition;
|
35 | 38 | import org.springframework.transaction.TransactionException;
|
36 | 39 | import org.springframework.transaction.support.AbstractPlatformTransactionManager;
|
|
44 | 47 | * @since 1.0
|
45 | 48 | *
|
46 | 49 | */
|
| 50 | +@RabbitAvailable(queues = RabbitTemplatePerformanceIntegrationTests.ROUTE) |
| 51 | +@LogLevels(level = "ERROR", classes = RabbitTemplate.class) |
47 | 52 | public class RabbitTemplatePerformanceIntegrationTests {
|
48 | 53 |
|
49 |
| - private static final String ROUTE = "test.queue"; |
| 54 | + public static final String ROUTE = "test.queue.RabbitTemplatePerformanceIntegrationTests"; |
50 | 55 |
|
51 |
| - private final RabbitTemplate template = new RabbitTemplate(); |
| 56 | + private static final RabbitTemplate template = new RabbitTemplate(); |
52 | 57 |
|
53 |
| - @Rule |
54 |
| - public LongRunningIntegrationTest longTests = new LongRunningIntegrationTest(); |
| 58 | + private static final ExecutorService exec = Executors.newFixedThreadPool(4); |
55 | 59 |
|
56 |
| - @Rule |
57 |
| - public RepeatProcessor repeat = new RepeatProcessor(4); |
| 60 | + private static CachingConnectionFactory connectionFactory; |
58 | 61 |
|
59 |
| - @Rule |
60 |
| - // After the repeat processor, so it only runs once |
61 |
| - public LogLevelAdjuster logLevels = new LogLevelAdjuster(Level.ERROR, RabbitTemplate.class); |
62 |
| - |
63 |
| - @Rule |
64 |
| - // After the repeat processor, so it only runs once |
65 |
| - public BrokerRunning brokerIsRunning = BrokerRunning.isRunningWithEmptyQueues(ROUTE); |
66 |
| - |
67 |
| - private CachingConnectionFactory connectionFactory; |
68 |
| - |
69 |
| - @Before |
70 |
| - public void declareQueue() { |
71 |
| - if (repeat.isInitialized()) { |
72 |
| - // Important to prevent concurrent re-initialization |
73 |
| - return; |
74 |
| - } |
| 62 | + @BeforeAll |
| 63 | + public static void declareQueue() { |
75 | 64 | connectionFactory = new CachingConnectionFactory();
|
76 | 65 | connectionFactory.setHost("localhost");
|
77 |
| - connectionFactory.setChannelCacheSize(repeat.getConcurrency()); |
78 | 66 | connectionFactory.setPort(BrokerTestUtils.getPort());
|
79 | 67 | template.setConnectionFactory(connectionFactory);
|
80 | 68 | }
|
81 | 69 |
|
82 |
| - @After |
83 |
| - public void cleanUp() { |
84 |
| - if (!repeat.isFinalizing()) { |
85 |
| - return; |
86 |
| - } |
87 |
| - this.template.stop(); |
88 |
| - this.connectionFactory.destroy(); |
89 |
| - this.brokerIsRunning.removeTestQueues(); |
| 70 | + @AfterAll |
| 71 | + public static void cleanUp() { |
| 72 | + template.stop(); |
| 73 | + connectionFactory.destroy(); |
| 74 | + exec.shutdownNow(); |
90 | 75 | }
|
91 | 76 |
|
92 |
| - @Test |
93 |
| - @Repeat(200) |
94 |
| - public void testSendAndReceive() throws Exception { |
95 |
| - template.convertAndSend(ROUTE, "message"); |
96 |
| - String result = (String) template.receiveAndConvert(ROUTE); |
97 |
| - int count = 5; |
98 |
| - while (result == null && count-- > 0) { |
99 |
| - /* |
100 |
| - * Retry for the purpose of non-transacted case because channel operations are async in that case |
101 |
| - */ |
102 |
| - Thread.sleep(10L); |
103 |
| - result = (String) template.receiveAndConvert(ROUTE); |
104 |
| - } |
105 |
| - assertThat(result).isEqualTo("message"); |
| 77 | + @RepeatedTest(50) |
| 78 | + public void testSendAndReceive() throws InterruptedException { |
| 79 | + CountDownLatch latch = new CountDownLatch(4); |
| 80 | + List<String> results = new ArrayList<>(); |
| 81 | + Stream.of(1, 2, 3, 4).forEach(i -> exec.execute(() -> { |
| 82 | + template.convertAndSend(ROUTE, "message"); |
| 83 | + results.add((String) template.receiveAndConvert(ROUTE, 10_000L)); |
| 84 | + latch.countDown(); |
| 85 | + })); |
| 86 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 87 | + assertThat(results).contains("message", "message", "message", "message"); |
106 | 88 | }
|
107 | 89 |
|
108 |
| - @Test |
109 |
| - @Repeat(200) |
110 |
| - public void testSendAndReceiveTransacted() throws Exception { |
| 90 | + @RepeatedTest(50) |
| 91 | + public void testSendAndReceiveTransacted() throws InterruptedException { |
| 92 | + CountDownLatch latch = new CountDownLatch(4); |
| 93 | + List<String> results = new ArrayList<>(); |
111 | 94 | template.setChannelTransacted(true);
|
112 |
| - template.convertAndSend(ROUTE, "message"); |
113 |
| - String result = (String) template.receiveAndConvert(ROUTE); |
114 |
| - assertThat(result).isEqualTo("message"); |
| 95 | + Stream.of(1, 2, 3, 4).forEach(i -> exec.execute(() -> { |
| 96 | + template.convertAndSend(ROUTE, "message"); |
| 97 | + results.add((String) template.receiveAndConvert(ROUTE, 10_000L)); |
| 98 | + latch.countDown(); |
| 99 | + })); |
| 100 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 101 | + assertThat(results).contains("message", "message", "message", "message"); |
115 | 102 | }
|
116 | 103 |
|
117 |
| - @Test |
118 |
| - @Repeat(200) |
119 |
| - public void testSendAndReceiveExternalTransacted() throws Exception { |
| 104 | + @RepeatedTest(50) |
| 105 | + public void testSendAndReceiveExternalTransacted() throws InterruptedException { |
| 106 | + template.setChannelTransacted(true); |
| 107 | + CountDownLatch latch = new CountDownLatch(4); |
| 108 | + List<String> results = new ArrayList<>(); |
120 | 109 | template.setChannelTransacted(true);
|
121 |
| - new TransactionTemplate(new TestTransactionManager()).execute(status -> { |
| 110 | + Stream.of(1, 2, 3, 4).forEach(i -> exec.execute(() -> { |
| 111 | + new TransactionTemplate(new TestTransactionManager()).execute(status -> { |
| 112 | + template.convertAndSend(ROUTE, "message"); |
| 113 | + return null; |
| 114 | + }); |
122 | 115 | template.convertAndSend(ROUTE, "message");
|
123 |
| - return null; |
124 |
| - }); |
125 |
| - template.convertAndSend(ROUTE, "message"); |
126 |
| - String result = (String) template.receiveAndConvert(ROUTE); |
127 |
| - assertThat(result).isEqualTo("message"); |
| 116 | + results.add((String) template.receiveAndConvert(ROUTE, 10_000L)); |
| 117 | + latch.countDown(); |
| 118 | + })); |
| 119 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 120 | + assertThat(results).contains("message", "message", "message", "message"); |
128 | 121 | }
|
129 | 122 |
|
130 | 123 | @SuppressWarnings("serial")
|
|
0 commit comments