@Test
  public void testOneSubscriberReceivesAMessage() {

    final Subscriber subscriber = context.mock(Subscriber.class);
    final String message = "message";
    context.checking(
        new Expectations() {
          {
            // never(subscriber).receive(message);
            oneOf(subscriber).receive(message);
            // exactly(1).of(subscriber).receive(message);
            // atLeast(1).of(subscriber).receive(message);
            // atMost(2).of(subscriber).receive(message);
            // between(1, 5).of(subscriber).receive(message);
            // allowing(subscriber).receive(message);
            will(returnValue("Hello world"));
          }
        });

    Publisher publisher = new Publisher();
    publisher.add(subscriber);
    // execute
    String answer = publisher.publish(message);
    // verify
    context.assertIsSatisfied();
    Assert.assertEquals("Hello world", answer);
  }
Пример #2
0
  private void publish(IConfig config, File dir) throws Exception {
    String clazz = config.getString("class", null);
    Publisher publisher = null;
    if (clazz == null) publisher = new PathPublisher();
    else publisher = getAqua().createPublisher(clazz);

    publisher.publish(config, dir);
  }
 private void fireTimeout() {
   publisher.publish(
       new Distributor<KioskModeHandler.Listener>() {
         @Override
         public void deliverTo(Listener subscriber) {
           subscriber.notifyTimedOut();
         }
       });
 }
Пример #4
0
  @Test
  public void testBPubSub() throws InterruptedException {
    String topic = "test";

    // Start pubsub server
    Server server =
        new Server("127.0.0.1", Settings.CLIENT_SUBSCRIBE_PORT, Settings.CLIENT_PUBLISH_PORT);
    server.start();

    // Create publisher and subscriber
    Publisher publisher = new Publisher("127.0.0.1", Settings.CLIENT_PUBLISH_PORT);
    Subscriber subscriber = new Subscriber("127.0.0.1", Settings.CLIENT_SUBSCRIBE_PORT);

    // Subscribe to test topic
    TestPubSubCallback cb = new TestPubSubCallback();
    subscriber.subscribe(topic, cb);
    Thread.sleep(1000);

    // Publish a message
    System.out.println("Publisher publishing " + "hello" + " on topic " + topic);
    publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello").build());
    cb.awaitMessage("hello");

    // Publish a message
    System.out.println("Publisher publishing " + "hello" + " on topic " + "badtest");
    publisher.publish(
        "badtest", PubSubProtos.StringMessage.newBuilder().setMessage("hello").build());
    cb.awaitNoMessage("hello");

    // Publish a message
    System.out.println("Publisher publishing " + "hello2" + " on topic " + topic);
    publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello2").build());
    cb.awaitMessage("hello2");

    // Unsubscribe
    subscriber.unsubscribe(topic, cb);
    Thread.sleep(1000);

    // Publish a message
    System.out.println("Publisher publishing " + "hello2" + " on topic " + topic);
    publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello2").build());
    cb.awaitNoMessage("hello2");
  }