use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.
the class ScanWithPublisherTest method scanWithFinalizationOnCancelDifferentThreads.
@ParameterizedTest(name = "{displayName} [{index}] {arguments}")
@ValueSource(booleans = { true, false })
void scanWithFinalizationOnCancelDifferentThreads(final boolean interleaveCancellation) {
ExecutorService executorService = Executors.newFixedThreadPool(1);
try {
final AtomicInteger finalizations = new AtomicInteger(0);
PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
final CountDownLatch checkpoint = new CountDownLatch(1);
final CountDownLatch requested = new CountDownLatch(1);
final CountDownLatch resume = new CountDownLatch(1);
final CountDownLatch nextDelivered = new CountDownLatch(1);
final CountDownLatch nextDeliveredResume = new CountDownLatch(1);
final PublisherSource<Integer> source = toSource(scanWithOperator(fromSource(processor), true, new ScanWithLifetimeMapper<Integer, Integer>() {
@Override
public Integer mapOnNext(@Nullable final Integer next) {
return next;
}
@Override
public Integer mapOnError(final Throwable cause) throws Throwable {
throw cause;
}
@Override
public Integer mapOnComplete() {
return 5;
}
@Override
public boolean mapTerminal() {
if (interleaveCancellation) {
checkpoint.countDown();
try {
resume.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throwException(e);
}
}
return true;
}
@Override
public void afterFinally() {
finalizations.incrementAndGet();
}
}));
Future<Void> f = executorService.submit(() -> {
try {
TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
source.subscribe(subscriber);
Subscription s = subscriber.awaitSubscription();
s.request(2);
requested.countDown();
if (interleaveCancellation) {
checkpoint.await();
} else {
nextDelivered.await();
}
s.cancel();
if (!interleaveCancellation) {
nextDeliveredResume.countDown();
}
resume.countDown();
} catch (Exception e) {
throw new AssertionError(e);
}
return null;
});
requested.await();
processor.onNext(1);
if (!interleaveCancellation) {
nextDelivered.countDown();
nextDeliveredResume.await();
}
processor.onComplete();
f.get();
assertThat(finalizations.get(), is(1));
} catch (Throwable e) {
throw new AssertionError(e);
} finally {
executorService.shutdown();
}
}
use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.
the class SourceAdaptersTest method publisherFromSourceCancel.
@Test
void publisherFromSourceCancel() {
PublisherSource.Subscription srcSubscription = mock(PublisherSource.Subscription.class);
PublisherSource<Integer> source = s -> s.onSubscribe(srcSubscription);
fromSource(source).firstOrElse(() -> null).toFuture().cancel(true);
verify(srcSubscription).cancel();
}
use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.
the class ScanWithPublisherTest method scanWithLifetimeSignalReentry.
@Test
void scanWithLifetimeSignalReentry() throws InterruptedException {
AtomicInteger finalizations = new AtomicInteger();
CountDownLatch completed = new CountDownLatch(1);
PublisherSource<Integer> syncNoReentryProtectionSource = subscriber -> subscriber.onSubscribe(new Subscription() {
int count;
@Override
public void request(final long n) {
if (count == 2) {
subscriber.onComplete();
} else {
subscriber.onNext(count++);
}
}
@Override
public void cancel() {
}
});
toSource(fromSource(syncNoReentryProtectionSource).scanWithLifetime(() -> new ScanWithLifetimeMapper<Integer, Integer>() {
@Override
public void afterFinally() {
finalizations.incrementAndGet();
completed.countDown();
}
@Nullable
@Override
public Integer mapOnNext(@Nullable final Integer next) {
return next;
}
@Nullable
@Override
public Integer mapOnError(final Throwable cause) {
return null;
}
@Nullable
@Override
public Integer mapOnComplete() {
return null;
}
@Override
public boolean mapTerminal() {
return false;
}
})).subscribe(new PublisherSource.Subscriber<Integer>() {
Subscription subscription;
@Override
public void onSubscribe(final Subscription subscription) {
this.subscription = subscription;
subscription.request(1);
}
@Override
public void onNext(@Nullable final Integer integer) {
subscription.request(1);
}
@Override
public void onError(final Throwable t) {
}
@Override
public void onComplete() {
}
});
completed.await();
assertThat(finalizations.get(), is(1));
}
use of io.servicetalk.concurrent.PublisherSource in project servicetalk by apple.
the class SourceAdaptersTest method publisherFromSourceError.
@Test
void publisherFromSourceError() {
PublisherSource<Integer> src = s -> {
s.onSubscribe(EMPTY_SUBSCRIPTION);
s.onError(DELIBERATE_EXCEPTION);
};
Future<Integer> future = fromSource(src).firstOrElse(() -> null).toFuture();
Exception e = assertThrows(ExecutionException.class, () -> future.get());
assertThat(e.getCause(), sameInstance(DELIBERATE_EXCEPTION));
}
Aggregations