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);
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations