Skip to content

Commit 71af6b1

Browse files
garyrussellartembilan
authored andcommittedJan 16, 2018
Improve Test Run Times
- Reduce container `receiveTimeout` so containers stop faster - Remove unnecessary `Thread.sleep()`s Reduced runtime from 5'26'' to 3'11'' on my machine.
1 parent b0cbc35 commit 71af6b1

7 files changed

+24
-17
lines changed
 

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerErrorHandlerIntegrationTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -127,6 +127,7 @@ public void testErrorHandlerThrowsARADRE() throws Exception {
127127
}
128128
throw new RuntimeException("bar");
129129
});
130+
container.setReceiveTimeout(50);
130131
container.start();
131132
Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class));
132133
doReturn(true).when(logger).isWarnEnabled();
@@ -217,6 +218,7 @@ public void testRejectingErrorHandler() throws Exception {
217218
admin.declareBinding(BindingBuilder.bind(dlq).to(dle).with(testQueueName));
218219

219220
container.setQueueNames(testQueueName);
221+
container.setReceiveTimeout(50);
220222
container.afterPropertiesSet();
221223
container.start();
222224

@@ -297,6 +299,7 @@ public void doTest(int messageCount, ErrorHandler errorHandler, CountDownLatch l
297299
container.setTxSize(messageCount);
298300
container.setQueueNames(queue.getName());
299301
container.setErrorHandler(errorHandler);
302+
container.setReceiveTimeout(50);
300303
container.afterPropertiesSet();
301304
container.start();
302305

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerLifecycleIntegrationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -239,6 +239,7 @@ private void doTest(MessageCount level, Concurrency concurrency, TransactionMode
239239
}
240240
container.setQueueNames(queue.getName());
241241
container.setShutdownTimeout(30000);
242+
container.setReceiveTimeout(50);
242243
container.afterPropertiesSet();
243244
container.start();
244245

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerContainerRetryIntegrationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,6 +230,7 @@ private void doTestRetry(int messageCount, int txSize, int failFrequency, int co
230230
container.setAdviceChain(new Advice[] { createRetryInterceptor(latch, stateful) });
231231

232232
container.setQueueNames(queue.getName());
233+
container.setReceiveTimeout(50);
233234
container.afterPropertiesSet();
234235
container.start();
235236

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/MessageListenerRecoveryCachingConnectionIntegrationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -467,6 +467,7 @@ protected SimpleMessageListenerContainer doCreateContainer(String queueName, Obj
467467
container.setAcknowledgeMode(acknowledgeMode);
468468
container.setRecoveryInterval(100);
469469
container.setFailedDeclarationRetryInterval(100);
470+
container.setReceiveTimeout(50);
470471
container.afterPropertiesSet();
471472
return container;
472473
}

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -130,8 +130,6 @@ public void declareQueues() {
130130

131131
@After
132132
public void clear() throws Exception {
133-
// Wait for broker communication to finish before trying to stop container
134-
Thread.sleep(300L);
135133
logger.debug("Shutting down at end of test");
136134
if (container != null) {
137135
container.shutdown();
@@ -144,12 +142,18 @@ public void clear() throws Exception {
144142
public void testChangeQueues() throws Exception {
145143
CountDownLatch latch = new CountDownLatch(30);
146144
container = createContainer(new MessageListenerAdapter(new PojoListener(latch)), queue.getName(), queue1.getName());
145+
final CountDownLatch consumerLatch = new CountDownLatch(1);
146+
this.container.setApplicationEventPublisher(e -> {
147+
if (e instanceof AsyncConsumerStoppedEvent) {
148+
consumerLatch.countDown();
149+
}
150+
});
147151
for (int i = 0; i < 10; i++) {
148152
template.convertAndSend(queue.getName(), i + "foo");
149153
template.convertAndSend(queue1.getName(), i + "foo");
150154
}
151155
container.addQueueNames(queue1.getName());
152-
Thread.sleep(1100); // allow current consumer to time out and terminate
156+
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS));
153157
for (int i = 0; i < 10; i++) {
154158
template.convertAndSend(queue.getName(), i + "foo");
155159
}
@@ -665,6 +669,7 @@ private SimpleMessageListenerContainer createContainer(Object listener, boolean
665669
if (queueNames != null) {
666670
container.setQueueNames(queueNames);
667671
}
672+
container.setReceiveTimeout(50);
668673
container.afterPropertiesSet();
669674
if (start) {
670675
container.start();

‎spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegrationTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -170,7 +170,6 @@ public void declareQueue() {
170170
@After
171171
public void clear() throws Exception {
172172
// Wait for broker communication to finish before trying to stop container
173-
Thread.sleep(300L);
174173
logger.debug("Shutting down at end of test");
175174
if (container != null) {
176175
container.shutdown();
@@ -255,11 +254,7 @@ private void doListenerWithExceptionTest(CountDownLatch latch, Object listener)
255254
assertTrue("Timed out waiting for message", waited);
256255
}
257256
finally {
258-
// Wait for broker communication to finish before trying to stop
259-
// container
260-
Thread.sleep(300L);
261257
container.shutdown();
262-
Thread.sleep(300L);
263258
}
264259
if (acknowledgeMode.isTransactionAllowed()) {
265260
assertNotNull(template.receiveAndConvert(queue.getName()));
@@ -297,6 +292,7 @@ private SimpleMessageListenerContainer doCreateContainer(Object listener) {
297292
container.setChannelTransacted(transactional);
298293
container.setAcknowledgeMode(acknowledgeMode);
299294
container.setBeanName("integrationTestContainer");
295+
container.setReceiveTimeout(50);
300296
// requires RabbitMQ 3.2.x
301297
// container.setConsumerArguments(Collections. <String, Object> singletonMap("x-priority", Integer.valueOf(10)));
302298
if (externalTransaction) {

‎spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/listener/ListenFromAutoDeleteQueueTests-context.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</rabbit:bindings>
3333
</rabbit:direct-exchange>
3434

35-
<rabbit:listener-container auto-startup="false">
35+
<rabbit:listener-container auto-startup="false" receive-timeout="50">
3636
<rabbit:listener id="container2" ref="foo" queues="otherAnon" admin="containerAdmin" />
3737
</rabbit:listener-container>
3838

@@ -42,11 +42,11 @@
4242
</rabbit:queue-arguments>
4343
</rabbit:queue>
4444

45-
<rabbit:listener-container concurrency="2">
45+
<rabbit:listener-container concurrency="2" receive-timeout="50">
4646
<rabbit:listener id="container3" ref="foo" queues="xExpires" admin="containerAdmin" />
4747
</rabbit:listener-container>
4848

49-
<rabbit:listener-container auto-declare="false">
49+
<rabbit:listener-container auto-declare="false" receive-timeout="50">
5050
<rabbit:listener id="container4" ref="foo" queues="anon2" admin="containerAdmin" />
5151
</rabbit:listener-container>
5252

0 commit comments

Comments
 (0)
Please sign in to comment.