use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class EmitterProcessorTest method testThreadAffinity.
@Test
public void testThreadAffinity() throws InterruptedException {
int count = 10;
Scheduler[] schedulers = new Scheduler[4];
CountDownLatch[] latches = new CountDownLatch[schedulers.length];
for (int i = 0; i < schedulers.length; i++) {
schedulers[i] = Schedulers.newSingle("scheduler" + i + '-');
int expectedCount = i == 1 ? count * 2 : count;
latches[i] = new CountDownLatch(expectedCount);
}
EmitterProcessor<Integer> processor = EmitterProcessor.create();
processor.publishOn(schedulers[0]).share();
processor.publishOn(schedulers[1]).subscribe(i -> {
assertThat(Thread.currentThread().getName().contains("scheduler1")).isTrue();
latches[1].countDown();
});
for (int i = 0; i < count; i++) processor.onNext(i);
processor.publishOn(schedulers[2]).map(i -> {
assertThat(Thread.currentThread().getName().contains("scheduler2")).isTrue();
latches[2].countDown();
return i;
}).publishOn(schedulers[3]).doOnNext(i -> {
assertThat(Thread.currentThread().getName().contains("scheduler3")).isTrue();
latches[3].countDown();
}).subscribe();
for (int i = 0; i < count; i++) processor.onNext(i);
processor.onComplete();
for (int i = 1; i < latches.length; i++) assertThat(latches[i].await(5, TimeUnit.SECONDS)).isTrue();
assertThat(latches[0].getCount()).isEqualTo(count);
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class StepVerifierTests method assertNextWithSubscribeOnDirectProcessor.
// see https://github.com/reactor/reactor-core/issues/959
@Test
public void assertNextWithSubscribeOnDirectProcessor() {
Scheduler scheduler = Schedulers.newElastic("test");
DirectProcessor<Integer> processor = DirectProcessor.create();
Mono<Integer> doAction = Mono.fromSupplier(() -> 22).doOnNext(processor::onNext).subscribeOn(scheduler);
assertThatExceptionOfType(AssertionError.class).isThrownBy(StepVerifier.create(processor).then(doAction::subscribe).assertNext(v -> assertThat(v).isEqualTo(23)).thenCancel()::verify);
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class TopicProcessorTest method testBufferSize1Shared.
// see https://github.com/reactor/reactor-core/issues/445
@Test(timeout = 5_000)
public void testBufferSize1Shared() throws Exception {
TopicProcessor<String> broadcast = TopicProcessor.<String>builder().name("share-name").bufferSize(1).autoCancel(true).share(true).build();
int simultaneousSubscribers = 3000;
CountDownLatch latch = new CountDownLatch(simultaneousSubscribers);
Scheduler scheduler = Schedulers.single();
FluxSink<String> sink = broadcast.sink();
Flux<String> flux = broadcast.filter(Objects::nonNull).publishOn(scheduler).cache(1);
for (int i = 0; i < simultaneousSubscribers; i++) {
flux.subscribe(s -> latch.countDown());
}
sink.next("data");
assertThat(latch.await(4, TimeUnit.SECONDS)).overridingErrorMessage("Data not received").isTrue();
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class TopicProcessorTest method testBufferSize1Created.
// see https://github.com/reactor/reactor-core/issues/445
@Test(timeout = 5_000)
public void testBufferSize1Created() throws Exception {
TopicProcessor<String> broadcast = TopicProcessor.<String>builder().name("share-name").bufferSize(1).autoCancel(true).build();
int simultaneousSubscribers = 3000;
CountDownLatch latch = new CountDownLatch(simultaneousSubscribers);
Scheduler scheduler = Schedulers.single();
FluxSink<String> sink = broadcast.sink();
Flux<String> flux = broadcast.filter(Objects::nonNull).publishOn(scheduler).cache(1);
for (int i = 0; i < simultaneousSubscribers; i++) {
flux.subscribe(s -> latch.countDown());
}
sink.next("data");
assertThat(latch.await(4, TimeUnit.SECONDS)).overridingErrorMessage("Data not received").isTrue();
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class FluxTests method consistentMultithreadingWithPartition.
@Test
public void consistentMultithreadingWithPartition() throws InterruptedException {
Scheduler supplier1 = Schedulers.newParallel("groupByPool", 2);
Scheduler supplier2 = Schedulers.newParallel("partitionPool", 5);
CountDownLatch latch = new CountDownLatch(10);
/*Disposable c = */
Flux.range(1, 10).groupBy(n -> n % 2 == 0).flatMap(stream -> stream.publishOn(supplier1).log("groupBy-" + stream.key())).parallel(5).runOn(supplier2).sequential().publishOn(asyncGroup).log("join").subscribe(t -> {
latch.countDown();
});
latch.await(30, TimeUnit.SECONDS);
assertThat("Not totally dispatched: " + latch.getCount(), latch.getCount() == 0);
supplier1.dispose();
supplier2.dispose();
}
Aggregations