runSubAfterCloseSubTest(true);
}
private void runSubAfterCloseSubTest(boolean sharedSubscriptionChannel) throws Exception {
HedwigClient client = new HedwigClient(new TestClientConfiguration(sharedSubscriptionChannel));
Publisher publisher = client.getPublisher();
final Subscriber subscriber = client.getSubscriber();
final ByteString topic = ByteString.copyFromUtf8("TestSubAfterCloseSub-" + sharedSubscriptionChannel);
final ByteString subid = ByteString.copyFromUtf8("mysub");
final CountDownLatch wakeupLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
final CountDownLatch subLatch = new CountDownLatch(1);
final CountDownLatch deliverLatch = new CountDownLatch(1);
try {
subscriber.subscribe(topic, subid, CreateOrAttach.CREATE_OR_ATTACH);
sleepDeliveryManager(wakeupLatch);
subscriber.asyncCloseSubscription(topic, subid, new Callback<Void>() {
@Override
public void operationFinished(Object ctx, Void resultOfOperation) {
closeLatch.countDown();
}
@Override
public void operationFailed(Object ctx, PubSubException exception) {
logger.error("Closesub failed : ", exception);
}
}, null);
subscriber.asyncSubscribe(topic, subid, CreateOrAttach.ATTACH, new Callback<Void>() {
@Override
public void operationFinished(Object ctx, Void resultOfOperation) {
try {
subscriber.startDelivery(topic, subid, new MessageHandler() {
@Override
public void deliver(ByteString topic, ByteString subid, Message msg,
Callback<Void> callback, Object context) {
deliverLatch.countDown();
}
});
} catch (Exception cnse) {
logger.error("Failed to start delivery : ", cnse);
}
subLatch.countDown();
}
@Override
public void operationFailed(Object ctx, PubSubException exception) {
logger.error("Failed to subscriber : ", exception);
}
}, null);
// Make the delivery manager thread sleep for a while.
// Before {@link https://issues.apache.org/jira/browse/BOOKKEEPER-507},
// subscribe would succeed before closesub, while closesub would clear
// a successful subscription w/o notifying the client.
TimeUnit.SECONDS.sleep(2);
// wake up fifo delivery thread
wakeupLatch.countDown();
// wait close sub to succeed
assertTrue("Async close subscription should succeed.",
closeLatch.await(5, TimeUnit.SECONDS));
assertTrue("Subscribe should succeed.",
subLatch.await(5, TimeUnit.SECONDS));
// publish a message
publisher.publish(topic, Message.newBuilder().setBody(topic).build());
// wait for seconds to receive message
assertTrue("Message should be received through successful subscription.",
deliverLatch.await(5, TimeUnit.SECONDS));
} finally {
client.close();