Ejemplo n.º 1
0
  @Test
  public void testEquals() {
    Buffer buffer = Buffer.wrap("Hello");

    assertTrue(buffer.equals(Buffer.wrap("Hello")));
    assertFalse(buffer.equals(Buffer.wrap("Other")));
  }
  @Test
  public void testCompleteSignalIsReceived() throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext());
    Flux.just(Buffer.wrap("One"), Buffer.wrap("Two"), Buffer.wrap("Three")).subscribe(processor);

    TestSubscriber<String> subscriber = TestSubscriber.create(0);
    Buffer.bufferToString(processor).subscribe(subscriber);

    subscriber.request(1);
    subscriber.awaitAndAssertNextValues("One");

    subscriber.request(1);
    subscriber.awaitAndAssertNextValues("Two");

    subscriber.request(1);
    subscriber.awaitAndAssertNextValues("Three").assertComplete();
  }
  @Test
  public void testWorksWithTwoSubscribersViaEmitter() throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext());
    Flux.just(Buffer.wrap("Live"), Buffer.wrap("Hard"), Buffer.wrap("Die"), Buffer.wrap("Harder"))
        .subscribe(processor);

    FluxProcessor<Buffer, Buffer> emitter = EmitterProcessor.create();
    processor.subscribe(emitter);

    TestSubscriber<String> subscriber1 = TestSubscriber.create();
    Buffer.bufferToString(emitter).subscribe(subscriber1);

    TestSubscriber<String> subscriber2 = TestSubscriber.create();
    Buffer.bufferToString(emitter).subscribe(subscriber2);

    subscriber1.awaitAndAssertNextValues("Live", "Hard", "Die", "Harder").assertComplete();
    subscriber2.awaitAndAssertNextValues("Live", "Hard", "Die", "Harder").assertComplete();
  }
 @Test
 public void decode() throws InterruptedException {
   Stream<ByteBuffer> source =
       Streams.just(Buffer.wrap("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}").byteBuffer());
   List<Object> results =
       Streams.wrap(decoder.decode(source, ResolvableType.forClass(Pojo.class), null))
           .toList()
           .await();
   assertEquals(1, results.size());
   assertEquals("foofoo", ((Pojo) results.get(0)).getFoo());
 }
 @Test
 public void decodeMultipleChunksToArray() throws InterruptedException {
   JsonObjectDecoder decoder = new JsonObjectDecoder(true);
   Stream<ByteBuffer> source =
       Streams.just(
           Buffer.wrap("[{\"foo\": \"foofoo\", \"bar\"").byteBuffer(),
           Buffer.wrap(": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")
               .byteBuffer());
   List<String> results =
       Streams.wrap(decoder.decode(source, null, null))
           .map(
               chunk -> {
                 byte[] b = new byte[chunk.remaining()];
                 chunk.get(b);
                 return new String(b, StandardCharsets.UTF_8);
               })
           .toList()
           .await();
   assertEquals(2, results.size());
   assertEquals("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}", results.get(0));
   assertEquals("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}", results.get(1));
 }
  @Test
  public void testNextSignalIsReceived() throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext());
    TestSubscriber<String> subscriber = TestSubscriber.create(0);
    Buffer.bufferToString(processor).subscribe(subscriber);
    subscriber.request(4);

    Flux.just(
            Buffer.wrap("Live"),
            Buffer.wrap("Hard"),
            Buffer.wrap("Die"),
            Buffer.wrap("Harder"),
            Buffer.wrap("Extra"))
        .subscribe(processor);

    subscriber.awaitAndAssertNextValues("Live", "Hard", "Die", "Harder");

    subscriber.request(1);

    subscriber.awaitAndAssertNextValues("Extra");
    subscriber.await();
  }
  @Test
  public void testClientReceivesException() throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext());

    // as error is delivered on a different channelId compared to signal
    // its delivery could shutdown the processor before the processor subscriber
    // receives signal
    Flux.concat(
            Flux.just(Buffer.wrap("Item")),
            Flux.error(new RuntimeException("Something went wrong")))
        .subscribe(processor);

    TestSubscriber<String> subscriber = TestSubscriber.create();
    Buffer.bufferToString(processor).subscribe(subscriber);

    subscriber
        .await(TIMEOUT)
        .assertErrorWith(t -> assertThat(t.getMessage(), is("Something went wrong")));
  }
  @Test
  public void testRemotePublisherReceivesCompleteBeforeProcessorIsShutdown()
      throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext());

    Flux.just(Buffer.wrap("Live")).subscribe(processor);

    TestSubscriber<String> subscriber = TestSubscriber.create(0);
    Buffer.bufferToString(processor).subscribe(subscriber);

    AeronFlux remotePublisher = new AeronFlux(createContext());
    TestSubscriber<String> remoteSubscriber = TestSubscriber.create(0);
    Buffer.bufferToString(remotePublisher).subscribe(remoteSubscriber);

    subscriber.request(1);
    remoteSubscriber.request(1);

    subscriber.awaitAndAssertNextValues("Live").assertComplete();
    remoteSubscriber.awaitAndAssertNextValues("Live").assertComplete();
  }
  @Test
  public void testCancelsUpstreamSubscriptionWhenLastSubscriptionIsCancelledAndAutoCancel()
      throws InterruptedException {
    AeronProcessor processor = AeronProcessor.create(createContext().autoCancel(true));

    final CountDownLatch subscriptionCancelledLatch = new CountDownLatch(1);
    Publisher<Buffer> dataPublisher =
        new Publisher<Buffer>() {
          @Override
          public void subscribe(Subscriber<? super Buffer> subscriber) {
            subscriber.onSubscribe(
                new Subscription() {
                  @Override
                  public void request(long n) {
                    System.out.println("Requested: " + n);
                  }

                  @Override
                  public void cancel() {
                    System.out.println("Upstream subscription cancelled");
                    subscriptionCancelledLatch.countDown();
                  }
                });
          }
        };
    dataPublisher.subscribe(processor);

    TestSubscriber<String> client = TestSubscriber.create();

    Buffer.bufferToString(processor).subscribe(client);

    processor.onNext(Buffer.wrap("Hello"));

    client.awaitAndAssertNextValues("Hello").cancel();

    assertTrue(
        "Subscription wasn't cancelled",
        subscriptionCancelledLatch.await(TIMEOUT.getSeconds(), TimeUnit.SECONDS));
  }
 @Override
 public Buffer createElement(int element) {
   return Buffer.wrap("" + element);
 }