Skip to content

Commit 3b146fe

Browse files
committed
Fix race condition around Redis key
https://build.spring.io/browse/INT-FATS5IC-922/ https://build.spring.io/browse/INT-MJATS41-1764/ The `RedisQueueMessageDrivenEndpointTests` may be called from different CI plans against the same Redis instance. So, clean up keys before and after every unit test **Cherry-pick until 4.3.x** # Conflicts: # spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests.java # Conflicts: # spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests.java # Conflicts: # spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests-context.xml # spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests.java
1 parent 3465110 commit 3b146fe

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests-context.xml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
<int:queue/>
1919
</int:channel>
2020

21-
<int-redis:queue-inbound-channel-adapter queue="si.test.Int3017IntegrationInbound"
22-
channel="fromChannel"
23-
expect-message="true"
24-
serializer="testSerializer"/>
21+
<int-redis:queue-inbound-channel-adapter id="fromChannelEndpoint" queue="si.test.Int3017IntegrationInbound"
22+
channel="fromChannel"
23+
expect-message="true"
24+
auto-startup="false"
25+
serializer="testSerializer"/>
2526

2627
<bean id="testSerializer" class="org.springframework.integration.redis.util.CustomJsonSerializer"/>
2728

@@ -30,9 +31,11 @@
3031
<int-redis:queue-outbound-channel-adapter queue-expression="headers.redis_queue"/>
3132
</int:chain>
3233

33-
<int-redis:queue-inbound-channel-adapter queue="si.test.Int3017IntegrationSymmetrical"
34-
channel="symmetricalRedisChannel"
35-
serializer=""/>
34+
<int-redis:queue-inbound-channel-adapter id="symmetricalRedisChannelEndpoint"
35+
queue="si.test.Int3017IntegrationSymmetrical"
36+
channel="symmetricalRedisChannel"
37+
auto-startup="false"
38+
serializer=""/>
3639

3740

3841
<int:payload-deserializing-transformer input-channel="symmetricalRedisChannel" output-channel="symmetricalOutputChannel"/>

spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisQueueMessageDrivenEndpointTests.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2019 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.
@@ -46,6 +46,7 @@
4646
import org.springframework.beans.factory.annotation.Autowired;
4747
import org.springframework.context.ApplicationEvent;
4848
import org.springframework.context.ApplicationEventPublisher;
49+
import org.springframework.context.Lifecycle;
4950
import org.springframework.data.redis.RedisConnectionFailureException;
5051
import org.springframework.data.redis.RedisSystemException;
5152
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -88,9 +89,15 @@ public class RedisQueueMessageDrivenEndpointTests extends RedisAvailableTests {
8889
@Autowired
8990
private PollableChannel fromChannel;
9091

92+
@Autowired
93+
private Lifecycle fromChannelEndpoint;
94+
9195
@Autowired
9296
private MessageChannel symmetricalInputChannel;
9397

98+
@Autowired
99+
private Lifecycle symmetricalRedisChannelEndpoint;
100+
94101
@Autowired
95102
private PollableChannel symmetricalOutputChannel;
96103

@@ -106,6 +113,7 @@ public void testInt3014Default() throws Exception {
106113
redisTemplate.setKeySerializer(new StringRedisSerializer());
107114
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
108115
redisTemplate.afterPropertiesSet();
116+
redisTemplate.delete(queueName);
109117

110118
String payload = "testing";
111119

@@ -135,6 +143,7 @@ public void testInt3014Default() throws Exception {
135143
assertEquals(payload2, receive.getPayload());
136144

137145
endpoint.stop();
146+
redisTemplate.delete(queueName);
138147
}
139148

140149
@Test
@@ -149,6 +158,7 @@ public void testInt3014ExpectMessageTrue() throws Exception {
149158
redisTemplate.setKeySerializer(new StringRedisSerializer());
150159
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
151160
redisTemplate.afterPropertiesSet();
161+
redisTemplate.delete(queueName);
152162

153163
Message<?> message = MessageBuilder.withPayload("testing").build();
154164

@@ -187,38 +197,53 @@ public void testInt3014ExpectMessageTrue() throws Exception {
187197
Matchers.containsString("java.lang.String cannot be cast to org.springframework.messaging.Message"));
188198

189199
endpoint.stop();
200+
redisTemplate.delete(queueName);
190201
}
191202

192203
@Test
193204
@RedisAvailable
194205
public void testInt3017IntegrationInbound() throws Exception {
195-
String payload = new Date().toString();
196-
206+
String queueName = "si.test.redisQueueInboundChannelAdapterTests2";
197207
RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
198208
redisTemplate.setConnectionFactory(this.connectionFactory);
199209
redisTemplate.afterPropertiesSet();
210+
redisTemplate.delete(queueName);
211+
212+
this.fromChannelEndpoint.start();
213+
String payload = new Date().toString();
214+
200215

201216
redisTemplate.boundListOps("si.test.Int3017IntegrationInbound")
202217
.leftPush("{\"payload\":\"" + payload + "\",\"headers\":{}}");
203218

204219
Message<?> receive = this.fromChannel.receive(10000);
205220
assertNotNull(receive);
206221
assertEquals(payload, receive.getPayload());
222+
this.fromChannelEndpoint.stop();
223+
redisTemplate.delete(queueName);
207224
}
208225

209226
@Test
210227
@RedisAvailable
211228
public void testInt3017IntegrationSymmetrical() throws Exception {
229+
String queueName = "si.test.Int3017IntegrationSymmetrical";
230+
RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
231+
redisTemplate.setConnectionFactory(this.connectionFactory);
232+
redisTemplate.afterPropertiesSet();
233+
redisTemplate.delete(queueName);
234+
this.symmetricalRedisChannelEndpoint.start();
212235
UUID payload = UUID.randomUUID();
213236
Message<UUID> message = MessageBuilder.withPayload(payload)
214-
.setHeader("redis_queue", "si.test.Int3017IntegrationSymmetrical")
237+
.setHeader("redis_queue", queueName)
215238
.build();
216239

217240
this.symmetricalInputChannel.send(message);
218241

219242
Message<?> receive = this.symmetricalOutputChannel.receive(10000);
220243
assertNotNull(receive);
221244
assertEquals(payload, receive.getPayload());
245+
this.symmetricalRedisChannelEndpoint.stop();
246+
redisTemplate.delete(queueName);
222247
}
223248

224249
@Test
@@ -233,6 +258,7 @@ public void testInt3442ProperlyStop() throws Exception {
233258
redisTemplate.setKeySerializer(new StringRedisSerializer());
234259
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
235260
redisTemplate.afterPropertiesSet();
261+
redisTemplate.delete(queueName);
236262

237263
while (redisTemplate.boundListOps(queueName).rightPop() != null) {
238264
// drain
@@ -277,6 +303,7 @@ public void run() {
277303
assertTrue(stopLatch.await(21, TimeUnit.SECONDS));
278304

279305
Mockito.verify(boundListOperations, atLeastOnce()).rightPush(Mockito.any(byte[].class));
306+
redisTemplate.delete(queueName);
280307
}
281308

282309

@@ -292,7 +319,8 @@ public void testInt3196Recovery() throws Exception {
292319

293320
final CountDownLatch exceptionsLatch = new CountDownLatch(2);
294321

295-
RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
322+
RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName,
323+
this.connectionFactory);
296324
endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
297325
endpoint.setApplicationEventPublisher(new ApplicationEventPublisher() {
298326

@@ -345,6 +373,7 @@ public void publishEvent(Object event) {
345373
assertEquals(payload, receive.getPayload());
346374

347375
endpoint.stop();
376+
redisTemplate.delete(queueName);
348377
}
349378

350379
@Test
@@ -359,6 +388,7 @@ public void testInt3932ReadFromLeft() throws Exception {
359388
redisTemplate.setKeySerializer(new StringRedisSerializer());
360389
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
361390
redisTemplate.afterPropertiesSet();
391+
redisTemplate.delete(queueName);
362392

363393
String payload = "testing";
364394

@@ -389,6 +419,7 @@ public void testInt3932ReadFromLeft() throws Exception {
389419
assertEquals(payload2, receive.getPayload());
390420

391421
endpoint.stop();
422+
redisTemplate.delete(queueName);
392423
}
393424

394425
private void waitListening(RedisQueueMessageDrivenEndpoint endpoint) throws InterruptedException {

0 commit comments

Comments
 (0)