Search in sources :

Example 1 with VirtualTimeScheduler

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

the class FluxBufferTimeoutTest method downstreamDemandShouldBeAbleToDecreaseOnTimeSpan.

@Test
public void downstreamDemandShouldBeAbleToDecreaseOnTimeSpan() {
    Subscription[] subscriptionsHolder = new Subscription[1];
    CoreSubscriber<List<String>> actual = new LambdaSubscriber<>(null, e -> {
    }, null, s -> subscriptionsHolder[0] = s);
    VirtualTimeScheduler timeScheduler = VirtualTimeScheduler.getOrSet();
    FluxBufferTimeout.BufferTimeoutSubscriber<String, List<String>> test = new FluxBufferTimeout.BufferTimeoutSubscriber<String, List<String>>(actual, 5, 100, timeScheduler.createWorker(), ArrayList::new);
    Subscription subscription = Operators.emptySubscription();
    test.onSubscribe(subscription);
    subscriptionsHolder[0].request(1);
    assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(1L);
    timeScheduler.advanceTimeBy(Duration.ofMillis(100));
    assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(1L);
    test.onNext(String.valueOf("0"));
    timeScheduler.advanceTimeBy(Duration.ofMillis(100));
    assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(0L);
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 2 with VirtualTimeScheduler

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

the class FluxCacheTest method cacheFlux.

@Test
public void cacheFlux() {
    try {
        // delayElements now uses parallel() so VTS must be enabled everywhere
        VirtualTimeScheduler vts = VirtualTimeScheduler.getOrSet();
        Flux<Tuple2<Long, Integer>> source = Flux.just(1, 2, 3).delayElements(Duration.ofMillis(1000)).cache().elapsed();
        StepVerifier.create(source).then(() -> vts.advanceTimeBy(Duration.ofSeconds(3))).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 1).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 2).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 3).verifyComplete();
        StepVerifier.create(source).then(() -> vts.advanceTimeBy(Duration.ofSeconds(3))).expectNextMatches(t -> t.getT1() == 0 && t.getT2() == 1).expectNextMatches(t -> t.getT1() == 0 && t.getT2() == 2).expectNextMatches(t -> t.getT1() == 0 && t.getT2() == 3).verifyComplete();
    } finally {
        VirtualTimeScheduler.reset();
    }
}
Also used : StepVerifier(reactor.test.StepVerifier) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test)

Example 3 with VirtualTimeScheduler

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

the class FluxCacheTest method cacheFluxTTL2.

@Test
public void cacheFluxTTL2() {
    try {
        // delayElements now uses parallel() so VTS must be enabled everywhere
        VirtualTimeScheduler vts = VirtualTimeScheduler.getOrSet();
        AtomicInteger i = new AtomicInteger(0);
        Flux<Integer> source = Flux.defer(() -> Flux.just(i.incrementAndGet())).cache(Duration.ofMillis(2000));
        StepVerifier.create(source).expectNext(1).verifyComplete();
        StepVerifier.create(source).expectNext(1).verifyComplete();
        vts.advanceTimeBy(Duration.ofSeconds(3));
        StepVerifier.create(source).expectNext(2).verifyComplete();
    } finally {
        VirtualTimeScheduler.reset();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with VirtualTimeScheduler

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

the class FluxCacheTest method cacheFluxTTL.

@Test
public void cacheFluxTTL() {
    try {
        // delayElements now uses parallel() so VTS must be enabled everywhere
        VirtualTimeScheduler vts = VirtualTimeScheduler.getOrSet();
        Flux<Tuple2<Long, Integer>> source = Flux.just(1, 2, 3).delayElements(Duration.ofMillis(1000)).cache(Duration.ofMillis(2000)).elapsed();
        StepVerifier.create(source).then(() -> vts.advanceTimeBy(Duration.ofSeconds(3))).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 1).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 2).expectNextMatches(t -> t.getT1() == 1000 && t.getT2() == 3).verifyComplete();
        StepVerifier.create(source).then(() -> vts.advanceTimeBy(Duration.ofSeconds(3))).expectNextMatches(t -> t.getT1() == 0 && t.getT2() == 2).expectNextMatches(t -> t.getT1() == 0 && t.getT2() == 3).verifyComplete();
    } finally {
        VirtualTimeScheduler.reset();
    }
}
Also used : StepVerifier(reactor.test.StepVerifier) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test)

Example 5 with VirtualTimeScheduler

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

the class DefaultStepVerifierBuilderTests method manuallyManagedVirtualTime.

@Test(timeout = 4000)
public void manuallyManagedVirtualTime() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    try {
        VirtualTimeScheduler.getOrSet(vts);
        assertThat(VirtualTimeScheduler.get()).isSameAs(vts);
        Flux<String> flux = Flux.just("foo").delayElements(Duration.ofSeconds(4));
        DefaultVerifySubscriber<String> s = new DefaultStepVerifierBuilder<String>(StepVerifierOptions.create().initialRequest(Long.MAX_VALUE).virtualTimeSchedulerSupplier(() -> vts), // important to avoid triggering of vts capture-and-enable
        null).thenAwait(Duration.ofSeconds(1)).expectNext("foo").expectComplete().toSubscriber();
        flux.subscribe(s);
        vts.advanceTimeBy(Duration.ofSeconds(3));
        s.verify();
        assertThat(s.virtualTimeScheduler()).isSameAs(vts);
        assertThat(VirtualTimeScheduler.get()).isSameAs(vts);
    } finally {
        VirtualTimeScheduler.reset();
    }
}
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