|
| 1 | +package com.danubemessaging.client.it; |
| 2 | + |
| 3 | +import com.danubemessaging.client.Consumer; |
| 4 | +import com.danubemessaging.client.DanubeClient; |
| 5 | +import com.danubemessaging.client.DispatchStrategy; |
| 6 | +import com.danubemessaging.client.Producer; |
| 7 | +import com.danubemessaging.client.SubType; |
| 8 | +import com.danubemessaging.client.model.StreamMessage; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.CountDownLatch; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | +import java.util.concurrent.atomic.AtomicReference; |
| 16 | + |
| 17 | +import static com.danubemessaging.client.it.TestHelpers.*; |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * Reliable nack tests: verify that nacking a message on a reliable-dispatch |
| 22 | + * topic causes the broker to redeliver the same message. |
| 23 | + */ |
| 24 | +class ReliableNackIT { |
| 25 | + |
| 26 | + private void runReliableNack(String topicPrefix, SubType subType) throws Exception { |
| 27 | + DanubeClient client = newClient(); |
| 28 | + String topic = uniqueTopic(topicPrefix); |
| 29 | + |
| 30 | + Producer producer = client.newProducer() |
| 31 | + .withTopic(topic) |
| 32 | + .withName("producer_reliable_nack") |
| 33 | + .withDispatchStrategy(DispatchStrategy.RELIABLE) |
| 34 | + .build(); |
| 35 | + producer.create(); |
| 36 | + |
| 37 | + Consumer consumer = client.newConsumer() |
| 38 | + .withTopic(topic) |
| 39 | + .withConsumerName("cons_rel_nack") |
| 40 | + .withSubscription("rel_sub_nack") |
| 41 | + .withSubscriptionType(subType) |
| 42 | + .build(); |
| 43 | + consumer.subscribe(); |
| 44 | + |
| 45 | + try { |
| 46 | + byte[] payload = "nack-redelivery-test".getBytes(); |
| 47 | + |
| 48 | + AtomicReference<StreamMessage> firstDelivery = new AtomicReference<>(); |
| 49 | + AtomicReference<StreamMessage> redelivery = new AtomicReference<>(); |
| 50 | + AtomicReference<Throwable> error = new AtomicReference<>(); |
| 51 | + AtomicInteger deliveryCount = new AtomicInteger(); |
| 52 | + CountDownLatch done = new CountDownLatch(1); |
| 53 | + |
| 54 | + consumer.receive().subscribe(new TestHelpers.MessageCollector(2) { |
| 55 | + @Override |
| 56 | + public void onNext(StreamMessage item) { |
| 57 | + try { |
| 58 | + int count = deliveryCount.incrementAndGet(); |
| 59 | + if (count == 1) { |
| 60 | + // First delivery — nack it |
| 61 | + firstDelivery.set(item); |
| 62 | + assertArrayEquals(payload, item.payload(), "first delivery payload mismatch"); |
| 63 | + consumer.nack(item, 0L, "testing nack redelivery"); |
| 64 | + } else if (count == 2) { |
| 65 | + // Redelivered message — verify and ack |
| 66 | + redelivery.set(item); |
| 67 | + assertArrayEquals(payload, item.payload(), "redelivered payload mismatch"); |
| 68 | + assertEquals(firstDelivery.get().messageId().topicOffset(), |
| 69 | + item.messageId().topicOffset(), |
| 70 | + "redelivered message should have same offset"); |
| 71 | + consumer.ack(item); |
| 72 | + done.countDown(); |
| 73 | + } |
| 74 | + } catch (Throwable t) { |
| 75 | + error.set(t); |
| 76 | + done.countDown(); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + Thread.sleep(400); |
| 82 | + |
| 83 | + producer.send(payload, Map.of()); |
| 84 | + |
| 85 | + assertTrue(done.await(15, TimeUnit.SECONDS), |
| 86 | + "Timeout: received " + deliveryCount.get() + "/2 deliveries"); |
| 87 | + |
| 88 | + if (error.get() != null) { |
| 89 | + fail("Assertion failed in subscriber: " + error.get().getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + assertNotNull(firstDelivery.get(), "first delivery should not be null"); |
| 93 | + assertNotNull(redelivery.get(), "redelivery should not be null"); |
| 94 | + } finally { |
| 95 | + consumer.close(); |
| 96 | + client.close(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void reliableNackExclusive() throws Exception { |
| 102 | + runReliableNack("/default/reliable_nack_exclusive", SubType.EXCLUSIVE); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void reliableNackShared() throws Exception { |
| 107 | + runReliableNack("/default/reliable_nack_shared", SubType.SHARED); |
| 108 | + } |
| 109 | +} |
0 commit comments