void receiveInts(ByteString topic, ByteString subscriberId, int start, int num) throws Exception { IntMessageHandler msgHandler = new IntMessageHandler(topic, subscriberId, start, num); subscriber.startDelivery(topic, subscriberId, msgHandler); msgHandler.await(num, TimeUnit.SECONDS); subscriber.stopDelivery(topic, subscriberId); }
void sendXExpectLastY(ByteString topic, ByteString subid, final int x, final int y) throws Exception { for (int i = 0; i < x; i++) { publisher.publish( topic, org.apache.hedwig.protocol.PubSubProtocol.Message.newBuilder() .setBody(ByteString.copyFromUtf8(String.valueOf(i))) .build()); } org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions opts = org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions.newBuilder() .setCreateOrAttach( org.apache.hedwig.protocol.PubSubProtocol.SubscribeRequest.CreateOrAttach.ATTACH) .build(); subscriber.subscribe(topic, subid, opts); final AtomicInteger expected = new AtomicInteger(x - y); final CountDownLatch latch = new CountDownLatch(1); subscriber.startDelivery( topic, subid, new org.apache.hedwig.client.api.MessageHandler() { @Override public synchronized void deliver( ByteString topic, ByteString subscriberId, org.apache.hedwig.protocol.PubSubProtocol.Message msg, org.apache.hedwig.util.Callback<Void> callback, Object context) { try { int value = Integer.valueOf(msg.getBody().toStringUtf8()); if (value == expected.get()) { expected.incrementAndGet(); } else { logger.error( "Did not receive expected value, expected {}, got {}", expected.get(), value); expected.set(0); latch.countDown(); } if (expected.get() == x) { latch.countDown(); } callback.operationFinished(context, null); } catch (Exception e) { logger.error("Received bad message", e); latch.countDown(); } } }); assertTrue( "Timed out waiting for messages Y is " + y + " expected is currently " + expected.get(), latch.await(10, TimeUnit.SECONDS)); assertEquals("Should be expected message with " + x, x, expected.get()); subscriber.stopDelivery(topic, subid); subscriber.closeSubscription(topic, subid); }
void receiveNumModM( final ByteString topic, final ByteString subid, final int start, final int num, final int M) throws Exception { org.apache.hedwig.filter.ServerMessageFilter filter = new org.apache.hedwig.filter.ServerMessageFilter() { @Override public org.apache.hedwig.filter.ServerMessageFilter initialize(Configuration conf) { // do nothing return this; } @Override public void uninitialize() { // do nothing; } @Override public org.apache.hedwig.filter.MessageFilterBase setSubscriptionPreferences( ByteString topic, ByteString subscriberId, org.apache.hedwig.protocol.PubSubProtocol.SubscriptionPreferences preferences) { // do nothing; return this; } @Override public boolean testMessage(org.apache.hedwig.protocol.PubSubProtocol.Message msg) { int value = Integer.valueOf(msg.getBody().toStringUtf8()); return 0 == value % M; } }; filter.initialize(conf.getConf()); org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions opts = org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions.newBuilder() .setCreateOrAttach( org.apache.hedwig.protocol.PubSubProtocol.SubscribeRequest.CreateOrAttach.ATTACH) .build(); subscriber.subscribe(topic, subid, opts); final int base = start + M - start % M; final AtomicInteger expected = new AtomicInteger(base); final CountDownLatch latch = new CountDownLatch(1); subscriber.startDeliveryWithFilter( topic, subid, new org.apache.hedwig.client.api.MessageHandler() { public synchronized void deliver( ByteString topic, ByteString subscriberId, org.apache.hedwig.protocol.PubSubProtocol.Message msg, org.apache.hedwig.util.Callback<Void> callback, Object context) { try { int value = Integer.valueOf(msg.getBody().toStringUtf8()); // duplicated messages received, ignore them if (value > start) { if (value == expected.get()) { expected.addAndGet(M); } else { logger.error( "Did not receive expected value, expected {}, got {}", expected.get(), value); expected.set(0); latch.countDown(); } if (expected.get() == (base + num * M)) { latch.countDown(); } } callback.operationFinished(context, null); } catch (Exception e) { logger.error("Received bad message", e); latch.countDown(); } } }, (org.apache.hedwig.filter.ClientMessageFilter) filter); assertTrue( "Timed out waiting for messages mod " + M + " expected is " + expected.get(), latch.await(10, TimeUnit.SECONDS)); assertEquals( "Should be expected message with " + (base + num * M), (base + num * M), expected.get()); subscriber.stopDelivery(topic, subid); filter.uninitialize(); subscriber.closeSubscription(topic, subid); }
// throttle doesn't work talking with 41 server void throttleX41(ByteString topic, ByteString subid, final int X) throws Exception { org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions options = org.apache.hedwig.protocol.PubSubProtocol.SubscriptionOptions.newBuilder() .setCreateOrAttach( org.apache.hedwig.protocol.PubSubProtocol.SubscribeRequest.CreateOrAttach .CREATE_OR_ATTACH) .setMessageWindowSize(X) .build(); subscribe(topic, subid, options); closeSubscription(topic, subid); publishInts(topic, 1, 3 * X); subscribe(topic, subid); final AtomicInteger expected = new AtomicInteger(1); final CountDownLatch throttleLatch = new CountDownLatch(1); final CountDownLatch nonThrottleLatch = new CountDownLatch(1); subscriber.startDelivery( topic, subid, new org.apache.hedwig.client.api.MessageHandler() { @Override public synchronized void deliver( ByteString topic, ByteString subscriberId, org.apache.hedwig.protocol.PubSubProtocol.Message msg, org.apache.hedwig.util.Callback<Void> callback, Object context) { try { int value = Integer.valueOf(msg.getBody().toStringUtf8()); logger.debug("Received message {},", value); if (value == expected.get()) { expected.incrementAndGet(); } else { // error condition logger.error( "Did not receive expected value, expected {}, got {}", expected.get(), value); expected.set(0); throttleLatch.countDown(); nonThrottleLatch.countDown(); } if (expected.get() > X + 1) { throttleLatch.countDown(); } if (expected.get() == (3 * X + 1)) { nonThrottleLatch.countDown(); } callback.operationFinished(context, null); } catch (Exception e) { logger.error("Received bad message", e); throttleLatch.countDown(); nonThrottleLatch.countDown(); } } }); assertTrue( "Should Receive more messages than throttle value " + X, throttleLatch.await(10, TimeUnit.SECONDS)); assertTrue( "Timed out waiting for messages " + (3 * X + 1), nonThrottleLatch.await(10, TimeUnit.SECONDS)); assertEquals("Should be expected message with " + (3 * X + 1), 3 * X + 1, expected.get()); subscriber.stopDelivery(topic, subid); closeSubscription(topic, subid); }