use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class ErrorHandlingTest method verifyStreamingResponse.
private void verifyStreamingResponse(final Publisher<TestResponse> resp) throws Exception {
TestPublisherSubscriber<TestResponse> subscriber = new TestPublisherSubscriber<>();
CountDownLatch terminationLatch = new CountDownLatch(1);
toSource(resp.afterFinally(terminationLatch::countDown)).subscribe(subscriber);
subscriber.awaitSubscription().request(Long.MAX_VALUE);
terminationLatch.await();
Throwable cause;
switch(testMode) {
case ServiceEmitsDataThenError:
case ServiceEmitsDataThenGrpcException:
case BlockingServiceWritesThenThrows:
case BlockingServiceWritesThenThrowsGrpcException:
case ServiceSecondOperatorThrowsGrpcException:
List<TestResponse> items = subscriber.takeOnNext(1);
assertThat("Unexpected response.", items, hasSize(1));
assertThat("Unexpected response.", items, contains(cannedResponse));
cause = subscriber.awaitOnError();
assertThat("Unexpected termination.", cause, is(notNullValue()));
verifyException(cause);
break;
case HttpClientFilterThrows:
case HttpClientFilterThrowsGrpcException:
case HttpClientFilterEmitsError:
case HttpClientFilterEmitsGrpcException:
case HttpFilterThrows:
case HttpFilterThrowsGrpcException:
case HttpFilterEmitsError:
case HttpFilterEmitsGrpcException:
case ServiceThrows:
case ServiceThrowsGrpcException:
case ServiceOperatorThrows:
case ServiceOperatorThrowsGrpcException:
case ServiceEmitsError:
case ServiceEmitsGrpcException:
case BlockingServiceThrows:
case BlockingServiceThrowsGrpcException:
cause = subscriber.awaitOnError();
assertThat("Unexpected termination.", cause, is(notNullValue()));
verifyException(cause);
break;
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class DefaultSerializerSerializationTest method applySerializationForPublisherWithTypeAndEstimator.
@Test
void applySerializationForPublisherWithTypeAndEstimator() {
TestPublisher<String> source = new TestPublisher<>();
final Publisher<Buffer> serialized = factory.serialize(source, allocator, String.class, sizeEstimator);
TestPublisherSubscriber<Buffer> subscriber = new TestPublisherSubscriber<>();
toSource(serialized).subscribe(subscriber);
subscriber.awaitSubscription().request(2);
verify(provider).getSerializer(String.class);
Buffer expected1 = verifySerializedBufferWithSizes(source, "Hello", 1);
assertThat(subscriber.takeOnNext(), is(expected1));
Buffer expected2 = verifySerializedBufferWithSizes(source, "Hello", 2);
assertThat(subscriber.takeOnNext(), is(expected2));
source.onComplete();
subscriber.awaitOnComplete();
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class CompletableToPublisherTest method publishOnOriginalIsPreservedOnComplete.
@Test
void publishOnOriginalIsPreservedOnComplete() throws Exception {
ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
TestPublisherSubscriber<String> subscriber = new TestPublisherSubscriber<>();
TestCompletable completable = new TestCompletable();
CountDownLatch analyzed = publishOnOriginalIsPreserved0(errors, subscriber, completable);
completable.onComplete();
analyzed.await();
assertThat("Unexpected errors observed: " + errors, errors, hasSize(0));
subscriber.awaitOnComplete();
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class CompletableToPublisherTest method publishOnOriginalIsPreserved0.
private CountDownLatch publishOnOriginalIsPreserved0(final ConcurrentLinkedQueue<AssertionError> errors, final TestPublisherSubscriber<String> subscriber, final TestCompletable completable) {
final Thread testThread = currentThread();
CountDownLatch analyzed = new CountDownLatch(1);
CountDownLatch receivedOnSubscribe = new CountDownLatch(1);
toSource(completable.publishOn(EXEC.executor()).beforeOnComplete(() -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked onComplete " + "(from Completable). Thread: " + currentThread()));
}
}).beforeOnError(__ -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked onError" + "(from Completable). Thread: " + currentThread()));
}
}).<String>toPublisher().beforeOnComplete(() -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked onComplete " + "(from Publisher). Thread: " + currentThread()));
}
}).beforeOnError(__ -> {
if (currentThread() == testThread) {
errors.add(new AssertionError("Invalid thread invoked onError " + "(from Publisher). Thread: " + currentThread()));
}
}).afterOnComplete(analyzed::countDown).afterOnError(__ -> analyzed.countDown()).afterOnSubscribe(__ -> receivedOnSubscribe.countDown())).subscribe(subscriber);
assertThat("Completable not subscribed.", completable.isSubscribed(), is(true));
return analyzed;
}
use of io.servicetalk.concurrent.test.internal.TestPublisherSubscriber in project servicetalk by apple.
the class DefaultDnsClientTest method srvInactiveEventsAggregated.
@ParameterizedTest(name = "missing-record-status={0}")
@MethodSource("missingRecordStatus")
void srvInactiveEventsAggregated(ServiceDiscovererEvent.Status missingRecordStatus) throws Exception {
setup(missingRecordStatus);
client.closeAsync().toFuture().get();
client = dnsClientBuilder(missingRecordStatus).inactiveEventsOnError(true).build();
final String domain = "sd.servicetalk.io";
final String targetDomain1 = "target1.mysvc.servicetalk.io";
final String targetDomain2 = "target2.mysvc.servicetalk.io";
final String targetDomain3 = "target3.mysvc.servicetalk.io";
final int targetPort = 9876;
final String ip1 = nextIp();
final String ip2 = nextIp();
final String ip3 = nextIp();
recordStore.addSrv(domain, targetDomain1, targetPort, DEFAULT_TTL);
recordStore.addSrv(domain, targetDomain2, targetPort, DEFAULT_TTL);
recordStore.addSrv(domain, targetDomain3, targetPort, DEFAULT_TTL);
recordStore.addIPv4Address(targetDomain1, DEFAULT_TTL, ip1);
recordStore.addIPv4Address(targetDomain2, DEFAULT_TTL, ip2);
recordStore.addIPv4Address(targetDomain3, DEFAULT_TTL, ip3);
Publisher<Collection<ServiceDiscovererEvent<InetSocketAddress>>> publisher = client.dnsSrvQuery(domain);
TestPublisherSubscriber<Collection<ServiceDiscovererEvent<InetSocketAddress>>> subscriber = new TestPublisherSubscriber<>();
toSource(publisher).subscribe(subscriber);
Subscription subscription = subscriber.awaitSubscription();
subscription.request(10);
List<ServiceDiscovererEvent<InetSocketAddress>> signals = new ArrayList<>();
do {
Collection<ServiceDiscovererEvent<InetSocketAddress>> next = subscriber.takeOnNext();
assertNotNull(next);
signals.addAll(next);
} while (signals.size() != 3);
assertHasEvent(signals, ip1, targetPort, AVAILABLE);
assertHasEvent(signals, ip2, targetPort, AVAILABLE);
assertHasEvent(signals, ip3, targetPort, AVAILABLE);
// Atomically remove all the SRV records, the next resolution should result in a host not found exception.
recordStore.removeRecords(createSrvRecord(domain, targetDomain1, targetPort, DEFAULT_TTL), createSrvRecord(domain, targetDomain2, targetPort, DEFAULT_TTL), createSrvRecord(domain, targetDomain3, targetPort, DEFAULT_TTL));
Collection<ServiceDiscovererEvent<InetSocketAddress>> next = subscriber.takeOnNext();
assertNotNull(next);
assertHasEvent(next, ip1, targetPort, missingRecordStatus);
assertHasEvent(next, ip2, targetPort, missingRecordStatus);
assertHasEvent(next, ip3, targetPort, missingRecordStatus);
assertThat(subscriber.awaitOnError(), instanceOf(UnknownHostException.class));
}
Aggregations