Search in sources :

Example 16 with VirtualTimeScheduler

use of reactor.test.scheduler.VirtualTimeScheduler in project reactor-core by reactor.

the class MonoDelayElementTest method cancelUpstreamOnceWhenRejected.

@Test
public void cancelUpstreamOnceWhenRejected() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    vts.dispose();
    AtomicLong upstreamCancelCount = new AtomicLong();
    Mono<String> source = Mono.just("foo").log().hide().doOnCancel(upstreamCancelCount::incrementAndGet);
    try {
        StepVerifier.withVirtualTime(() -> new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, vts).log(), () -> vts, Long.MAX_VALUE).expectSubscription().verifyComplete();
    } catch (Throwable e) {
        assertThat(e).hasMessageContaining("Scheduler unavailable");
    } finally {
        assertThat(upstreamCancelCount.get()).isEqualTo(1);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 17 with VirtualTimeScheduler

use of reactor.test.scheduler.VirtualTimeScheduler in project reactor-core by reactor.

the class MonoDelayElementTest method cancelUpstreamOnceWhenCancelled.

@Test
public void cancelUpstreamOnceWhenCancelled() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    AtomicLong upstreamCancelCount = new AtomicLong();
    Mono<String> source = Mono.just("foo").log().hide().doOnCancel(() -> upstreamCancelCount.incrementAndGet());
    StepVerifier.withVirtualTime(() -> new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, vts), () -> vts, Long.MAX_VALUE).expectSubscription().expectNoEvent(Duration.ofSeconds(1)).thenCancel().verify();
    vts.advanceTimeBy(Duration.ofHours(1));
    assertThat(upstreamCancelCount.get()).isEqualTo(1);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 18 with VirtualTimeScheduler

use of reactor.test.scheduler.VirtualTimeScheduler in project reactor-core by reactor.

the class GuideTests method errorHandlingIntervalMillisNotContinued.

@Test
public void errorHandlingIntervalMillisNotContinued() throws InterruptedException {
    VirtualTimeScheduler virtualTimeScheduler = VirtualTimeScheduler.create();
    VirtualTimeScheduler.set(virtualTimeScheduler);
    Flux<String> flux = Flux.interval(Duration.ofMillis(250)).map(input -> {
        if (input < 3)
            return "tick " + input;
        throw new RuntimeException("boom");
    }).onErrorReturn("Uh oh");
    flux.subscribe(System.out::println);
    // Thread.sleep(2100); // <1>
    virtualTimeScheduler.advanceTimeBy(Duration.ofHours(1));
    StepVerifier.withVirtualTime(() -> flux, () -> virtualTimeScheduler, Long.MAX_VALUE).thenAwait(Duration.ofSeconds(3)).expectNext("tick 0").expectNext("tick 1").expectNext("tick 2").expectNext("Uh oh").verifyComplete();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) UnicastProcessor(reactor.core.publisher.UnicastProcessor) Arrays(java.util.Arrays) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Hooks(reactor.core.publisher.Hooks) Tuple2(reactor.util.function.Tuple2) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ConnectableFlux(reactor.core.publisher.ConnectableFlux) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestName(org.junit.rules.TestName) Duration(java.time.Duration) After(org.junit.After) LocalTime(java.time.LocalTime) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Schedulers(reactor.core.scheduler.Schedulers) Before(org.junit.Before) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) PublisherProbe(reactor.test.publisher.PublisherProbe) BaseSubscriber(reactor.core.publisher.BaseSubscriber) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Subscription(org.reactivestreams.Subscription) Optional(java.util.Optional) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 19 with VirtualTimeScheduler

use of reactor.test.scheduler.VirtualTimeScheduler in project reactor-core by reactor.

the class StepVerifierTests method verifyVirtualTimeOnErrorAsync.

@Test
public void verifyVirtualTimeOnErrorAsync() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    StepVerifier.withVirtualTime(() -> Flux.just(123).subscribeOn(vts), () -> vts, 0).thenRequest(1).thenAwait().expectNext(123).expectComplete().verify();
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 VirtualTimeScheduler (reactor.test.scheduler.VirtualTimeScheduler)19 Duration (java.time.Duration)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 StepVerifier (reactor.test.StepVerifier)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Subscription (org.reactivestreams.Subscription)5 Schedulers (reactor.core.scheduler.Schedulers)5 Tuple2 (reactor.util.function.Tuple2)5 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Executors (java.util.concurrent.Executors)3 TimeoutException (java.util.concurrent.TimeoutException)3 LongAdder (java.util.concurrent.atomic.LongAdder)3 Exceptions (reactor.core.Exceptions)3 Mono (reactor.core.publisher.Mono)3