use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class CacheSingleTest method threeSubscribersOneLateQueueData.
@ParameterizedTest
@CsvSource(value = { "true,", "false,1", "false," })
void threeSubscribersOneLateQueueData(boolean onError, @Nullable Integer value) {
Single<Integer> single = source.cache(2);
toSource(single).subscribe(subscriber1);
toSource(single).subscribe(subscriber2);
Cancellable localSubscription1 = subscriber1.awaitSubscription();
Cancellable localSubscription2 = subscriber2.awaitSubscription();
if (onError) {
source.onError(DELIBERATE_EXCEPTION);
assertThat(subscriber1.awaitOnError(), is(DELIBERATE_EXCEPTION));
assertThat(subscriber2.awaitOnError(), is(DELIBERATE_EXCEPTION));
} else {
source.onSuccess(value);
assertThat(subscriber1.awaitOnSuccess(), is(value));
assertThat(subscriber2.awaitOnSuccess(), is(value));
}
toSource(single).subscribe(subscriber3);
Cancellable localSubscription3 = subscriber3.awaitSubscription();
if (onError) {
assertThat(subscriber3.awaitOnError(), is(DELIBERATE_EXCEPTION));
} else {
assertThat(subscriber3.awaitOnSuccess(), is(value));
}
// cancel after terminal shouldn't impact anything.
localSubscription1.cancel();
localSubscription2.cancel();
localSubscription3.cancel();
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class CacheSingleTest method singleSubscriberCancel.
@ParameterizedTest
@ValueSource(booleans = { true, false })
void singleSubscriberCancel(boolean cancelUpstream) throws InterruptedException {
toSource(source.cache(1, cancelUpstream)).subscribe(subscriber1);
Cancellable subscription1 = subscriber1.awaitSubscription();
subscription1.cancel();
// multiple cancels should be safe.
subscription1.cancel();
if (cancelUpstream) {
cancellable.awaitCancelled();
} else {
assertThat(cancellable.isCancelled(), is(false));
}
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class CancellableSetTest method testAddAndRemove.
@Test
void testAddAndRemove() {
CancellableSet c = newCompositeCancellable();
Cancellable cancellable = mock(Cancellable.class);
add(c, cancellable);
c.remove(cancellable);
c.cancel();
verifyNoInteractions(cancellable);
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class CancellableSetTest method duplicateAddDoesNotCancel.
@Test
void duplicateAddDoesNotCancel() {
CancellableSet c = newCompositeCancellable();
Cancellable cancellable = mock(Cancellable.class);
int addCount = 0;
if (add(c, cancellable)) {
++addCount;
}
if (add(c, cancellable)) {
++addCount;
}
verify(cancellable, never()).cancel();
for (int i = 0; i < addCount; ++i) {
assertTrue(c.remove(cancellable));
}
c.cancel();
verify(cancellable, never()).cancel();
}
use of io.servicetalk.concurrent.Cancellable in project servicetalk by apple.
the class ClosableConcurrentStackTest method concurrentClosePushRemove.
@Test
void concurrentClosePushRemove() throws Exception {
ClosableConcurrentStack<Cancellable> stack = new ClosableConcurrentStack<>();
final int itemCount = 1000;
CyclicBarrier barrier = new CyclicBarrier(itemCount + 1);
List<Single<Cancellable>> completableList = new ArrayList<>(itemCount);
for (int i = 0; i < itemCount; ++i) {
final int finalI = i;
completableList.add(EXECUTOR_RULE.executor().submit(() -> {
Cancellable c = mock(Cancellable.class);
try {
barrier.await();
} catch (Exception e) {
throw new AssertionError(e);
}
assertTrue(stack.push(c), () -> "failed for index: " + finalI);
return c;
}));
}
Future<Collection<Cancellable>> future = collectUnordered(completableList, itemCount).toFuture();
barrier.await();
Collection<Cancellable> cancellables = future.get();
stack.close(Cancellable::cancel);
assertFalse(stack.push(() -> {
}));
for (Cancellable c : cancellables) {
verify(c).cancel();
}
}
Aggregations