Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ void tryCompletePending() {
if (future == null) {
assert(messageAndCnx == null);
} else {
future.complete(messageAndCnx.msg);
if (!future.complete(messageAndCnx.msg)) {
messageAndCnx.msg.close();
closeAsync();
}

ClientCnx currentCnx = cnx();
if (currentCnx == messageAndCnx.cnx) {
Expand All @@ -137,10 +140,14 @@ private void reset() {
while (!pendingRawReceives.isEmpty()) {
toError.add(pendingRawReceives.remove());
}
RawMessageAndCnx m = incomingRawMessages.poll();
while (m != null) {
m.msg.close();
m = incomingRawMessages.poll();
}
incomingRawMessages.clear();
}
toError.forEach((f) -> f.completeExceptionally(
new PulsarClientException.ConsumerBusyException("Sought while reading")));
toError.forEach((f) -> f.cancel(false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -333,4 +334,31 @@ public void testAcknowledgeWithProperties() throws Exception {
Assert.assertEquals(ledger.openCursor(subscription).getProperties().get("foobar"),
Long.valueOf(0xdeadbeefdecaL));
}

@Test
public void testReadCancellationOnClose() throws Exception {
int numKeys = 10;

String topic = "persistent://my-property/use/my-ns/my-raw-topic";
publishMessages(topic, numKeys/2);

RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
List<Future<RawMessage>> futures = new ArrayList<>();
for (int i = 0; i < numKeys; i++) {
futures.add(reader.readNextAsync());
}

for (int i = 0; i < numKeys/2; i++) {
futures.remove(0).get(5, TimeUnit.SECONDS); // complete successfully
}
reader.closeAsync().get();
while (!futures.isEmpty()) {
try {
futures.remove(0).get(5, TimeUnit.SECONDS);
Assert.fail("Should have been cancelled");
} catch (CancellationException ee) {
// correct behaviour
}
}
}
}