|
14 | 14 |
|
15 | 15 | package com.rabbitmq.stream.impl;
|
16 | 16 |
|
17 |
| -import static com.rabbitmq.stream.impl.TestUtils.publishAndWaitForConfirms; |
18 |
| -import static com.rabbitmq.stream.impl.TestUtils.waitAtMost; |
| 17 | +import static com.rabbitmq.stream.impl.Assertions.assertThat; |
| 18 | +import static com.rabbitmq.stream.impl.TestUtils.*; |
19 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 20 |
|
21 |
| -import com.rabbitmq.stream.Consumer; |
22 |
| -import com.rabbitmq.stream.Environment; |
23 |
| -import com.rabbitmq.stream.EnvironmentBuilder; |
24 |
| -import com.rabbitmq.stream.OffsetSpecification; |
| 21 | +import com.rabbitmq.stream.*; |
25 | 22 | import com.rabbitmq.stream.impl.TestUtils.BrokerVersionAtLeast311Condition;
|
26 | 23 | import io.netty.channel.EventLoopGroup;
|
27 | 24 | import java.util.Map;
|
28 | 25 | import java.util.concurrent.ConcurrentHashMap;
|
29 | 26 | import java.util.concurrent.atomic.AtomicInteger;
|
30 | 27 | import java.util.concurrent.atomic.AtomicLong;
|
| 28 | +import java.util.stream.Stream; |
31 | 29 | import org.junit.jupiter.api.AfterEach;
|
32 | 30 | import org.junit.jupiter.api.BeforeEach;
|
33 | 31 | import org.junit.jupiter.api.Test;
|
34 | 32 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.MethodSource; |
35 | 35 |
|
36 | 36 | @ExtendWith({
|
37 | 37 | TestUtils.StreamTestInfrastructureExtension.class,
|
@@ -237,4 +237,72 @@ void externalTrackingSecondConsumerShouldTakeOverWhereTheFirstOneLeftOff() throw
|
237 | 237 | // nothing stored on the server side
|
238 | 238 | assertThat(cf.get().queryOffset(consumerName, stream).getOffset()).isZero();
|
239 | 239 | }
|
| 240 | + |
| 241 | + public static Stream<java.util.function.Consumer<Consumer>> |
| 242 | + activeConsumerShouldGetUpdateNotificationAfterDisruption() { |
| 243 | + return Stream.of( |
| 244 | + namedConsumer(consumer -> Host.killConnection(connectionName(consumer)), "kill connection"), |
| 245 | + namedConsumer(consumer -> Host.restartStream(stream(consumer)), "restart stream"), |
| 246 | + namedConsumer(Consumer::close, "close consumer")); |
| 247 | + } |
| 248 | + |
| 249 | + @ParameterizedTest |
| 250 | + @MethodSource |
| 251 | + @TestUtils.DisabledIfRabbitMqCtlNotSet |
| 252 | + void activeConsumerShouldGetUpdateNotificationAfterDisruption( |
| 253 | + java.util.function.Consumer<Consumer> disruption) { |
| 254 | + String consumerName = "foo"; |
| 255 | + Sync consumer1Active = sync(); |
| 256 | + Sync consumer1Inactive = sync(); |
| 257 | + Consumer consumer1 = |
| 258 | + environment.consumerBuilder().stream(stream) |
| 259 | + .name(consumerName) |
| 260 | + .noTrackingStrategy() |
| 261 | + .singleActiveConsumer() |
| 262 | + .consumerUpdateListener( |
| 263 | + context -> { |
| 264 | + if (context.isActive()) { |
| 265 | + consumer1Active.down(); |
| 266 | + } else { |
| 267 | + consumer1Inactive.down(); |
| 268 | + } |
| 269 | + return OffsetSpecification.next(); |
| 270 | + }) |
| 271 | + .messageHandler((context, message) -> {}) |
| 272 | + .build(); |
| 273 | + |
| 274 | + Sync consumer2Active = sync(); |
| 275 | + Sync consumer2Inactive = sync(); |
| 276 | + environment.consumerBuilder().stream(stream) |
| 277 | + .name(consumerName) |
| 278 | + .noTrackingStrategy() |
| 279 | + .singleActiveConsumer() |
| 280 | + .consumerUpdateListener( |
| 281 | + context -> { |
| 282 | + if (!context.isActive()) { |
| 283 | + consumer2Inactive.down(); |
| 284 | + } |
| 285 | + return OffsetSpecification.next(); |
| 286 | + }) |
| 287 | + .messageHandler((context, message) -> {}) |
| 288 | + .build(); |
| 289 | + |
| 290 | + assertThat(consumer1Active).completes(); |
| 291 | + assertThat(consumer2Inactive).hasNotCompleted(); |
| 292 | + assertThat(consumer1Inactive).hasNotCompleted(); |
| 293 | + assertThat(consumer2Active).hasNotCompleted(); |
| 294 | + |
| 295 | + disruption.accept(consumer1); |
| 296 | + |
| 297 | + assertThat(consumer2Inactive).hasNotCompleted(); |
| 298 | + assertThat(consumer1Inactive).completes(); |
| 299 | + } |
| 300 | + |
| 301 | + private static String connectionName(Consumer consumer) { |
| 302 | + return ((StreamConsumer) consumer).subscriptionConnectionName(); |
| 303 | + } |
| 304 | + |
| 305 | + private static String stream(Consumer consumer) { |
| 306 | + return ((StreamConsumer) consumer).stream(); |
| 307 | + } |
240 | 308 | }
|
0 commit comments