use of reactor.core.Disposable in project reactor-core by reactor.
the class AbstractSchedulerTest method workerScheduleAndDispose.
@Test
@Timeout(10)
public final void workerScheduleAndDispose() throws Exception {
Scheduler s = schedulerNotCached();
try {
Scheduler.Worker w = s.createWorker();
assertThat(w.isDisposed()).isFalse();
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch latch2 = shouldCheckDisposeTask() ? new CountDownLatch(1) : null;
try {
Disposable d = w.schedule(() -> {
try {
latch.countDown();
if (latch2 != null && !latch2.await(10, TimeUnit.SECONDS) && shouldCheckInterrupted()) {
fail("Should have interrupted");
}
} catch (InterruptedException e) {
}
});
latch.await();
if (shouldCheckDisposeTask()) {
assertThat(d.isDisposed()).isFalse();
}
d.dispose();
// noop
d.dispose();
} catch (Throwable schedulingError) {
fail("unexpected scheduling error", schedulingError);
}
Thread.yield();
if (latch2 != null) {
latch2.countDown();
}
Disposable[] massCancel;
boolean hasErrors = false;
if (shouldCheckMassWorkerDispose()) {
int n = 10;
massCancel = new Disposable[n];
Throwable[] errors = new Throwable[n];
Thread current = Thread.currentThread();
for (int i = 0; i < n; i++) {
try {
massCancel[i] = w.schedule(() -> {
if (current == Thread.currentThread()) {
return;
}
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
}
});
} catch (RejectedExecutionException ree) {
errors[i] = ree;
hasErrors = true;
}
}
} else {
massCancel = null;
}
w.dispose();
// noop
w.dispose();
assertThat(w.isDisposed()).isTrue();
if (massCancel != null) {
assertThat(hasErrors).as("mass cancellation errors").isFalse();
for (Disposable _d : massCancel) {
assertThat(_d.isDisposed()).isTrue();
}
}
assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> w.schedule(() -> {
})).isSameAs(Exceptions.failWithRejected());
} finally {
s.dispose();
// noop
s.dispose();
}
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class ElasticSchedulerTest method doesntRecycleWhileRunningAfterDisposed.
@Test
public void doesntRecycleWhileRunningAfterDisposed() throws Exception {
Scheduler s = Schedulers.newElastic("test-recycle");
((ElasticScheduler) s).evictor.shutdownNow();
try {
AtomicBoolean stop = new AtomicBoolean(false);
CountDownLatch started = new CountDownLatch(1);
Disposable d = s.schedule(() -> {
started.countDown();
// simulate uninterruptible computation
for (; ; ) {
if (stop.get()) {
break;
}
}
});
assertThat(started.await(10, TimeUnit.SECONDS)).as("latch timeout").isTrue();
d.dispose();
Thread.sleep(100);
assertThat(((ElasticScheduler) s).cache).isEmpty();
stop.set(true);
Thread.sleep(100);
assertThat(((ElasticScheduler) s).cache.size()).isEqualTo(1);
} finally {
s.dispose();
}
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class ElasticSchedulerTest method eviction.
@Test
@Timeout(10)
public void eviction() throws Exception {
Scheduler s = Schedulers.newElastic("test-recycle", 2);
((ElasticScheduler) s).evictor.shutdownNow();
try {
for (int i = 0; i < 100; i++) {
Disposable d = s.schedule(() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
d.dispose();
}
while (((ElasticScheduler) s).cache.peek() != null) {
((ElasticScheduler) s).eviction();
Thread.sleep(100);
}
assertThat(((ElasticScheduler) s).all).isEmpty();
} finally {
s.dispose();
// noop
s.dispose();
}
assertThat(((ElasticScheduler) s).cache).isEmpty();
assertThat(s.isDisposed()).isTrue();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class GuideTests method errorHandlingUsing.
@Test
public void errorHandlingUsing() {
AtomicBoolean isDisposed = new AtomicBoolean();
Disposable disposableInstance = new Disposable() {
@Override
public void dispose() {
// <4>
isDisposed.set(true);
}
@Override
public String toString() {
return "DISPOSABLE";
}
};
Flux<String> flux = Flux.using(// <1>
() -> disposableInstance, // <2>
disposable -> Flux.just(disposable.toString()), // <3>
Disposable::dispose);
StepVerifier.create(flux).expectNext("DISPOSABLE").verifyComplete();
assertThat(isDisposed.get()).isTrue();
}
use of reactor.core.Disposable in project reactor-core by reactor.
the class EmitterProcessorTest method state.
@Test
public void state() {
EmitterProcessor<Integer> tp = EmitterProcessor.create();
assertThat(tp.getPending()).isEqualTo(0);
assertThat(tp.getBufferSize()).isEqualTo(Queues.SMALL_BUFFER_SIZE);
assertThat(tp.isCancelled()).isFalse();
assertThat(tp.inners()).isEmpty();
assertThat(tp.currentSubscriberCount()).as("has subscriber").isZero();
Disposable d1 = tp.subscribe();
assertThat(tp.inners()).hasSize(1);
assertThat(tp.currentSubscriberCount()).isPositive();
FluxSink<Integer> s = tp.sink();
s.next(2);
s.next(3);
s.next(4);
assertThat(tp.getPending()).isEqualTo(0);
AtomicReference<Subscription> d2 = new AtomicReference<>();
tp.subscribe(new CoreSubscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
d2.set(s);
}
@Override
public void onNext(Integer integer) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
s.next(5);
s.next(6);
s.next(7);
assertThat(tp.scan(BUFFERED)).isEqualTo(3);
assertThat(tp.isTerminated()).isFalse();
s.complete();
assertThat(tp.isTerminated()).isFalse();
d1.dispose();
d2.get().cancel();
assertThat(tp.isTerminated()).isTrue();
StepVerifier.create(tp).verifyComplete();
// noop
tp.onNext(8);
EmitterProcessor<Void> empty = EmitterProcessor.create();
empty.onComplete();
assertThat(empty.isTerminated()).isTrue();
}
Aggregations