use of reactor.core.Disposable in project reactor-core by reactor.
the class FluxTests method cancelOn.
@Test
public void cancelOn() throws Exception {
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicReference<Thread> thread = new AtomicReference<>();
Disposable res = Flux.never().doOnCancel(() -> {
thread.set(Thread.currentThread());
countDownLatch.countDown();
}).cancelOn(asyncGroup).subscribe();
res.dispose();
assertThat(countDownLatch.await(3, TimeUnit.SECONDS)).isTrue();
assertThat(thread.get()).isNotSameAs(Thread.currentThread());
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenDirectTaskIsDisposed.
@Test
public void estimateRemainingTaskCapacityResetWhenDirectTaskIsDisposed() throws InterruptedException {
BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
boundedElasticScheduler.start();
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean taskRan = new AtomicBoolean();
// occupy the scheduler
boundedElasticScheduler.schedule(() -> {
try {
latch.await();
} catch (InterruptedException e) {
// expected to be interrupted
}
});
// small window to start the first task
Thread.sleep(10);
// enqueue task on worker
Disposable task = boundedElasticScheduler.schedule(() -> taskRan.set(true));
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity when running").isZero();
task.dispose();
Awaitility.with().pollDelay(50, TimeUnit.MILLISECONDS).await().atMost(100, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after dispose").isOne());
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class FluxTests method endLessTimer.
/**
* This test case demonstrates a silent failure of {@link Flux#interval(Duration)}
* when a resolution is specified that
* is less than the backing {@link Timer} class.
*
* @throws InterruptedException - on failure.
* @throws TimeoutException - on failure. <p> by @masterav10 : https://github.com/reactor/reactor/issues/469
*/
@Test
@Disabled
public void endLessTimer() throws InterruptedException, TimeoutException {
int tasks = 50;
// XXX: Fails when less than 100
long delayMS = 50;
Phaser barrier = new Phaser(tasks + 1);
List<Long> times = new ArrayList<>();
// long localTime = System.currentTimeMillis(); for java 7
long localTime = Instant.now().toEpochMilli();
long elapsed = System.nanoTime();
Disposable ctrl = Flux.interval(Duration.ofMillis(delayMS)).log("test").map((signal) -> {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - elapsed);
}).doOnNext((elapsedMillis) -> {
times.add(localTime + elapsedMillis);
barrier.arrive();
}).subscribe();
barrier.awaitAdvanceInterruptibly(barrier.arrive(), tasks * delayMS + 1000, TimeUnit.MILLISECONDS);
ctrl.dispose();
assertThat(times.size()).isEqualTo(tasks);
for (int i = 1; i < times.size(); i++) {
Long prev = times.get(i - 1);
Long time = times.get(i);
assertThat(prev).isGreaterThan(0L);
assertThat(time).isGreaterThan(0L);
assertThat(time - prev).isLessThanOrEqualTo((long) (delayMS * 1.2));
}
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class FluxTests method testBeyondLongMaxMicroBatching.
@Test
public void testBeyondLongMaxMicroBatching() throws InterruptedException {
List<Integer> tasks = IntStream.range(0, 1500).boxed().collect(Collectors.toList());
CountDownLatch countDownLatch = new CountDownLatch(tasks.size());
Flux<Integer> worker = Flux.fromIterable(tasks).log("before", Level.FINE).publishOn(asyncGroup);
/*Disposable tail = */
worker.log("after", Level.FINE).parallel(2).groups().subscribe(s -> s.log("w" + s.key(), Level.FINE).publishOn(asyncGroup).map(v -> v).subscribe(v -> countDownLatch.countDown(), Throwable::printStackTrace));
countDownLatch.await(5, TimeUnit.SECONDS);
assertThat(countDownLatch.getCount()).as("Count max: %d", tasks.size()).isEqualTo(0);
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class PopularTagTests method sampleTest.
@Test
public void sampleTest() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Disposable top10every1second = Flux.fromIterable(PULP_SAMPLE).publishOn(asyncGroup).flatMap(samuelJackson -> Flux.fromArray(samuelJackson.split(" ")).publishOn(asyncGroup).filter(w -> !w.trim().isEmpty()).doOnNext(i -> simulateLatency())).window(Duration.ofSeconds(2)).flatMap(s -> s.groupBy(w -> w).flatMap(w -> w.count().map(c -> Tuples.of(w.key(), c))).collectSortedList((a, b) -> -a.getT2().compareTo(b.getT2())).flatMapMany(Flux::fromIterable).take(10).doAfterTerminate(() -> LOG.info("------------------------ window terminated" + "----------------------"))).subscribe(entry -> LOG.info(entry.getT1() + ": " + entry.getT2()), error -> LOG.error("", error), latch::countDown);
awaitLatch(top10every1second, latch);
}
Aggregations