use of reactor.core.Disposable in project reactor-core by reactor.
the class FluxReplayTest method cancel.
@Test
public void cancel() {
ConnectableFlux<Integer> replay = Sinks.many().unicast().<Integer>onBackpressureBuffer().asFlux().replay(2);
replay.subscribe(v -> {
}, e -> {
throw Exceptions.propagate(e);
});
Disposable connected = replay.connect();
// the lambda subscriber itself is cancelled so it will bubble the exception
// propagated by connect().dispose()
assertThatExceptionOfType(RuntimeException.class).isThrownBy(connected::dispose).withMessage("Disconnected");
boolean cancelled = (((FluxReplay.ReplaySubscriber) connected).state & FluxReplay.ReplaySubscriber.DISPOSED_FLAG) == FluxReplay.ReplaySubscriber.DISPOSED_FLAG;
assertThat(cancelled).isTrue();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenDirectTaskIsExecuted.
@Test
public void estimateRemainingTaskCapacityResetWhenDirectTaskIsExecuted() 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) {
e.printStackTrace();
}
});
// 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();
latch.countDown();
Awaitility.await().untilTrue(taskRan);
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after run").isOne();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method taskPutInPendingQueueIsEventuallyExecuted.
@Test
public void taskPutInPendingQueueIsEventuallyExecuted() throws InterruptedException {
BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
boundedElasticScheduler.start();
Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
CountDownLatch latch = new CountDownLatch(1);
// enqueue blocking task on worker
worker.schedule(() -> {
try {
latch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// small window to start the first task
Thread.sleep(10);
AtomicBoolean ranSecond = new AtomicBoolean();
Disposable task = worker.schedule(() -> ranSecond.set(true));
assertThat(ranSecond).as("is pending execution").isFalse();
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("queue full").isZero();
latch.countDown();
Awaitility.await().atMost(100, TimeUnit.MILLISECONDS).pollInterval(10, TimeUnit.MILLISECONDS).pollDelay(10, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(ranSecond).as("did run after task1 done").isTrue());
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity restored").isOne();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenWorkerTaskIsExecuted.
@Test
public void estimateRemainingTaskCapacityResetWhenWorkerTaskIsExecuted() throws InterruptedException {
BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
boundedElasticScheduler.start();
Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean taskRan = new AtomicBoolean();
// occupy the scheduler
worker.schedule(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// small window to start the first task
Thread.sleep(10);
// enqueue task on worker
Disposable task = worker.schedule(() -> taskRan.set(true));
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity when running").isZero();
latch.countDown();
Awaitility.await().untilTrue(taskRan);
assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after run").isOne();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenWorkerTaskIsDisposed.
@Test
public void estimateRemainingTaskCapacityResetWhenWorkerTaskIsDisposed() throws InterruptedException {
BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
boundedElasticScheduler.start();
Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean taskRan = new AtomicBoolean();
// occupy the scheduler
worker.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 = worker.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());
}
Aggregations