15
15
*/
16
16
package org .springframework .data .redis .listener ;
17
17
18
- import static org .assertj . core . api . Assertions .*;
18
+ import static org .awaitility . Awaitility .*;
19
19
import static org .junit .Assume .*;
20
20
21
+ import java .time .Duration ;
21
22
import java .util .ArrayList ;
22
23
import java .util .Arrays ;
23
24
import java .util .Collection ;
24
- import java .util .HashSet ;
25
- import java .util .LinkedHashSet ;
26
25
import java .util .List ;
27
- import java .util .Set ;
28
26
import java .util .UUID ;
29
27
import java .util .concurrent .BlockingDeque ;
30
28
import java .util .concurrent .LinkedBlockingDeque ;
31
- import java .util .concurrent .TimeUnit ;
32
29
import java .util .stream .Collectors ;
33
30
34
31
import org .junit .jupiter .api .AfterEach ;
@@ -156,18 +153,7 @@ void testContainerPatternResubscribe() throws Exception {
156
153
template .convertAndSend (CHANNEL , payload1 );
157
154
template .convertAndSend (ANOTHER_CHANNEL , payload2 );
158
155
159
- // anotherListener receives both messages
160
- List <String > msgs = new ArrayList <>();
161
- msgs .add (bag2 .poll (500 , TimeUnit .MILLISECONDS ));
162
- msgs .add (bag2 .poll (500 , TimeUnit .MILLISECONDS ));
163
-
164
- assertThat (msgs .size ()).isEqualTo (2 );
165
- assertThat (msgs ).contains (payload1 );
166
- assertThat (msgs ).contains (payload2 );
167
- msgs .clear ();
168
-
169
- // unsubscribed adapter did not receive message
170
- assertThat (bag .poll (500 , TimeUnit .MILLISECONDS )).isNull ();
156
+ await ().atMost (Duration .ofSeconds (2 )).until (() -> bag2 .contains (payload1 ) && bag2 .contains (payload2 ));
171
157
172
158
// bind original listener on another channel
173
159
container .addMessageListener (adapter , new ChannelTopic (ANOTHER_CHANNEL ));
@@ -178,21 +164,10 @@ void testContainerPatternResubscribe() throws Exception {
178
164
template .convertAndSend (CHANNEL , payload1 );
179
165
template .convertAndSend (ANOTHER_CHANNEL , payload2 );
180
166
181
- // original listener received only one message on another channel
182
- msgs .clear ();
183
- msgs .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
184
- msgs .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
185
-
186
- assertThat (msgs ).contains (payload2 );
187
- assertThat (msgs .contains (null )).isTrue ();
167
+ await ().atMost (Duration .ofSeconds (2 )).until (() -> bag .contains (payload2 ));
188
168
189
169
// another listener receives messages on both channels
190
- msgs .clear ();
191
- msgs .add (bag2 .poll (500 , TimeUnit .MILLISECONDS ));
192
- msgs .add (bag2 .poll (500 , TimeUnit .MILLISECONDS ));
193
- assertThat (msgs .size ()).isEqualTo (2 );
194
- assertThat (msgs ).contains (payload1 );
195
- assertThat (msgs ).contains (payload2 );
170
+ await ().atMost (Duration .ofSeconds (2 )).until (() -> bag2 .contains (payload1 ) && bag2 .contains (payload2 ));
196
171
}
197
172
198
173
@ ParameterizedRedisTest
@@ -222,15 +197,7 @@ void testContainerChannelResubscribe() throws Exception {
222
197
template .convertAndSend (ANOTHER_CHANNEL , anotherPayload1 );
223
198
template .convertAndSend (ANOTHER_CHANNEL , anotherPayload2 );
224
199
225
- Set <String > set = new LinkedHashSet <>();
226
- set .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
227
- set .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
228
-
229
- assertThat (set .contains (payload1 )).isFalse ();
230
- assertThat (set .contains (payload2 )).isFalse ();
231
-
232
- assertThat (set .contains (anotherPayload1 )).isTrue ();
233
- assertThat (set .contains (anotherPayload2 )).isTrue ();
200
+ await ().atMost (Duration .ofSeconds (2 )).until (() -> bag .contains (anotherPayload1 ) && bag .contains (anotherPayload2 ));
234
201
}
235
202
236
203
/**
@@ -246,8 +213,8 @@ void testInitializeContainerWithMultipleTopicsIncludingPattern() throws Exceptio
246
213
247
214
container .stop ();
248
215
249
- String uniqueChannel = "random-" + UUID .randomUUID (). toString () ;
250
- PubSubAwaitUtil .runAndAwaitChannelSubscription (template .getConnectionFactory (), uniqueChannel , () -> {
216
+ String uniqueChannel = "random-" + UUID .randomUUID ();
217
+ PubSubAwaitUtil .runAndAwaitPatternSubscription (template .getConnectionFactory (), () -> {
251
218
252
219
container .addMessageListener (adapter ,
253
220
Arrays .asList (new Topic [] { new ChannelTopic (uniqueChannel ), new PatternTopic ("s*" ) }));
@@ -256,16 +223,12 @@ void testInitializeContainerWithMultipleTopicsIncludingPattern() throws Exceptio
256
223
257
224
// timing: There's currently no other way to synchronize
258
225
// than to hope the subscribe/unsubscribe are executed within the time.
259
- Thread .sleep (50 );
226
+ Thread .sleep (250 );
260
227
261
228
template .convertAndSend ("somechannel" , "HELLO" );
262
229
template .convertAndSend (uniqueChannel , "WORLD" );
263
230
264
- Set <String > set = new LinkedHashSet <>();
265
- set .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
266
- set .add (bag .poll (500 , TimeUnit .MILLISECONDS ));
267
-
268
- assertThat (set ).isEqualTo (new HashSet <>(Arrays .asList (new String [] { "HELLO" , "WORLD" })));
231
+ await ().atMost (Duration .ofSeconds (2 )).until (() -> bag .contains ("HELLO" ) && bag .contains ("WORLD" ));
269
232
}
270
233
271
234
private class MessageHandler {
0 commit comments