Skip to content

Commit 7cc3f0a

Browse files
garyrussellartembilan
authored andcommitted
Use Awaitility in tests
1 parent 095b2df commit 7cc3f0a

28 files changed

+183
-377
lines changed

build.gradle

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ext {
4040

4141
assertjVersion = '3.15.0'
4242
assertkVersion = '0.20'
43+
awaitilityVersion = '4.0.3'
4344
commonsHttpClientVersion = '4.5.10'
4445
commonsPoolVersion = '2.8.0'
4546
googleJsr305Version = '3.0.2'
@@ -55,9 +56,9 @@ ext {
5556
rabbitmqVersion = project.hasProperty('rabbitmqVersion') ? project.rabbitmqVersion : '5.9.0'
5657
rabbitmqHttpClientVersion = '3.2.0.RELEASE'
5758
reactorVersion = 'Dysprosium-SR7'
58-
springDataCommonsVersion = '2.3.0.RC1'
59-
springVersion = project.hasProperty('springVersion') ? project.springVersion : '5.2.5.RELEASE'
60-
springRetryVersion = '1.2.5.RELEASE'
59+
springDataCommonsVersion = '2.3.0.RELEASE'
60+
springVersion = project.hasProperty('springVersion') ? project.springVersion : '5.2.6.RELEASE'
61+
springRetryVersion = '1.3.0'
6162
}
6263

6364
nohttp {
@@ -161,10 +162,14 @@ subprojects { subproject ->
161162
testImplementation 'org.junit.jupiter:junit-jupiter-params'
162163
testImplementation 'org.junit.jupiter:junit-jupiter-engine'
163164
testImplementation 'org.junit.platform:junit-platform-launcher'
165+
testImplementation("org.awaitility:awaitility:$awaitilityVersion") {
166+
exclude group: 'org.hamcrest'
167+
}
164168

165169
// To avoid compiler warnings about @API annotations in JUnit code
166170
testCompileOnly 'org.apiguardian:apiguardian-api:1.0.0'
167-
171+
172+
testCompileOnly "com.google.code.findbugs:jsr305:$googleJsr305Version"
168173
testImplementation 'org.jetbrains.kotlin:kotlin-reflect'
169174
testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
170175

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/AsyncRabbitTemplateTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.fail;
21+
import static org.awaitility.Awaitility.await;
2122

2223
import java.util.Map;
2324
import java.util.UUID;
@@ -175,13 +176,9 @@ public void testMessage1ArgDirect() throws Exception {
175176
}
176177

177178
private void waitForZeroInUseConsumers() throws InterruptedException {
178-
int n = 0;
179179
Map<?, ?> inUseConsumers = TestUtils
180180
.getPropertyValue(this.asyncDirectTemplate, "directReplyToContainer.inUseConsumerChannels", Map.class);
181-
while (n++ < 100 && inUseConsumers.size() > 0) {
182-
Thread.sleep(100);
183-
}
184-
assertThat(inUseConsumers).hasSize(0);
181+
await().until(() -> inUseConsumers.size() == 0);
185182
}
186183

187184
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/ComplexTypeJsonIntegrationTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.annotation;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021

2122
import java.util.concurrent.TimeUnit;
2223

@@ -111,13 +112,9 @@ public void testReceiveNoWait() throws Exception {
111112
m.getMessageProperties().getHeaders().remove("__TypeId__");
112113
return m;
113114
});
114-
Foo<Bar<Baz, Qux>> foo =
115-
this.rabbitTemplate.receiveAndConvert(new ParameterizedTypeReference<Foo<Bar<Baz, Qux>>>() { });
116-
int n = 0;
117-
while (n++ < 100 && foo == null) {
118-
Thread.sleep(100);
119-
foo = this.rabbitTemplate.receiveAndConvert(new ParameterizedTypeReference<Foo<Bar<Baz, Qux>>>() { });
120-
}
115+
Foo<Bar<Baz, Qux>> foo = await().until(
116+
() -> this.rabbitTemplate.receiveAndConvert(new ParameterizedTypeReference<Foo<Bar<Baz, Qux>>>() { }),
117+
msg -> msg != null);
121118
verifyFooBarBazQux(foo);
122119
}
123120

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/ConsumerBatchingTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2019-2020 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.annotation;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021

2122
import java.io.IOException;
2223
import java.io.Serializable;
@@ -82,11 +83,9 @@ public void replayWholeBatch() throws InterruptedException {
8283
assertThat(this.listener.foos)
8384
.extracting(foo -> foo.getBar())
8485
.contains("foo", "bar", "baz", "qux", "foo", "bar", "baz", "qux");
85-
Timer timer = null;
86-
int n = 0;
87-
while (timer == null && n++ < 100) {
86+
Timer timer = await().until(() -> {
8887
try {
89-
timer = this.meterRegistry.get("spring.rabbitmq.listener")
88+
return this.meterRegistry.get("spring.rabbitmq.listener")
9089
.tag("listener.id", "batch.1")
9190
.tag("queue", "[c.batch.1]")
9291
.tag("result", "success")
@@ -95,9 +94,9 @@ public void replayWholeBatch() throws InterruptedException {
9594
.timer();
9695
}
9796
catch (@SuppressWarnings("unused") Exception e) {
98-
Thread.sleep(100);
97+
return null;
9998
}
100-
}
99+
}, tim -> tim != null);
101100
assertThat(timer).isNotNull();
102101
assertThat(timer.count()).isEqualTo(1L);
103102
timer = this.meterRegistry.get("spring.rabbitmq.listener")

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.fail;
21+
import static org.awaitility.Awaitility.await;
2122
import static org.mockito.Mockito.doAnswer;
2223
import static org.mockito.Mockito.mock;
2324

@@ -872,11 +873,9 @@ public void messagingMessageReturned() throws InterruptedException {
872873
assertThat(message).isNotNull();
873874
assertThat(new String(message.getBody())).isEqualTo("{\"field\":\"MESSAGING\"}");
874875
assertThat(message.getMessageProperties().getHeaders().get("foo")).isEqualTo("bar");
875-
Timer timer = null;
876-
int n = 0;
877-
while (timer == null && n++ < 100) {
876+
Timer timer = await().until(() -> {
878877
try {
879-
timer = this.meterRegistry.get("spring.rabbitmq.listener")
878+
return this.meterRegistry.get("spring.rabbitmq.listener")
880879
.tag("listener.id", "list.of.messages")
881880
.tag("queue", "test.messaging.message")
882881
.tag("result", "success")
@@ -885,10 +884,9 @@ public void messagingMessageReturned() throws InterruptedException {
885884
.timer();
886885
}
887886
catch (@SuppressWarnings("unused") Exception e) {
888-
Thread.sleep(100);
887+
return null;
889888
}
890-
}
891-
assertThat(timer).isNotNull();
889+
}, tim -> tim != null);
892890
assertThat(timer.count()).isEqualTo(1L);
893891
}
894892

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -17,8 +17,9 @@
1717
package org.springframework.amqp.rabbit.config;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021

21-
import java.util.Properties;
22+
import java.time.Duration;
2223

2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
@@ -66,16 +67,9 @@ public void testArgumentsQueue() throws Exception {
6667

6768
assertThat(queue.getArguments().get("x-message-ttl")).isEqualTo(100L);
6869
template.convertAndSend(queue.getName(), "message");
69-
Properties props = rabbitAdmin.getQueueProperties("arguments");
70-
if (props != null) {
71-
int n = 0;
72-
while (n++ < 200 && (Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT) > 0) {
73-
Thread.sleep(50);
74-
props = rabbitAdmin.getQueueProperties("arguments");
75-
}
76-
assertThat((Integer) props.get(RabbitAdmin.QUEUE_MESSAGE_COUNT)).isEqualTo(0);
77-
}
78-
70+
await().with().pollInterval(Duration.ofMillis(50))
71+
.until(() -> rabbitAdmin.getQueueProperties("arguments")
72+
.get(RabbitAdmin.QUEUE_MESSAGE_COUNT).equals(0));
7973
connectionFactory.destroy();
8074
RabbitAvailableCondition.getBrokerRunning().deleteQueues("arguments");
8175
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/PublisherCallbackChannelTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.connection;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021
import static org.mockito.ArgumentMatchers.any;
2122
import static org.mockito.BDDMockito.willAnswer;
2223
import static org.mockito.Mockito.mock;
@@ -130,12 +131,10 @@ void testNotCached() throws Exception {
130131
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
131132
assertThat(closedLatch.await(10, TimeUnit.SECONDS)).isTrue();
132133
cacheProperties = cf.getCacheProperties();
133-
int n = 0;
134-
while (n++ < 100 && Integer.parseInt(cacheProperties.getProperty("idleChannelsNotTx")) < 2) {
135-
Thread.sleep(100);
136-
cacheProperties = cf.getCacheProperties();
137-
}
138-
assertThat(cacheProperties.getProperty("idleChannelsNotTx")).isEqualTo("2");
134+
await().until(() -> {
135+
Properties props = cf.getCacheProperties();
136+
return Integer.parseInt(props.getProperty("idleChannelsNotTx")) == 2;
137+
});
139138
}
140139

141140
}

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/BatchingRabbitTemplateTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021
import static org.mockito.ArgumentMatchers.any;
2122
import static org.mockito.ArgumentMatchers.anyString;
2223
import static org.mockito.Mockito.doNothing;
@@ -27,6 +28,7 @@
2728

2829
import java.io.OutputStream;
2930
import java.lang.reflect.Method;
31+
import java.time.Duration;
3032
import java.util.ArrayList;
3133
import java.util.List;
3234
import java.util.concurrent.CountDownLatch;
@@ -69,6 +71,7 @@
6971
import org.springframework.amqp.support.postprocessor.ZipPostProcessor;
7072
import org.springframework.amqp.utils.test.TestUtils;
7173
import org.springframework.beans.DirectFieldAccessor;
74+
import org.springframework.lang.Nullable;
7275
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
7376
import org.springframework.util.ReflectionUtils;
7477
import org.springframework.util.StopWatch;
@@ -615,15 +618,10 @@ public void testSimpleBatchDeflaterWithEncoding() throws Exception {
615618
assertThat(new String(message.getBody())).isEqualTo("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar");
616619
}
617620

621+
@Nullable
618622
private Message receive(BatchingRabbitTemplate template) throws InterruptedException {
619-
Message message = template.receive(ROUTE);
620-
int n = 0;
621-
while (n++ < 200 && message == null) {
622-
Thread.sleep(50);
623-
message = template.receive(ROUTE);
624-
}
625-
assertThat(message).isNotNull();
626-
return message;
623+
return await().with().pollInterval(Duration.ofMillis(50))
624+
.until(() -> template.receive(ROUTE), msg -> msg != null);
627625
}
628626

629627
@Test

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/FixedReplyQueueDeadLetterTests.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
2021

2122
import java.net.MalformedURLException;
2223
import java.net.URISyntaxException;
@@ -99,13 +100,7 @@ void test() throws Exception {
99100
void testQueueArgs1() throws MalformedURLException, URISyntaxException, InterruptedException {
100101
Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(),
101102
brokerRunning.getAdminPassword());
102-
QueueInfo queue = client.getQueue("/", "all.args.1");
103-
int n = 0;
104-
while (n++ < 100 && queue == null) {
105-
Thread.sleep(100);
106-
queue = client.getQueue("/", "all.args.1");
107-
}
108-
assertThat(n).isLessThan(100);
103+
QueueInfo queue = await().until(() -> client.getQueue("/", "all.args.1"), que -> que != null);
109104
Map<String, Object> arguments = queue.getArguments();
110105
assertThat(arguments.get("x-message-ttl")).isEqualTo(1000);
111106
assertThat(arguments.get("x-expires")).isEqualTo(200_000);
@@ -124,13 +119,7 @@ void testQueueArgs1() throws MalformedURLException, URISyntaxException, Interrup
124119
void testQueueArgs2() throws MalformedURLException, URISyntaxException, InterruptedException {
125120
Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(),
126121
brokerRunning.getAdminPassword());
127-
QueueInfo queue = client.getQueue("/", "all.args.2");
128-
int n = 0;
129-
while (n++ < 100 && queue == null) {
130-
Thread.sleep(100);
131-
queue = client.getQueue("/", "all.args.1");
132-
}
133-
assertThat(n).isLessThan(100);
122+
QueueInfo queue = await().until(() -> client.getQueue("/", "all.args.2"), que -> que != null);
134123
Map<String, Object> arguments = queue.getArguments();
135124
assertThat(arguments.get("x-message-ttl")).isEqualTo(1000);
136125
assertThat(arguments.get("x-expires")).isEqualTo(200_000);
@@ -148,13 +137,7 @@ void testQueueArgs2() throws MalformedURLException, URISyntaxException, Interrup
148137
void testQueueArgs3() throws MalformedURLException, URISyntaxException, InterruptedException {
149138
Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(),
150139
brokerRunning.getAdminPassword());
151-
QueueInfo queue = client.getQueue("/", "all.args.3");
152-
int n = 0;
153-
while (n++ < 100 && queue == null) {
154-
Thread.sleep(100);
155-
queue = client.getQueue("/", "all.args.1");
156-
}
157-
assertThat(n).isLessThan(100);
140+
QueueInfo queue = await().until(() -> client.getQueue("/", "all.args.3"), que -> que != null);
158141
Map<String, Object> arguments = queue.getArguments();
159142
assertThat(arguments.get("x-message-ttl")).isEqualTo(1000);
160143
assertThat(arguments.get("x-expires")).isEqualTo(200_000);
@@ -178,13 +161,7 @@ void testQueueArgs3() throws MalformedURLException, URISyntaxException, Interrup
178161
void testQuorumArgs() throws MalformedURLException, URISyntaxException, InterruptedException {
179162
Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(),
180163
brokerRunning.getAdminPassword());
181-
QueueInfo queue = client.getQueue("/", "test.quorum");
182-
int n = 0;
183-
while (n++ < 100 && queue == null) {
184-
Thread.sleep(100);
185-
queue = client.getQueue("/", "test.quorum");
186-
}
187-
assertThat(n).isLessThan(100);
164+
QueueInfo queue = await().until(() -> client.getQueue("/", "test.quorum"), que -> que != null);
188165
Map<String, Object> arguments = queue.getArguments();
189166
assertThat(arguments.get("x-queue-type")).isEqualTo("quorum");
190167
assertThat(arguments.get("x-delivery-limit")).isEqualTo(10);

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminIntegrationTests.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -18,8 +18,10 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21+
import static org.awaitility.Awaitility.await;
2122

2223
import java.io.IOException;
24+
import java.time.Duration;
2325
import java.util.UUID;
2426

2527
import org.junit.jupiter.api.AfterEach;
@@ -412,7 +414,6 @@ public void testDeclareDelayedExchange() throws Exception {
412414
assertThat(System.currentTimeMillis() - t1).isGreaterThan(950L);
413415

414416
ExchangeInfo exchange2 = getExchange(exchangeName);
415-
assertThat(exchange2).isNotNull();
416417
assertThat(exchange2.getArguments().get("x-delayed-type")).isEqualTo(ExchangeTypes.DIRECT);
417418
assertThat(exchange2.getType()).isEqualTo("x-delayed-message");
418419

@@ -422,13 +423,8 @@ public void testDeclareDelayedExchange() throws Exception {
422423

423424
private ExchangeInfo getExchange(String exchangeName) throws Exception {
424425
Client rabbitRestClient = new Client("http://localhost:15672/api/", "guest", "guest");
425-
int n = 0;
426-
ExchangeInfo exchange = rabbitRestClient.getExchange("/", exchangeName);
427-
while (n++ < 100 && exchange == null) {
428-
Thread.sleep(100);
429-
exchange = rabbitRestClient.getExchange("/", exchangeName);
430-
}
431-
return exchange;
426+
return await().pollDelay(Duration.ZERO)
427+
.until(() -> rabbitRestClient.getExchange("/", exchangeName), exch -> exch != null);
432428
}
433429

434430
/**

0 commit comments

Comments
 (0)