Search in sources :

Example 1 with TestPublisher

use of reactor.test.publisher.TestPublisher in project reactor-core by reactor.

the class StepVerifierTests method thenCancel_cancelsAfterFirst2.

@Test
public void thenCancel_cancelsAfterFirst2() {
    TestPublisher<Long> publisher = TestPublisher.create();
    AtomicBoolean downStreamCancelled = new AtomicBoolean();
    AtomicBoolean asserted = new AtomicBoolean();
    Flux<Long> source = publisher.flux().doOnCancel(() -> downStreamCancelled.set(true));
    Duration took = StepVerifier.create(source).then(() -> Schedulers.elastic().schedule(() -> publisher.next(0L))).assertNext(next -> {
        asserted.set(true);
        assertThat(next).isEqualTo(0L);
    }).then(() -> Schedulers.elastic().schedule(() -> publisher.next(1L))).thenCancel().verify(Duration.ofSeconds(5));
    publisher.assertCancelled();
    assertThat(asserted.get()).as("expectation processed").isTrue();
    assertThat(downStreamCancelled.get()).as("is cancelled by awaitThenCancel").isTrue();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) UnicastProcessor(reactor.core.publisher.UnicastProcessor) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) ExecutorService(java.util.concurrent.ExecutorService) DirectProcessor(reactor.core.publisher.DirectProcessor) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Collections.emptyList(java.util.Collections.emptyList) Context(reactor.util.context.Context) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) Flux(reactor.core.publisher.Flux) List(java.util.List) Fuseable(reactor.core.Fuseable) REQUEST_OVERFLOW(reactor.test.publisher.TestPublisher.Violation.REQUEST_OVERFLOW) Assert(org.junit.Assert) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Duration(java.time.Duration) Test(org.junit.Test)

Example 2 with TestPublisher

use of reactor.test.publisher.TestPublisher in project reactor-core by reactor.

the class StepVerifierTests method thenCancel_cancelsAfterFirst.

@Test
public void thenCancel_cancelsAfterFirst() {
    TestPublisher<Long> publisher = TestPublisher.create();
    AtomicBoolean downStreamCancelled = new AtomicBoolean();
    AtomicBoolean asserted = new AtomicBoolean();
    Flux<Long> source = publisher.flux().onBackpressureBuffer().doOnCancel(() -> downStreamCancelled.set(true)).log();
    Duration took = StepVerifier.create(source, 1).then(() -> Schedulers.elastic().schedule(() -> publisher.next(0L))).assertNext(next -> {
        LockSupport.parkNanos(Duration.ofMillis(500).toNanos());
        asserted.set(true);
        assertThat(next).isEqualTo(0L);
    }).then(() -> Schedulers.elastic().schedule(() -> publisher.next(1L))).then(() -> Schedulers.elastic().schedule(() -> publisher.next(2L), 50, TimeUnit.MILLISECONDS)).expectNoEvent(Duration.ofMillis(100)).thenRequest(1).thenRequest(1).assertNext(next -> {
        LockSupport.parkNanos(Duration.ofMillis(500).toNanos());
        assertThat(next).isEqualTo(1L);
    }).thenAwait(Duration.ofSeconds(2)).thenCancel().verify(Duration.ofSeconds(5));
    publisher.assertCancelled();
    assertThat(asserted.get()).as("expectation processed").isTrue();
    assertThat(downStreamCancelled.get()).as("is cancelled by awaitThenCancel").isTrue();
    assertThat(took.toMillis()).as("blocked on first assertNext").isGreaterThanOrEqualTo(1000L);
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) UnicastProcessor(reactor.core.publisher.UnicastProcessor) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) ExecutorService(java.util.concurrent.ExecutorService) DirectProcessor(reactor.core.publisher.DirectProcessor) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Collections.emptyList(java.util.Collections.emptyList) Context(reactor.util.context.Context) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) Flux(reactor.core.publisher.Flux) List(java.util.List) Fuseable(reactor.core.Fuseable) REQUEST_OVERFLOW(reactor.test.publisher.TestPublisher.Violation.REQUEST_OVERFLOW) Assert(org.junit.Assert) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Date (java.util.Date)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 Future (java.util.concurrent.Future)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LongAdder (java.util.concurrent.atomic.LongAdder)2 LockSupport (java.util.concurrent.locks.LockSupport)2 Assertions (org.assertj.core.api.Assertions)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 Fuseable (reactor.core.Fuseable)2 DirectProcessor (reactor.core.publisher.DirectProcessor)2