use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class CompositeCancellable method create.
/**
* Creates new instance of {@link Cancellable} which is a composite of {@code toCompose}.
*
* @param toCompose All {@link Cancellable} to compose.
* @return A composite {@link Cancellable} for all {@code toCompose}.
*/
static Cancellable create(Cancellable... toCompose) {
switch(toCompose.length) {
case 0:
throw new IllegalArgumentException("At least one Cancellable required to compose.");
case 1:
return requireNonNull(toCompose[0]);
case 2:
Cancellable first = requireNonNull(toCompose[0]);
Cancellable second = requireNonNull(toCompose[1]);
return () -> {
try {
first.cancel();
} finally {
second.cancel();
}
};
default:
return new CompositeCancellable(toCompose);
}
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class RunnableCompletableTest method cancelInterrupts.
@Test
void cancelInterrupts() throws Exception {
final Completable source = Completable.fromRunnable(factory);
final CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
try {
// await till interrupted.
latch.await();
} catch (InterruptedException e) {
latch.countDown();
Thread.currentThread().interrupt();
throw e;
}
return 1;
}).when(factory).run();
toSource(source).subscribe(new CompletableSource.Subscriber() {
@Override
public void onSubscribe(final Cancellable cancellable) {
cancellable.cancel();
}
@Override
public void onComplete() {
// noop
}
@Override
public void onError(final Throwable t) {
// noop
}
});
latch.await();
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class TimeoutCompletableTest method executorScheduleThrows.
@Test
void executorScheduleThrows() {
toSource(source.ignoreElement().timeout(1, NANOSECONDS, new DelegatingExecutor(testExecutor) {
@Override
public Cancellable schedule(final Runnable task, final long delay, final TimeUnit unit) {
throw DELIBERATE_EXCEPTION;
}
})).subscribe(listener);
assertThat(listener.awaitOnError(), is(DELIBERATE_EXCEPTION));
TestCancellable cancellable = new TestCancellable();
source.onSubscribe(cancellable);
assertTrue(cancellable.isCancelled());
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class ShareContextOnSubscribeTest method awaitTermination.
private void awaitTermination(Completable completable) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
// completable.toFuture() will use the toSingle() conversion and share context operator will not be effective
// since it isn't the last operator. So we directly subscribe to the completable.
toSource(completable).subscribe(new CompletableSource.Subscriber() {
@Override
public void onSubscribe(final Cancellable cancellable) {
// Noop
}
@Override
public void onComplete() {
latch.countDown();
}
@Override
public void onError(final Throwable t) {
latch.countDown();
}
});
latch.await();
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class SingleZipWithTest method cancelAfterCompleteOutOfOrder.
@Test
void cancelAfterCompleteOutOfOrder() throws InterruptedException {
TestCancellable cancellable = new TestCancellable();
TestSingle<Integer> first = new TestSingle.Builder<Integer>().disableAutoOnSubscribe().build(subscriber1 -> {
subscriber1.onSubscribe(cancellable);
return subscriber1;
});
toSource(first.zipWith(second, SingleZipWithTest::combine)).subscribe(subscriber);
Cancellable c = subscriber.awaitSubscription();
second.onSuccess(10.1);
c.cancel();
cancellable.awaitCancelled();
}
Aggregations