Search in sources :

Example 11 with VirtualTimeScheduler

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

the class FluxRefCountGraceTest method shouldNotRetainSubscriptionToSourceWhenComplete.

@Test
public void shouldNotRetainSubscriptionToSourceWhenComplete() throws Exception {
    VirtualTimeScheduler scheduler = VirtualTimeScheduler.create();
    Duration gracePeriod = Duration.ofMillis(10);
    Flux<String> f = Flux.just("hello world").replay(1).refCount(1, gracePeriod, scheduler);
    AssertSubscriber<String> s = AssertSubscriber.create();
    f.subscribe(s);
    scheduler.advanceTimeBy(gracePeriod);
    StepVerifier.create(f.next()).expectNext("hello world").verifyComplete();
    scheduler.advanceTimeBy(gracePeriod);
    s.assertValueCount(1).assertNoError().assertComplete();
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Duration(java.time.Duration) Test(org.junit.Test)

Example 12 with VirtualTimeScheduler

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

the class FluxCacheTest method cacheFluxHistoryTTL.

@Test
public void cacheFluxHistoryTTL() {
    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(2, 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 13 with VirtualTimeScheduler

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

the class MonoDelayElementTest method cancelBeforeNext.

@Test
public void cancelBeforeNext() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    AtomicBoolean emitted = new AtomicBoolean();
    AtomicBoolean cancelled = new AtomicBoolean();
    Mono<Long> source = Mono.delay(Duration.ofMillis(1000), vts);
    StepVerifier.withVirtualTime(() -> new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, vts).doOnCancel(() -> cancelled.set(true)).doOnNext(n -> emitted.set(true)), () -> vts, Long.MAX_VALUE).expectSubscription().expectNoEvent(Duration.ofMillis(500)).thenCancel().verify();
    vts.advanceTimeBy(Duration.ofHours(1));
    assertThat(emitted.get()).isFalse();
    assertThat(cancelled.get()).isTrue();
}
Also used : TestPublisher(reactor.test.publisher.TestPublisher) StepVerifier(reactor.test.StepVerifier) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) Assertions.fail(org.assertj.core.api.Assertions.fail) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Schedulers(reactor.core.scheduler.Schedulers) Exceptions(reactor.core.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 14 with VirtualTimeScheduler

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

the class MonoDelayElementTest method monoApiTestMillisAndTimer.

@Test
public void monoApiTestMillisAndTimer() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    StepVerifier.withVirtualTime(() -> Mono.just("foo").delayElement(Duration.ofMillis(5000L), vts), () -> vts, Long.MAX_VALUE).expectSubscription().expectNoEvent(Duration.ofSeconds(5)).expectNext("foo").verifyComplete();
}
Also used : VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 15 with VirtualTimeScheduler

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

the class MonoDelayElementTest method cancelDuringDelay.

@Test
public void cancelDuringDelay() {
    VirtualTimeScheduler vts = VirtualTimeScheduler.create();
    AtomicBoolean emitted = new AtomicBoolean();
    AtomicBoolean cancelled = new AtomicBoolean();
    Mono<String> source = Mono.just("foo").log().hide();
    StepVerifier.withVirtualTime(() -> new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, vts).doOnCancel(() -> cancelled.set(true)).log().doOnNext(n -> emitted.set(true)), () -> vts, Long.MAX_VALUE).expectSubscription().expectNoEvent(Duration.ofSeconds(1)).thenCancel().verify();
    vts.advanceTimeBy(Duration.ofHours(1));
    assertThat(emitted.get()).isFalse();
    assertThat(cancelled.get()).isTrue();
}
Also used : TestPublisher(reactor.test.publisher.TestPublisher) StepVerifier(reactor.test.StepVerifier) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) Assertions.fail(org.assertj.core.api.Assertions.fail) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Schedulers(reactor.core.scheduler.Schedulers) Exceptions(reactor.core.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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