use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class CompletableToPublisherTest method subscribeOnOriginalIsPreserved.
@Test
void subscribeOnOriginalIsPreserved() throws Exception {
final Thread testThread = currentThread();
final CountDownLatch analyzed = new CountDownLatch(1);
ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
TestCompletable completable = new TestCompletable.Builder().disableAutoOnSubscribe().build();
TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
toSource(completable.beforeCancel(() -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked cancel. Thread: " + currentThread()));
}
}).afterCancel(analyzed::countDown).subscribeOn(EXEC.executor()).<String>toPublisher()).subscribe(subscriber);
TestCancellable cancellable = new TestCancellable();
// waits till subscribed.
completable.onSubscribe(cancellable);
assertThat("Completable not subscribed.", completable.isSubscribed(), is(true));
subscriber.awaitSubscription().cancel();
analyzed.await();
assertThat("Completable did not get a cancel.", cancellable.isCancelled(), is(true));
assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class CompletableToPublisherTest method publishOnOriginalIsPreservedOnError.
@Test
void publishOnOriginalIsPreservedOnError() throws Exception {
ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
TestCompletable completable = new TestCompletable();
CountDownLatch analyzed = publishOnOriginalIsPreserved0(errors, subscriber, completable);
completable.onError(DELIBERATE_EXCEPTION);
analyzed.await();
assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
Throwable err = subscriber.awaitOnError();
assertThat("Wrong error received.", err, is(sameInstance(DELIBERATE_EXCEPTION)));
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class SingleToPublisherTest method subscribeOnOriginalIsPreserved.
@Test
void subscribeOnOriginalIsPreserved() throws Exception {
final Thread testThread = currentThread();
final CountDownLatch analyzed = new CountDownLatch(1);
ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
TestSingle<String> single = new TestSingle.Builder<String>().disableAutoOnSubscribe().build();
TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
toSource(single.beforeCancel(() -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked cancel. Thread: " + currentThread()));
}
}).afterCancel(analyzed::countDown).subscribeOn(EXEC.executor()).toPublisher()).subscribe(subscriber);
TestCancellable cancellable = new TestCancellable();
// waits till subscribed.
single.onSubscribe(cancellable);
assertThat("Single not subscribed.", single.isSubscribed(), is(true));
subscriber.awaitSubscription().cancel();
analyzed.await();
assertThat("Single did not get a cancel.", cancellable.isCancelled(), is(true));
assertNoAsyncErrors(errors);
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class PublisherBufferConcurrencyTest method addingAndBoundaryEmission.
@Test
void addingAndBoundaryEmission() throws Exception {
TestPublisher<Integer> original = new TestPublisher<>();
TestPublisher<Accumulator<Integer, Integer>> boundaries = new TestPublisher<>();
TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
CountDownLatch waitForBoundary = new CountDownLatch(1);
CountDownLatch waitForAdd = new CountDownLatch(1);
Accumulator<Integer, Integer> accumulator = new Accumulator<Integer, Integer>() {
private int added;
@Override
public void accumulate(@Nullable final Integer integer) {
waitForAdd.countDown();
try {
waitForBoundary.await();
if (integer == null) {
return;
}
added = integer;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throwException(e);
}
}
@Override
public Integer finish() {
return added;
}
};
toSource(original.buffer(new BufferStrategy<Integer, Accumulator<Integer, Integer>, Integer>() {
@Override
public Publisher<Accumulator<Integer, Integer>> boundaries() {
return boundaries;
}
@Override
public int bufferSizeHint() {
return 8;
}
})).subscribe(subscriber);
subscriber.awaitSubscription().request(1);
// initial boundary
boundaries.onNext(accumulator);
assertThat(subscriber.pollOnNext(10, MILLISECONDS), is(nullValue()));
CountDownLatch waitForOnNextReturn = new CountDownLatch(1);
EXEC.executor().submit(() -> original.onNext(1)).beforeFinally(waitForOnNextReturn::countDown).subscribe();
waitForAdd.await();
subscriber.awaitSubscription().request(1);
boundaries.onNext(new SummingAccumulator());
waitForBoundary.countDown();
waitForOnNextReturn.await();
// Last accumulator will be overwritten by add()
boundaries.onNext(new SummingAccumulator());
assertThat("Unexpected result.", subscriber.takeOnNext(), is(1));
original.onComplete();
// Boundary has to complete for terminal to be emitted
boundaries.onNext(new SummingAccumulator());
// empty accumulator
assertThat("Unexpected result.", subscriber.takeOnNext(), is(0));
subscriber.awaitOnComplete();
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class NettyChannelPublisherTest method testCancelThenResubscribeDeliversErrorAndNotQueuedData.
private void testCancelThenResubscribeDeliversErrorAndNotQueuedData(boolean doChannelRead, boolean setupFireReadOnClose) throws Exception {
if (setupFireReadOnClose) {
setupFireReadOnCloseEvents();
}
TestPublisherSubscriber<Integer> subscriber1 = new TestPublisherSubscriber<>();
TestPublisherSubscriber<Integer> subscriber2 = new TestPublisherSubscriber<>();
toSource(publisher).subscribe(subscriber1);
Subscription subscription1 = subscriber1.awaitSubscription();
subscription1.request(1);
assertFalse(channel.writeInbound(1));
Integer next = subscriber1.takeOnNext();
assertThat(next, is(1));
// this write should be queued, because there isn't any requestN demand.
assertFalse(channel.writeInbound(2));
// cancel of active subscription should clear the queue and fail future Subscribers.
subscription1.cancel();
if (doChannelRead) {
try {
assertFalse(channel.writeInbound(3));
} catch (Exception e) {
assertThat(e, instanceOf(ClosedChannelException.class));
return;
}
}
toSource(publisher).subscribe(subscriber2);
subscriber2.awaitSubscription().request(Long.MAX_VALUE);
assertThat(subscriber2.pollAllOnNext(), is(empty()));
assertThat(subscriber2.awaitOnError(), is(instanceOf(ClosedChannelException.class)));
}
Aggregations