@Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  for (String s : valuesToReturn) {
                    if (s == null) {
                      System.out.println("throwing exception");
                      try {
                        Thread.sleep(100);
                      } catch (Throwable e) {

                      }
                      observer.onError(new NullPointerException());
                      return;
                    } else {
                      observer.onNext(s);
                    }
                  }
                  System.out.println("subscription complete");
                  observer.onComplete();
                }
              });
      t.start();
    }
Ejemplo n.º 2
0
    @Override
    public void request(long n) {
      if (SubscriptionHelper.validateRequest(n)) {
        return;
      }
      if (compareAndSet(false, true)) {
        int i = 0;
        final Subscriber<? super Integer> a = s;
        final AtomicInteger c = counter;

        while (!cancelled) {
          a.onNext(i++);
          c.incrementAndGet();
        }
        System.out.println("unsubscribed after: " + i);
      }
    }
Ejemplo n.º 3
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");
  }
 @Override
 public void subscribe(Subscriber<? super String> observer) {
   observer.onSubscribe(EmptySubscription.INSTANCE);
   boolean errorThrown = false;
   for (String s : valuesToReturn) {
     if (s == null) {
       System.out.println("throwing exception");
       observer.onError(new NullPointerException());
       errorThrown = true;
       // purposefully not returning here so it will continue calling onNext
       // so that we also test that we handle bad sequences like this
     } else {
       observer.onNext(s);
     }
   }
   if (!errorThrown) {
     observer.onComplete();
   }
 }
    @Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  observer.onNext("hello");
                  observer.onComplete();
                }
              });
      t.start();
    }
    @Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  try {
                    Thread.sleep(100);
                  } catch (InterruptedException e) {
                    observer.onError(e);
                  }
                  observer.onNext("hello");
                  observer.onComplete();
                }
              });
      t.start();
    }
 @Override
 public void subscribe(Subscriber<? super String> observer) {
   observer.onSubscribe(EmptySubscription.INSTANCE);
   observer.onNext("hello");
   observer.onComplete();
 }