use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class DefaultSerializerDeserializationTest method publisherCompletesWithLeftOverData.
@Test
void publisherCompletesWithLeftOverData() {
Buffer first = mock(Buffer.class);
Buffer second = mock(Buffer.class);
when(deSerializer.deserialize(first)).thenReturn(singletonList("Hello1"));
when(deSerializer.deserialize(second)).thenReturn(emptyList());
when(deSerializer.hasData()).thenReturn(true);
final IllegalStateException e = new IllegalStateException();
doThrow(e).when(deSerializer).close();
final Publisher<String> deserialized = factory.deserialize(from(first, second), String.class);
TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
toSource(deserialized).subscribe(subscriber);
subscriber.awaitSubscription().request(2);
verify(provider).getDeserializer(String.class);
verify(deSerializer).deserialize(first);
verify(deSerializer).deserialize(second);
assertThat(subscriber.takeOnNext(), is("Hello1"));
assertThat(subscriber.awaitOnError(), sameInstance(e));
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class MulticastPublisherTest method concurrentRequestN.
@Test
void concurrentRequestN() throws InterruptedException {
final int expectedSubscribers = 50;
Publisher<Integer> multicast = source.multicast(expectedSubscribers);
@SuppressWarnings("unchecked") TestPublisherSubscriber<Integer>[] subscribers = (TestPublisherSubscriber<Integer>[]) new TestPublisherSubscriber[expectedSubscribers];
final int expectedSubscribersMinus1 = expectedSubscribers - 1;
for (int i = 0; i < expectedSubscribersMinus1; ++i) {
subscribers[i] = new TestPublisherSubscriber<>();
toSource(multicast).subscribe(subscribers[i]);
}
subscribers[expectedSubscribersMinus1] = new TestPublisherSubscriber<>();
toSource(multicast).subscribe(subscribers[expectedSubscribersMinus1]);
for (int i = 0; i < expectedSubscribersMinus1; ++i) {
subscribers[i].awaitSubscription();
}
ExecutorService executorService = new ThreadPoolExecutor(0, expectedSubscribers, 1, SECONDS, new SynchronousQueue<>());
try {
CyclicBarrier barrier = new CyclicBarrier(expectedSubscribers);
CountDownLatch doneLatch = new CountDownLatch(expectedSubscribers);
AtomicReference<Throwable> throwableRef = new AtomicReference<>();
for (int i = 1; i <= expectedSubscribers; ++i) {
executorService.execute(requestIRunnable(subscribers, i, i, barrier, throwableRef, doneLatch));
}
doneLatch.await();
assertThat(throwableRef.get(), is(nullValue()));
assertThat(subscription.requested(), is(1L));
assertThat(subscription.isCancelled(), is(false));
} finally {
executorService.shutdown();
}
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class MulticastPublisherTest method concurrentRequestNAndOnNext.
@Test
void concurrentRequestNAndOnNext() throws BrokenBarrierException, InterruptedException {
final int expectedSubscribers = 400;
Publisher<Integer> multicast = source.multicast(expectedSubscribers);
@SuppressWarnings("unchecked") TestPublisherSubscriber<Integer>[] subscribers = (TestPublisherSubscriber<Integer>[]) new TestPublisherSubscriber[expectedSubscribers];
final int expectedSubscribersMinus1 = expectedSubscribers - 1;
for (int i = 0; i < expectedSubscribersMinus1; ++i) {
subscribers[i] = new TestPublisherSubscriber<>();
toSource(multicast).subscribe(subscribers[i]);
}
subscribers[expectedSubscribersMinus1] = new TestPublisherSubscriber<>();
toSource(multicast).subscribe(subscribers[expectedSubscribersMinus1]);
for (int i = 0; i < expectedSubscribersMinus1; ++i) {
subscribers[i].awaitSubscription();
}
ExecutorService executorService = new ThreadPoolExecutor(0, expectedSubscribers, 1, SECONDS, new SynchronousQueue<>());
try {
CyclicBarrier barrier = new CyclicBarrier(expectedSubscribers + 1);
CountDownLatch doneLatch = new CountDownLatch(expectedSubscribers);
AtomicReference<Throwable> throwableRef = new AtomicReference<>();
for (int i = 1; i <= expectedSubscribers; ++i) {
executorService.execute(requestIRunnable(subscribers, i, expectedSubscribers, barrier, throwableRef, doneLatch));
}
barrier.await();
for (int i = 0; i < expectedSubscribers; ++i) {
subscription.awaitRequestN(i + 1);
source.onNext(i);
}
doneLatch.await();
assertThat(throwableRef.get(), is(nullValue()));
List<Integer> expectedItems = new ArrayList<>(expectedSubscribers);
for (int x = 0; x < expectedSubscribers; ++x) {
expectedItems.add(x);
}
for (int i = 0; i < expectedSubscribers; ++i) {
final Integer[] expectedSubset = expectedItems.subList(0, i).toArray(new Integer[0]);
List<Integer> actual = subscribers[i].takeOnNext(i);
if (expectedSubset.length == 0) {
assertThat(actual.isEmpty(), is(true));
} else {
assertThat(actual, contains(expectedSubset));
}
}
assertThat(subscription.requested(), is((long) expectedSubscribers));
assertThat(subscription.isCancelled(), is(false));
} finally {
executorService.shutdown();
}
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber 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.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class ScanWithPublisherTest method scanWithFinalizationOnCancel.
@Test
void scanWithFinalizationOnCancel() {
final AtomicInteger finalizations = new AtomicInteger(0);
PublisherSource.Processor<Integer, Integer> processor = newPublisherProcessor();
TestPublisherSubscriber<Integer> subscriber = new TestPublisherSubscriber<>();
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() {
return true;
}
@Override
public void afterFinally() {
finalizations.incrementAndGet();
}
})).subscribe(subscriber);
Subscription s = subscriber.awaitSubscription();
s.request(1);
processor.onNext(1);
processor.onComplete();
s.cancel();
assertThat(finalizations.get(), is(1));
}
Aggregations