use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class EmitterProcessorTest method test.
@Test
@Ignore
public void test() {
Scheduler asyncGroup = Schedulers.single();
FluxProcessor<String, String> emitter = EmitterProcessor.create();
CountDownLatch requestReceived = new CountDownLatch(1);
AtomicLong demand = new AtomicLong(0);
Publisher<String> publisher = s -> s.onSubscribe(new Subscription() {
@Override
public void request(long n) {
System.out.println("request: " + n + " " + s);
demand.addAndGet(n);
requestReceived.countDown();
}
@Override
public void cancel() {
System.out.println("cancel");
}
});
Flux.from(publisher).subscribeOn(asyncGroup).subscribe(emitter);
AssertSubscriber<String> subscriber = AssertSubscriber.create();
emitter.subscribe(subscriber);
int i = 0;
for (; ; ) {
if (getAndSub(demand, 1) != 0) {
emitter.onNext("" + (i++));
} else {
System.out.println("NO REQUESTED: " + emitter);
LockSupport.parkNanos(100_000_000);
}
}
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class FluxProcessorTest method testSubmitSession.
@Test
public void testSubmitSession() throws Exception {
FluxProcessor<Integer, Integer> processor = EmitterProcessor.create();
AtomicInteger count = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(1);
Scheduler scheduler = Schedulers.parallel();
processor.publishOn(scheduler).delaySubscription(Duration.ofMillis(1000)).limitRate(1).subscribe(d -> {
count.incrementAndGet();
latch.countDown();
});
FluxSink<Integer> session = processor.sink();
session.next(1);
// System.out.println(emission);
session.complete();
latch.await(5, TimeUnit.SECONDS);
Assert.assertTrue("latch : " + count, count.get() == 1);
scheduler.dispose();
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class FluxProcessorTest method testEmitter2.
@Test
public void testEmitter2() throws Throwable {
FluxProcessor<Integer, Integer> processor = EmitterProcessor.create();
int n = 100_000;
int subs = 4;
final CountDownLatch latch = new CountDownLatch((n + 1) * subs);
Scheduler c = Schedulers.single();
for (int i = 0; i < subs; i++) {
processor.publishOn(c).doOnComplete(latch::countDown).doOnNext(d -> latch.countDown()).subscribe();
}
FluxSink<Integer> session = processor.sink();
for (int i = 0; i < n; i++) {
while (session.requestedFromDownstream() == 0) {
}
session.next(i);
}
session.complete();
boolean waited = latch.await(5, TimeUnit.SECONDS);
Assert.assertTrue("latch : " + latch.getCount(), waited);
c.dispose();
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class FluxWindowTimeoutTest method scanMainSubscriber.
@Test
public void scanMainSubscriber() {
Scheduler scheduler = new MyScheduler();
CoreSubscriber<Flux<Integer>> actual = new LambdaSubscriber<>(null, e -> {
}, null, null);
FluxWindowTimeout.WindowTimeoutSubscriber<Integer> test = new FluxWindowTimeout.WindowTimeoutSubscriber<>(actual, 123, Long.MAX_VALUE, scheduler);
Subscription parent = Operators.emptySubscription();
test.onSubscribe(parent);
Assertions.assertThat(test.scan(Scannable.Attr.RUN_ON)).isSameAs(scheduler.createWorker());
Assertions.assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
Assertions.assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);
Assertions.assertThat(test.scan(Scannable.Attr.CAPACITY)).isEqualTo(123);
test.requested = 35;
Assertions.assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(35);
Assertions.assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(0);
test.onNext(1);
test.onNext(2);
Assertions.assertThat(test.inners().findFirst().get().scan(Scannable.Attr.BUFFERED)).isEqualTo(2);
Assertions.assertThat(test.inners().findFirst().get().scan(Scannable.Attr.CANCELLED)).isEqualTo(false);
Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
test.onComplete();
Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
test.cancel();
Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.
the class FluxSubscribeOnTest method gh507.
@Test
public void gh507() {
Scheduler s = Schedulers.newSingle("subscribe");
Scheduler s2 = Schedulers.newParallel("receive");
AtomicBoolean interrupted = new AtomicBoolean();
AtomicBoolean timedOut = new AtomicBoolean();
try {
Flux.from((Publisher<String>) subscriber -> {
subscriber.onSubscribe(new Subscription() {
private int totalCount;
@Override
public void request(long n) {
for (int i = 0; i < n; i++) {
if (totalCount++ < 317) {
subscriber.onNext(String.valueOf(totalCount));
} else {
subscriber.onComplete();
}
}
}
@Override
public void cancel() {
}
});
}).subscribeOn(s).limitRate(10).doOnNext(d -> {
CountDownLatch latch = new CountDownLatch(1);
Mono.fromCallable(() -> d).subscribeOn(s2).doFinally(it -> latch.countDown()).subscribe();
try {
if (!latch.await(5, TimeUnit.SECONDS)) {
timedOut.set(true);
}
} catch (InterruptedException e) {
interrupted.set(true);
}
}).blockLast(Duration.ofSeconds(2));
assertThat(interrupted).as("interrupted").isFalse();
assertThat(timedOut).as("latch timeout").isFalse();
} finally {
s.dispose();
s2.dispose();
}
}
Aggregations