|
| 1 | +// Copyright (c) 2024 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// If you have any questions regarding licensing, please contact us at |
| 17 | + |
| 18 | +package com.rabbitmq.client.amqp.impl; |
| 19 | + |
| 20 | +import static com.rabbitmq.client.amqp.Management.QueueType.STREAM; |
| 21 | +import static com.rabbitmq.client.amqp.Resource.State.OPEN; |
| 22 | +import static com.rabbitmq.client.amqp.Resource.State.RECOVERING; |
| 23 | +import static com.rabbitmq.client.amqp.impl.Assertions.*; |
| 24 | +import static com.rabbitmq.client.amqp.impl.Cli.closeConnection; |
| 25 | +import static com.rabbitmq.client.amqp.impl.TestUtils.name; |
| 26 | +import static com.rabbitmq.client.amqp.impl.TestUtils.sync; |
| 27 | +import static org.assertj.core.api.Assertions.*; |
| 28 | + |
| 29 | +import com.rabbitmq.client.amqp.*; |
| 30 | +import com.rabbitmq.client.amqp.impl.TestUtils.Sync; |
| 31 | +import java.time.Duration; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.concurrent.atomic.AtomicLong; |
| 34 | +import java.util.stream.IntStream; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.TestInfo; |
| 39 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 40 | + |
| 41 | +@ExtendWith(AmqpTestInfrastructureExtension.class) |
| 42 | +public class AmqpConsumerTest { |
| 43 | + |
| 44 | + BackOffDelayPolicy backOffDelayPolicy = BackOffDelayPolicy.fixed(Duration.ofMillis(100)); |
| 45 | + Environment environment; |
| 46 | + Connection connection; |
| 47 | + String q; |
| 48 | + String connectionName; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void init(TestInfo info) { |
| 52 | + this.q = name(info); |
| 53 | + connection.management().queue(this.q).type(STREAM).declare(); |
| 54 | + this.connectionName = ((AmqpConnection) connection).name(); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + void tearDown() { |
| 59 | + connection.management().queueDeletion().delete(this.q); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void subscriptionListenerShouldBeCalledOnRecovery() { |
| 64 | + Sync subscriptionSync = sync(); |
| 65 | + Sync recoveredSync = sync(); |
| 66 | + connection |
| 67 | + .consumerBuilder() |
| 68 | + .queue(this.q) |
| 69 | + .subscriptionListener(ctx -> subscriptionSync.down()) |
| 70 | + .listeners(recoveredListener(recoveredSync)) |
| 71 | + .messageHandler((ctx, msg) -> {}) |
| 72 | + .build(); |
| 73 | + |
| 74 | + assertThat(subscriptionSync).completes(); |
| 75 | + assertThat(recoveredSync).hasNotCompleted(); |
| 76 | + sync().reset(); |
| 77 | + closeConnection(this.connectionName); |
| 78 | + assertThat(recoveredSync).completes(); |
| 79 | + assertThat(subscriptionSync).completes(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void streamConsumerRestartsWhereItLeftOff() { |
| 84 | + Connection publisherConnection = environment.connectionBuilder().build(); |
| 85 | + Publisher publisher = publisherConnection.publisherBuilder().queue(this.q).build(); |
| 86 | + int messageCount = 100; |
| 87 | + Runnable publish = |
| 88 | + () -> { |
| 89 | + Sync publishSync = sync(messageCount); |
| 90 | + Publisher.Callback callback = ctx -> publishSync.down(); |
| 91 | + IntStream.range(0, messageCount) |
| 92 | + .forEach( |
| 93 | + ignored -> { |
| 94 | + publisher.publish(publisher.message(), callback); |
| 95 | + }); |
| 96 | + assertThat(publishSync).completes(); |
| 97 | + }; |
| 98 | + |
| 99 | + publish.run(); |
| 100 | + |
| 101 | + Sync consumeSync = sync(messageCount); |
| 102 | + AtomicLong lastOffsetProcessed = new AtomicLong(-1); |
| 103 | + AtomicInteger consumedMessageCount = new AtomicInteger(0); |
| 104 | + AtomicInteger subscriptionListenerCallCount = new AtomicInteger(0); |
| 105 | + Sync recoveredSync = sync(); |
| 106 | + ConsumerBuilder.SubscriptionListener subscriptionListener = |
| 107 | + ctx -> { |
| 108 | + subscriptionListenerCallCount.incrementAndGet(); |
| 109 | + ctx.streamOptions().offset(lastOffsetProcessed.get() + 1); |
| 110 | + }; |
| 111 | + Consumer.MessageHandler messageHandler = |
| 112 | + (ctx, msg) -> { |
| 113 | + long offset = (long) msg.annotation("x-stream-offset"); |
| 114 | + ctx.accept(); |
| 115 | + lastOffsetProcessed.set(offset); |
| 116 | + consumedMessageCount.incrementAndGet(); |
| 117 | + consumeSync.down(); |
| 118 | + }; |
| 119 | + Consumer consumer = |
| 120 | + connection |
| 121 | + .consumerBuilder() |
| 122 | + .listeners(recoveredListener(recoveredSync)) |
| 123 | + .queue(this.q) |
| 124 | + .subscriptionListener(subscriptionListener) |
| 125 | + .messageHandler(messageHandler) |
| 126 | + .build(); |
| 127 | + |
| 128 | + assertThat(subscriptionListenerCallCount).hasValue(1); |
| 129 | + assertThat(consumeSync).completes(); |
| 130 | + |
| 131 | + closeConnection(this.connectionName); |
| 132 | + assertThat(recoveredSync).completes(); |
| 133 | + assertThat(subscriptionListenerCallCount).hasValue(2); |
| 134 | + assertThat(consumedMessageCount).hasValue(messageCount); |
| 135 | + |
| 136 | + long offsetAfterRecovery = lastOffsetProcessed.get(); |
| 137 | + consumeSync.reset(messageCount); |
| 138 | + publish.run(); |
| 139 | + assertThat(consumeSync).completes(); |
| 140 | + assertThat(consumedMessageCount).hasValue(messageCount * 2); |
| 141 | + assertThat(lastOffsetProcessed).hasValueGreaterThan(offsetAfterRecovery); |
| 142 | + |
| 143 | + consumer.close(); |
| 144 | + |
| 145 | + long offsetAfterClosing = lastOffsetProcessed.get(); |
| 146 | + consumeSync.reset(messageCount); |
| 147 | + publish.run(); |
| 148 | + |
| 149 | + connection |
| 150 | + .consumerBuilder() |
| 151 | + .queue(this.q) |
| 152 | + .subscriptionListener(subscriptionListener) |
| 153 | + .messageHandler(messageHandler) |
| 154 | + .build(); |
| 155 | + |
| 156 | + assertThat(subscriptionListenerCallCount).hasValue(3); |
| 157 | + assertThat(consumeSync).completes(); |
| 158 | + assertThat(consumedMessageCount).hasValue(messageCount * 3); |
| 159 | + assertThat(lastOffsetProcessed).hasValueGreaterThan(offsetAfterClosing); |
| 160 | + } |
| 161 | + |
| 162 | + private static Resource.StateListener recoveredListener(Sync sync) { |
| 163 | + return context -> { |
| 164 | + if (context.previousState() == RECOVERING && context.currentState() == OPEN) { |
| 165 | + sync.down(); |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | +} |
0 commit comments