use of org.reactivestreams.Processor 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 org.reactivestreams.Processor in project reactor-netty by reactor.
the class TcpServerTests method test5.
@Test
@Ignore
public void test5() throws Exception {
// Hot stream of data, could be injected from anywhere
EmitterProcessor<String> broadcaster = EmitterProcessor.create();
// Get a reference to the tail of the operation pipeline (microbatching + partitioning)
final Processor<List<String>, List<String>> processor = WorkQueueProcessor.<List<String>>builder().autoCancel(false).build();
broadcaster.bufferTimeout(10, Duration.ofSeconds(1)).log("broadcaster").subscribe(processor);
// on a server dispatching data on the default shared dispatcher, and serializing/deserializing as string
// Listen for anything exactly hitting the root URI and route the incoming connection request to the callback
NettyContext s = HttpServer.create(0).newRouter(r -> r.get("/", (request, response) -> {
// prepare a response header to be appended first before any reply
response.addHeader("X-CUSTOM", "12345");
// returning a stream of String from each microbatch merged
return response.sendString(Flux.from(processor).flatMap(Flux::fromIterable).take(Duration.ofSeconds(5)).concatWith(Flux.just("end\n")));
})).block(Duration.ofSeconds(30));
for (int i = 0; i < 50; i++) {
Thread.sleep(500);
broadcaster.onNext(System.currentTimeMillis() + "\n");
}
s.dispose();
}
Aggregations