Esempio n. 1
0
 @Override
 public boolean runCmd(String[] args) throws Exception {
   CreateOrAttach mode;
   boolean receive = true;
   if (args.length < 3) {
     return false;
   } else if (args.length == 3) {
     mode = CreateOrAttach.ATTACH;
     receive = true;
   } else {
     try {
       mode = CreateOrAttach.valueOf(Integer.parseInt(args[3]));
     } catch (Exception e) {
       System.err.println("Unknow mode : " + args[3]);
       return false;
     }
     if (args.length >= 5) {
       try {
         receive = Boolean.parseBoolean(args[4]);
       } catch (Exception e) {
         receive = false;
       }
     }
   }
   if (mode == null) {
     System.err.println("Unknow mode : " + args[3]);
     return false;
   }
   ByteString topic = ByteString.copyFromUtf8(args[1]);
   ByteString subId = ByteString.copyFromUtf8(args[2]);
   try {
     SubscriptionOptions options =
         SubscriptionOptions.newBuilder().setCreateOrAttach(mode).setForceAttach(false).build();
     subscriber.subscribe(topic, subId, options);
     if (receive) {
       subscriber.startDelivery(topic, subId, consoleHandler);
       System.out.println("SUB DONE AND RECEIVE");
     } else {
       System.out.println("SUB DONE BUT NOT RECEIVE");
     }
   } catch (Exception e) {
     System.err.println("SUB FAILED");
     e.printStackTrace();
   }
   return true;
 }
  @Test
  public void testInvalidServerSideMessageFilter() throws Exception {
    ByteString topic = ByteString.copyFromUtf8("TestInvalidMessageFilter");
    ByteString subid = ByteString.copyFromUtf8("mysub");

    SubscriptionOptions options =
        SubscriptionOptions.newBuilder()
            .setCreateOrAttach(CreateOrAttach.CREATE_OR_ATTACH)
            .setMessageFilter("Invalid_Message_Filter")
            .build();
    try {
      subscriber.subscribe(topic, subid, options);
      // coun't reach here
      fail("Should fail subscribe with invalid message filter");
    } catch (PubSubException pse) {
      assertTrue(
          "Should respond with INVALID_MESSAGE_FILTER",
          pse.getMessage().contains("INVALID_MESSAGE_FILTER"));
    }
  }
  private void receiveNumModM(
      final ByteString topic,
      final ByteString subid,
      final String filterClassName,
      final ClientMessageFilter filter,
      final int start,
      final int num,
      final int M,
      final boolean consume)
      throws Exception {
    PubSubProtocol.Map userOptions =
        PubSubProtocol.Map.newBuilder()
            .addEntries(
                PubSubProtocol.Map.Entry.newBuilder()
                    .setKey(OPT_MOD)
                    .setValue(ByteString.copyFromUtf8(String.valueOf(M))))
            .build();
    SubscriptionOptions.Builder optionsBuilder =
        SubscriptionOptions.newBuilder()
            .setCreateOrAttach(CreateOrAttach.ATTACH)
            .setOptions(userOptions);
    if (null != filterClassName) {
      optionsBuilder.setMessageFilter(filterClassName);
    }
    subscriber.subscribe(topic, subid, optionsBuilder.build());

    final int base = start + M - start % M;

    final AtomicInteger expected = new AtomicInteger(base);
    final CountDownLatch latch = new CountDownLatch(1);
    MessageHandler msgHandler =
        new MessageHandler() {
          public synchronized void deliver(
              ByteString topic,
              ByteString subscriberId,
              Message msg,
              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);
              if (consume) {
                subscriber.consume(topic, subid, msg.getMsgId());
              }
            } catch (Exception e) {
              logger.error("Received bad message", e);
              latch.countDown();
            }
          }
        };
    if (null != filter) {
      subscriber.startDeliveryWithFilter(topic, subid, msgHandler, filter);
    } else {
      subscriber.startDelivery(topic, subid, msgHandler);
    }
    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);
    subscriber.closeSubscription(topic, subid);
  }