Search in sources :

Example 6 with VirtualTimeScheduler

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

the class DefaultStepVerifierBuilderTests method suppliedVirtualTimeButNoSourceDoesntEnableScheduler.

@Test
public void suppliedVirtualTimeButNoSourceDoesntEnableScheduler() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    new DefaultStepVerifierBuilder<String>(StepVerifierOptions.create().initialRequest(Long.MAX_VALUE).virtualTimeSchedulerSupplier(() -> vts), // important to avoid triggering of vts capture-and-enable
    null).expectNoEvent(Duration.ofSeconds(4)).expectComplete().toSubscriber();
    try {
        // also test the side effect case where VTS has been enabled and not reset
        VirtualTimeScheduler current = VirtualTimeScheduler.get();
        assertThat(current).isNotSameAs(vts);
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageContaining("VirtualTimeScheduler");
    }
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 7 with VirtualTimeScheduler

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

the class StepVerifierTests method virtualTimeSchedulerUseExactlySupplied.

@Test
public void virtualTimeSchedulerUseExactlySupplied() {
    VirtualTimeScheduler vts1 = VirtualTimeScheduler.create();
    VirtualTimeScheduler vts2 = VirtualTimeScheduler.create();
    VirtualTimeScheduler.getOrSet(vts1);
    StepVerifier.withVirtualTime(Mono::empty, () -> vts2, Long.MAX_VALUE).then(() -> assertThat(VirtualTimeScheduler.get()).isSameAs(vts2)).verifyComplete();
    assertThat(vts1.isDisposed()).isFalse();
    assertThat(vts2.isDisposed()).isTrue();
    assertThat(VirtualTimeScheduler.isFactoryEnabled()).isFalse();
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Mono(reactor.core.publisher.Mono) Test(org.junit.Test)

Example 8 with VirtualTimeScheduler

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

the class StepVerifierTests method verifyVirtualTimeOnNextIntervalManual.

@Test(timeout = 3000)
public void verifyVirtualTimeOnNextIntervalManual() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    StepVerifier.withVirtualTime(() -> Flux.interval(Duration.ofMillis(1000), vts).map(d -> "t" + d)).then(() -> vts.advanceTimeBy(Duration.ofHours(1))).expectNextCount(3600).thenCancel().verify();
}
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) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 9 with VirtualTimeScheduler

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

the class GuideTests method errorHandlingIntervalMillisRetried.

@Test
public void errorHandlingIntervalMillisRetried() throws InterruptedException {
    VirtualTimeScheduler virtualTimeScheduler = VirtualTimeScheduler.create();
    VirtualTimeScheduler.set(virtualTimeScheduler);
    Flux<Tuple2<Long, String>> flux = Flux.interval(Duration.ofMillis(250)).map(input -> {
        if (input < 3)
            return "tick " + input;
        throw new RuntimeException("boom");
    }).elapsed().retry(1);
    flux.subscribe(System.out::println, // <2>
    System.err::println);
    // Thread.sleep(2100); // <3>
    virtualTimeScheduler.advanceTimeBy(Duration.ofHours(1));
    StepVerifier.withVirtualTime(() -> flux, () -> virtualTimeScheduler, Long.MAX_VALUE).thenAwait(Duration.ofSeconds(3)).expectNextMatches(t -> t.getT2().equals("tick 0")).expectNextMatches(t -> t.getT2().equals("tick 1")).expectNextMatches(t -> t.getT2().equals("tick 2")).expectNextMatches(t -> t.getT2().equals("tick 0")).expectNextMatches(t -> t.getT2().equals("tick 1")).expectNextMatches(t -> t.getT2().equals("tick 2")).verifyErrorMessage("boom");
}
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) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test)

Example 10 with VirtualTimeScheduler

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

the class MonoTakeUntilOtherTest method apiTakeSchedulerShortcircuits.

@Test
public void apiTakeSchedulerShortcircuits() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    StepVerifier.create(Mono.delay(Duration.ofMillis(200)).take(Duration.ofSeconds(10), vts)).then(() -> vts.advanceTimeBy(Duration.ofSeconds(10))).verifyComplete();
}
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