20
20
import static com .rabbitmq .client .amqp .Publisher .Status .ACCEPTED ;
21
21
import static com .rabbitmq .client .amqp .Resource .State .OPEN ;
22
22
import static com .rabbitmq .client .amqp .Resource .State .RECOVERING ;
23
- import static com .rabbitmq .client .amqp .impl .TestUtils .*;
24
- import static com .rabbitmq .client .amqp .impl .TestUtils .CountDownLatchConditions .completed ;
23
+ import static com .rabbitmq .client .amqp .impl .Assertions .assertThat ;
25
24
import static com .rabbitmq .client .amqp .impl .TestUtils .name ;
26
25
import static com .rabbitmq .client .amqp .impl .TestUtils .waitAtMost ;
27
26
import static java .time .Duration .ofMillis ;
34
33
import com .rabbitmq .client .amqp .metrics .NoOpMetricsCollector ;
35
34
import java .util .*;
36
35
import java .util .concurrent .ConcurrentHashMap ;
37
- import java .util .concurrent .CountDownLatch ;
38
36
import java .util .concurrent .atomic .AtomicInteger ;
39
- import java .util .concurrent .atomic .AtomicReference ;
40
37
import org .assertj .core .api .ThrowableAssert .ThrowingCallable ;
41
38
import org .junit .jupiter .api .*;
42
39
import org .junit .jupiter .params .ParameterizedTest ;
@@ -80,18 +77,18 @@ static void afterAll() {
80
77
void connectionShouldRecoverAfterClosingIt (boolean isolateResources , TestInfo info ) {
81
78
String q = name (info );
82
79
String connectionName = UUID .randomUUID ().toString ();
83
- Map <Resource .State , CountDownLatch > stateLatches = new ConcurrentHashMap <>();
84
- stateLatches .put (RECOVERING , new CountDownLatch (1 ));
85
- stateLatches .put (OPEN , new CountDownLatch (2 ));
80
+ Map <Resource .State , TestUtils . Sync > stateSync = new ConcurrentHashMap <>();
81
+ stateSync .put (RECOVERING , TestUtils . sync (1 ));
82
+ stateSync .put (OPEN , TestUtils . sync (2 ));
86
83
AmqpConnectionBuilder builder =
87
84
(AmqpConnectionBuilder )
88
85
new AmqpConnectionBuilder (environment )
89
86
.name (connectionName )
90
87
.isolateResources (isolateResources )
91
88
.listeners (
92
89
context -> {
93
- if (stateLatches .containsKey (context .currentState ())) {
94
- stateLatches .get (context .currentState ()).countDown ();
90
+ if (stateSync .containsKey (context .currentState ())) {
91
+ stateSync .get (context .currentState ()).down ();
95
92
}
96
93
})
97
94
.recovery ()
@@ -103,14 +100,14 @@ void connectionShouldRecoverAfterClosingIt(boolean isolateResources, TestInfo in
103
100
c .management ().queue ().name (q ).declare ();
104
101
AtomicInteger consumerOpenCount = new AtomicInteger (0 );
105
102
Collection <UUID > receivedMessageIds = Collections .synchronizedList (new ArrayList <>());
106
- AtomicReference < CountDownLatch > consumeLatch = new AtomicReference <>( new CountDownLatch ( 1 ) );
103
+ TestUtils . Sync consumeSync = TestUtils . sync ( );
107
104
c .consumerBuilder ()
108
105
.queue (q )
109
106
.messageHandler (
110
107
(context , message ) -> {
111
108
context .accept ();
112
109
receivedMessageIds .add (message .messageIdAsUuid ());
113
- consumeLatch . get (). countDown ();
110
+ consumeSync . down ();
114
111
})
115
112
.listeners (
116
113
context -> {
@@ -119,7 +116,7 @@ void connectionShouldRecoverAfterClosingIt(boolean isolateResources, TestInfo in
119
116
}
120
117
})
121
118
.build ();
122
- AtomicReference < CountDownLatch > publishLatch = new AtomicReference <>( new CountDownLatch ( 1 ) );
119
+ TestUtils . Sync publishSync = TestUtils . sync ( );
123
120
AtomicInteger publisherOpenCount = new AtomicInteger (0 );
124
121
Publisher p =
125
122
c .publisherBuilder ()
@@ -136,34 +133,34 @@ void connectionShouldRecoverAfterClosingIt(boolean isolateResources, TestInfo in
136
133
context -> {
137
134
if (context .status () == ACCEPTED ) {
138
135
publishedMessageIds .add (context .message ().messageIdAsUuid ());
139
- publishLatch . get (). countDown ();
136
+ publishSync . down ();
140
137
}
141
138
};
142
139
p .publish (p .message ().messageId (UUID .randomUUID ()), outboundMessageCallback );
143
140
assertThat (publisherOpenCount ).hasValue (1 );
144
- assertThat (publishLatch ). is ( CountDownLatchReferenceConditions . completed () );
141
+ assertThat (publishSync ). completes ( );
145
142
146
143
assertThat (consumerOpenCount ).hasValue (1 );
147
- assertThat (consumeLatch ). is ( CountDownLatchReferenceConditions . completed () );
144
+ assertThat (consumeSync ). completes ( );
148
145
assertThat (receivedMessageIds )
149
146
.hasSameSizeAs (publishedMessageIds )
150
147
.containsAll (publishedMessageIds );
151
148
152
- consumeLatch . set ( new CountDownLatch ( 1 ) );
149
+ consumeSync . reset ( );
153
150
154
151
Cli .closeConnection (connectionName );
155
- assertThat (stateLatches .get (RECOVERING )).is ( completed () );
156
- assertThat (stateLatches .get (OPEN )).is ( completed () );
152
+ assertThat (stateSync .get (RECOVERING )).completes ( );
153
+ assertThat (stateSync .get (OPEN )).completes ( );
157
154
assertThat (connectionAttemptCount ).hasValue (2 );
158
155
waitAtMost (() -> consumerOpenCount .get () == 2 );
159
156
waitAtMost (() -> publisherOpenCount .get () == 2 );
160
157
161
- publishLatch . set ( new CountDownLatch ( 1 ) );
158
+ publishSync . reset ( );
162
159
p .publish (p .message ().messageId (UUID .randomUUID ()), outboundMessageCallback );
163
- assertThat (publishLatch ). is ( CountDownLatchReferenceConditions . completed () );
160
+ assertThat (publishSync ). completes ( );
164
161
assertThat (publishedMessageIds ).hasSize (2 );
165
162
166
- assertThat (consumeLatch ). is ( CountDownLatchReferenceConditions . completed () );
163
+ assertThat (consumeSync ). completes ( );
167
164
assertThat (receivedMessageIds )
168
165
.hasSameSizeAs (publishedMessageIds )
169
166
.containsAll (publishedMessageIds );
@@ -179,18 +176,18 @@ void connectionShouldRecoverAfterClosingIt(boolean isolateResources, TestInfo in
179
176
void connectionShouldRecoverAfterBrokerStopStart (boolean isolateResources , TestInfo info ) {
180
177
String q = name (info );
181
178
String connectionName = UUID .randomUUID ().toString ();
182
- Map <Resource .State , CountDownLatch > stateLatches = new ConcurrentHashMap <>();
183
- stateLatches .put (RECOVERING , new CountDownLatch (1 ));
184
- stateLatches .put (OPEN , new CountDownLatch (2 ));
179
+ Map <Resource .State , TestUtils . Sync > stateSync = new ConcurrentHashMap <>();
180
+ stateSync .put (RECOVERING , TestUtils . sync (1 ));
181
+ stateSync .put (OPEN , TestUtils . sync (2 ));
185
182
AmqpConnectionBuilder builder =
186
183
(AmqpConnectionBuilder )
187
184
new AmqpConnectionBuilder (environment )
188
185
.name (connectionName )
189
186
.isolateResources (isolateResources )
190
187
.listeners (
191
188
context -> {
192
- if (stateLatches .containsKey (context .currentState ())) {
193
- stateLatches .get (context .currentState ()).countDown ();
189
+ if (stateSync .containsKey (context .currentState ())) {
190
+ stateSync .get (context .currentState ()).down ();
194
191
}
195
192
})
196
193
.recovery ()
@@ -201,7 +198,7 @@ void connectionShouldRecoverAfterBrokerStopStart(boolean isolateResources, TestI
201
198
c .management ().queue ().name (q ).autoDelete (true ).exclusive (true ).declare ();
202
199
try {
203
200
Cli .stopBroker ();
204
- assertThat (stateLatches .get (RECOVERING )).is ( completed () );
201
+ assertThat (stateSync .get (RECOVERING )).completes ( );
205
202
stream (
206
203
new ThrowingCallable [] {
207
204
() -> c .management ().queue ().name (q ).exclusive (true ).declare (),
@@ -216,7 +213,7 @@ void connectionShouldRecoverAfterBrokerStopStart(boolean isolateResources, TestI
216
213
} finally {
217
214
Cli .startBroker ();
218
215
}
219
- assertThat (stateLatches .get (OPEN )).is ( completed () );
216
+ assertThat (stateSync .get (OPEN )).completes ( );
220
217
assertThat (connectionAttemptCount ).hasValueGreaterThan (1 );
221
218
c .management ().queue ().name (q ).autoDelete (true ).exclusive (true ).declare ();
222
219
}
0 commit comments