Skip to content

Commit

Permalink
Improve Test Run Times
Browse files Browse the repository at this point in the history
- Reduce container `receiveTimeout` so containers stop faster
- Remove unnecessary `Thread.sleep()`s

Reduced runtime from 5'26'' to 3'11'' on my machine.
  • Loading branch information
garyrussell authored and artembilan committed Jan 16, 2018
1 parent b0cbc35 commit 71af6b1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -127,6 +127,7 @@ public void testErrorHandlerThrowsARADRE() throws Exception {
}
throw new RuntimeException("bar");
});
container.setReceiveTimeout(50);
container.start();
Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class));
doReturn(true).when(logger).isWarnEnabled();
Expand Down Expand Up @@ -217,6 +218,7 @@ public void testRejectingErrorHandler() throws Exception {
admin.declareBinding(BindingBuilder.bind(dlq).to(dle).with(testQueueName));

container.setQueueNames(testQueueName);
container.setReceiveTimeout(50);
container.afterPropertiesSet();
container.start();

Expand Down Expand Up @@ -297,6 +299,7 @@ public void doTest(int messageCount, ErrorHandler errorHandler, CountDownLatch l
container.setTxSize(messageCount);
container.setQueueNames(queue.getName());
container.setErrorHandler(errorHandler);
container.setReceiveTimeout(50);
container.afterPropertiesSet();
container.start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -239,6 +239,7 @@ private void doTest(MessageCount level, Concurrency concurrency, TransactionMode
}
container.setQueueNames(queue.getName());
container.setShutdownTimeout(30000);
container.setReceiveTimeout(50);
container.afterPropertiesSet();
container.start();

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

container.setQueueNames(queue.getName());
container.setReceiveTimeout(50);
container.afterPropertiesSet();
container.start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -467,6 +467,7 @@ protected SimpleMessageListenerContainer doCreateContainer(String queueName, Obj
container.setAcknowledgeMode(acknowledgeMode);
container.setRecoveryInterval(100);
container.setFailedDeclarationRetryInterval(100);
container.setReceiveTimeout(50);
container.afterPropertiesSet();
return container;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,8 +130,6 @@ public void declareQueues() {

@After
public void clear() throws Exception {
// Wait for broker communication to finish before trying to stop container
Thread.sleep(300L);
logger.debug("Shutting down at end of test");
if (container != null) {
container.shutdown();
Expand All @@ -144,12 +142,18 @@ public void clear() throws Exception {
public void testChangeQueues() throws Exception {
CountDownLatch latch = new CountDownLatch(30);
container = createContainer(new MessageListenerAdapter(new PojoListener(latch)), queue.getName(), queue1.getName());
final CountDownLatch consumerLatch = new CountDownLatch(1);
this.container.setApplicationEventPublisher(e -> {
if (e instanceof AsyncConsumerStoppedEvent) {
consumerLatch.countDown();
}
});
for (int i = 0; i < 10; i++) {
template.convertAndSend(queue.getName(), i + "foo");
template.convertAndSend(queue1.getName(), i + "foo");
}
container.addQueueNames(queue1.getName());
Thread.sleep(1100); // allow current consumer to time out and terminate
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS));
for (int i = 0; i < 10; i++) {
template.convertAndSend(queue.getName(), i + "foo");
}
Expand Down Expand Up @@ -665,6 +669,7 @@ private SimpleMessageListenerContainer createContainer(Object listener, boolean
if (queueNames != null) {
container.setQueueNames(queueNames);
}
container.setReceiveTimeout(50);
container.afterPropertiesSet();
if (start) {
container.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -170,7 +170,6 @@ public void declareQueue() {
@After
public void clear() throws Exception {
// Wait for broker communication to finish before trying to stop container
Thread.sleep(300L);
logger.debug("Shutting down at end of test");
if (container != null) {
container.shutdown();
Expand Down Expand Up @@ -255,11 +254,7 @@ private void doListenerWithExceptionTest(CountDownLatch latch, Object listener)
assertTrue("Timed out waiting for message", waited);
}
finally {
// Wait for broker communication to finish before trying to stop
// container
Thread.sleep(300L);
container.shutdown();
Thread.sleep(300L);
}
if (acknowledgeMode.isTransactionAllowed()) {
assertNotNull(template.receiveAndConvert(queue.getName()));
Expand Down Expand Up @@ -297,6 +292,7 @@ private SimpleMessageListenerContainer doCreateContainer(Object listener) {
container.setChannelTransacted(transactional);
container.setAcknowledgeMode(acknowledgeMode);
container.setBeanName("integrationTestContainer");
container.setReceiveTimeout(50);
// requires RabbitMQ 3.2.x
// container.setConsumerArguments(Collections. <String, Object> singletonMap("x-priority", Integer.valueOf(10)));
if (externalTransaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</rabbit:bindings>
</rabbit:direct-exchange>

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

Expand All @@ -42,11 +42,11 @@
</rabbit:queue-arguments>
</rabbit:queue>

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

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

Expand Down

0 comments on commit 71af6b1

Please sign in to comment.