Java Code Examples for reactor.test.publisher.TestPublisher#assertWasRequested()

The following examples show how to use reactor.test.publisher.TestPublisher#assertWasRequested() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonoDelayElementTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void cancelUpstreamOnceWhenRejected() {
	VirtualTimeScheduler vts = VirtualTimeScheduler.create();
	vts.dispose();

	TestPublisher<Object> testPublisher = TestPublisher.createCold();
	testPublisher.emit("Hello");

	StepVerifier.create(new MonoDelayElement<>(testPublisher.mono(), 2, TimeUnit.SECONDS, vts))
	            .verifyErrorSatisfies(e -> {
		            assertThat(e)
				            .isInstanceOf(RejectedExecutionException.class)
				            .hasMessage("Scheduler unavailable");
	            });

	testPublisher.assertWasRequested();
	testPublisher.assertCancelled(1);
}
 
Example 2
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotHangWhenOneElementUpstream() {
    TestPublisher<Long> publisher = TestPublisher.createCold();

    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst((first, innerFlux) ->
                                                  innerFlux.map(String::valueOf)
                                                           .subscriberContext(Context.of("a", "b"))
                                              )
                                              .subscriberContext(Context.of("a", "c"))
                                              .subscriberContext(Context.of("c", "d"));

    publisher.next(1L);
    publisher.complete();

    StepVerifier.create(switchTransformed, 0)
                .thenRequest(1)
                .expectNext("1")
                .expectComplete()
                .verify(Duration.ofSeconds(10));

    publisher.assertWasRequested();
    publisher.assertNoRequestOverflow();
}
 
Example 3
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void backpressureTest() {
    TestPublisher<Long> publisher = TestPublisher.createCold();
    AtomicLong requested = new AtomicLong();

    Flux<String> switchTransformed = publisher.flux()
                                              .doOnRequest(requested::addAndGet)
                                              .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf));

    publisher.next(1L);

    StepVerifier.create(switchTransformed, 0)
                .thenRequest(1)
                .expectNext("1")
                .thenRequest(1)
                .then(() -> publisher.next(2L))
                .expectNext("2")
                .then(publisher::complete)
                .expectComplete()
                .verify(Duration.ofSeconds(10));

    publisher.assertWasRequested();
    publisher.assertNoRequestOverflow();

    Assertions.assertThat(requested.get()).isEqualTo(2L);
}
 
Example 4
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldErrorOnOverflowTest() {
    TestPublisher<Long> publisher = TestPublisher.createCold();

    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf));

    publisher.next(1L);

    StepVerifier.create(switchTransformed, 0)
                .thenRequest(1)
                .expectNext("1")
                .then(() -> publisher.next(2L))
                .expectErrorSatisfies(t -> Assertions
                    .assertThat(t)
                    .isInstanceOf(IllegalStateException.class)
                    .hasMessage("Can't deliver value due to lack of requests")
                )
                .verify(Duration.ofSeconds(10));

    publisher.assertWasRequested();
    publisher.assertNoRequestOverflow();
}
 
Example 5
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeAbleToBeCancelledProperly2() {
    TestPublisher<Integer> publisher = TestPublisher.createCold();
    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst((first, innerFlux) ->
                                                  innerFlux
                                                      .map(String::valueOf)
                                                      .take(1)
                                              );

    publisher.next(1);
    publisher.next(2);
    publisher.next(3);
    publisher.next(4);

    StepVerifier.create(switchTransformed, 1)
                .expectNext("1")
                .expectComplete()
                .verify(Duration.ofSeconds(10));

    publisher.assertCancelled();
    publisher.assertWasRequested();
}
 
Example 6
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeAbleToBeCancelledProperly3() {
    TestPublisher<Integer> publisher = TestPublisher.createCold();
    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst((first, innerFlux) ->
                                                      innerFlux
                                                              .map(String::valueOf)
                                              )
                                              .take(1);

    publisher.next(1);
    publisher.next(2);
    publisher.next(3);
    publisher.next(4);

    StepVerifier.create(switchTransformed, 1)
                .expectNext("1")
                .expectComplete()
                .verify(Duration.ofSeconds(10));

    publisher.assertCancelled();
    publisher.assertWasRequested();
}
 
Example 7
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBeAbleToCatchDiscardedElement() {
    TestPublisher<Integer> publisher = TestPublisher.create();
    Integer[] discarded = new Integer[1];
    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf))
                                              .doOnDiscard(Integer.class, e -> discarded[0] = e);

    StepVerifier.create(switchTransformed, 0)
                .expectSubscription()
                .then(() -> publisher.next(1))
                .thenCancel()
                .verify(Duration.ofSeconds(10));

    publisher.assertCancelled();
    publisher.assertWasRequested();

    Assertions.assertThat(discarded).containsExactly(1);
}
 
Example 8
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFailOnIncorrectPublisherBehavior() {
    TestPublisher<Long> publisher =
            TestPublisher.createNoncompliant(TestPublisher.Violation.CLEANUP_ON_TERMINATE);
    Flux<Long> switchTransformed = publisher.flux()
                                            .switchOnFirst((first, innerFlux) -> innerFlux.subscriberContext(Context.of("a", "b")));

    StepVerifier.create(new Flux<Long>() {
        @Override
        public void subscribe(CoreSubscriber<? super Long> actual) {
            switchTransformed.subscribe(actual);
            publisher.next(1L);
        }
    }, 0)
                .thenRequest(1)
                .expectNext(1L)
                .thenRequest(1)
                .then(() -> publisher.next(2L))
                .expectNext(2L)
                .then(() -> publisher.error(new RuntimeException()))
                .then(() -> publisher.error(new RuntimeException()))
                .then(() -> publisher.error(new RuntimeException()))
                .then(() -> publisher.error(new RuntimeException()))
                .expectError()
                .verifyThenAssertThat(Duration.ofSeconds(5))
                .hasDroppedErrors(3)
                .tookLessThan(Duration.ofSeconds(10));

    publisher.assertWasRequested();
    publisher.assertNoRequestOverflow();
}
 
Example 9
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
// Since context is immutable, with switchOnFirst it should not be mutable as well. Upstream should observe downstream
// Inner should be able to access downstreamContext but should not modify upstream context after the first element
public void shouldNotBeAbleToAccessUpstreamContext() {
    TestPublisher<Long> publisher = TestPublisher.createCold();

    Flux<String> switchTransformed = publisher.flux()
                                              .switchOnFirst(
                                                  (first, innerFlux) -> innerFlux.map(String::valueOf)
                                                                                 .subscriberContext(Context.of("a", "b"))
                                              )
                                              .subscriberContext(Context.of("a", "c"))
                                              .subscriberContext(Context.of("c", "d"));

    publisher.next(1L);

    StepVerifier.create(switchTransformed, 0)
                .thenRequest(1)
                .expectNext("1")
                .thenRequest(1)
                .then(() -> publisher.next(2L))
                .expectNext("2")
                .expectAccessibleContext()
                .contains("a", "c")
                .contains("c", "d")
                .then()
                .then(publisher::complete)
                .expectComplete()
                .verify(Duration.ofSeconds(10));

    publisher.assertWasRequested();
    publisher.assertNoRequestOverflow();
}
 
Example 10
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToBeCancelledProperly() throws InterruptedException {
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    TestPublisher<Integer> publisher = TestPublisher.createCold();
    Flux<String> switchTransformed = publisher.flux()
            .doOnCancel(latch2::countDown)
            .switchOnFirst((first, innerFlux) -> innerFlux.map(String::valueOf))
            .doOnCancel(() -> {
                try {
                    if (!latch1.await(5, TimeUnit.SECONDS)) throw new IllegalStateException("latch didn't complete in 5s");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            })
            .cancelOn(Schedulers.boundedElastic());

    publisher.next(1);

    StepVerifier stepVerifier = StepVerifier.create(switchTransformed, 0)
            .thenCancel()
            .verifyLater();

    latch1.countDown();
    stepVerifier.verify(Duration.ofSeconds(10));

    Assertions.assertThat(latch2.await(1, TimeUnit.SECONDS)).isTrue();

    Instant endTime = Instant.now().plusSeconds(5);
    while (!publisher.wasCancelled()) {
        if (endTime.isBefore(Instant.now())) {
            break;
        }
    }
    publisher.assertCancelled();
    publisher.assertWasRequested();
}
 
Example 11
Source File: DefaultRSocketClientTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("interactions")
@SuppressWarnings({"unchecked", "rawtypes"})
public void shouldHaveNoLeaksOnPayloadInCaseOfRacingOfOnNextAndCancel(
    BiFunction<RSocketClient, Publisher<Payload>, Publisher<?>> request, FrameType requestType)
    throws Throwable {
  Assumptions.assumeThat(requestType).isNotEqualTo(FrameType.REQUEST_CHANNEL);

  for (int i = 0; i < 10000; i++) {
    ClientSocketRule rule = new ClientSocketRule();
    rule.apply(
            new Statement() {
              @Override
              public void evaluate() {}
            },
            null)
        .evaluate();
    Payload payload = ByteBufPayload.create("test", "testMetadata");
    TestPublisher<Payload> testPublisher =
        TestPublisher.createNoncompliant(TestPublisher.Violation.DEFER_CANCELLATION);
    AssertSubscriber assertSubscriber = AssertSubscriber.create(0);

    Publisher<?> publisher = request.apply(rule.client, testPublisher);
    publisher.subscribe(assertSubscriber);

    testPublisher.assertWasNotRequested();

    assertSubscriber.request(1);

    testPublisher.assertWasRequested();
    testPublisher.assertMaxRequested(1);
    testPublisher.assertMinRequested(1);

    RaceTestUtils.race(
        () -> {
          testPublisher.next(payload);
          rule.delayer.run();
        },
        assertSubscriber::cancel);

    Collection<ByteBuf> sent = rule.connection.getSent();
    if (sent.size() == 1) {
      Assertions.assertThat(sent)
          .allMatch(bb -> FrameHeaderCodec.frameType(bb).equals(requestType))
          .allMatch(ReferenceCounted::release);
    } else if (sent.size() == 2) {
      Assertions.assertThat(sent)
          .first()
          .matches(bb -> FrameHeaderCodec.frameType(bb).equals(requestType))
          .matches(ReferenceCounted::release);
      Assertions.assertThat(sent)
          .element(1)
          .matches(bb -> FrameHeaderCodec.frameType(bb).equals(FrameType.CANCEL))
          .matches(ReferenceCounted::release);
    } else {
      Assertions.assertThat(sent).isEmpty();
    }

    rule.allocator.assertHasNoLeaks();
  }
}