public void testAttachExistingSubscriptionsWhenARegionDown() throws Exception {
int batchSize = 10;
// sub it remotely to make subscriptions existed
for (Map.Entry<String, HedwigClient> entry : regionClientsMap.entrySet()) {
HedwigClient client = entry.getValue();
for (int i = 0; i < batchSize; i++) {
client.getSubscriber().asyncSubscribe(ByteString.copyFromUtf8("Topic" + i),
ByteString.copyFromUtf8("LocalSubscriber"), CreateOrAttach.CREATE_OR_ATTACH,
new TestCallback(queue), null);
assertTrue(queue.take());
}
}
// stop regions
for (int i=0; i<numRegions; i++) {
stopRegion(i);
}
// start regions again
for (int i=0; i<numRegions; i++) {
startRegion(i);
}
// first shut down a region
Random r = new Random();
int regionId = r.nextInt(numRegions);
stopRegion(regionId);
// subscribe to topics when a region shuts down
// it should succeed since the subscriptions existed before
for (HedwigClient client : regionClientsMap.values()) {
for (int i = 0; i < batchSize; i++) {
client.getSubscriber().asyncSubscribe(ByteString.copyFromUtf8("Topic" + i),
ByteString.copyFromUtf8("LocalSubscriber"), CreateOrAttach.CREATE_OR_ATTACH,
new TestCallback(queue), null);
assertTrue(queue.take());
}
}
// Start delivery for local subscribers in all regions
for (Map.Entry<String, HedwigClient> entry : regionClientsMap.entrySet()) {
HedwigClient client = entry.getValue();
for (int i = 0; i < batchSize; i++) {
client.getSubscriber().startDelivery(ByteString.copyFromUtf8("Topic" + i),
ByteString.copyFromUtf8("LocalSubscriber"), new TestMessageHandler(consumeQueue));
}
}
// start region again
startRegion(regionId);
// wait for retry
Thread.sleep(3 * TEST_RETRY_REMOTE_SUBSCRIBE_INTERVAL_VALUE);
String regionName = REGION_PREFIX + regionId;
HedwigClient client = regionClientsMap.get(regionName);
for (int i = 0; i < batchSize; i++) {
client.getSubscriber().asyncSubscribe(ByteString.copyFromUtf8("Topic" + i),
ByteString.copyFromUtf8("LocalSubscriber"), CreateOrAttach.CREATE_OR_ATTACH,
new TestCallback(queue), null);
assertTrue(queue.take());
client.getSubscriber().startDelivery(ByteString.copyFromUtf8("Topic" + i),
ByteString.copyFromUtf8("LocalSubscriber"), new TestMessageHandler(consumeQueue));
}
// Now start publishing messages for the subscribed topics in one of the
// regions and verify that it gets delivered and consumed in all of the
// other ones.
Publisher publisher = client.getPublisher();
for (int i = 0; i < batchSize; i++) {
publisher.asyncPublish(ByteString.copyFromUtf8("Topic" + i), Message.newBuilder().setBody(
ByteString.copyFromUtf8(regionName + "-Message" + i)).build(), new TestCallback(queue), null);
assertTrue(queue.take());
}