Search in sources :

Example 1 with TestPublisherSubscriber

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);
    }
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestResponse(io.servicetalk.grpc.netty.TesterProto.TestResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with TestPublisherSubscriber

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();
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Test(org.junit.jupiter.api.Test)

Example 3 with TestPublisherSubscriber

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();
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with TestPublisherSubscriber

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;
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) ExecutorExtension(io.servicetalk.concurrent.api.ExecutorExtension) Completable(io.servicetalk.concurrent.api.Completable) TestCancellable(io.servicetalk.concurrent.api.TestCancellable) Thread.currentThread(java.lang.Thread.currentThread) Disabled(org.junit.jupiter.api.Disabled) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) Executor(io.servicetalk.concurrent.api.Executor) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Thread.currentThread(java.lang.Thread.currentThread)

Example 5 with TestPublisherSubscriber

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));
}
Also used : TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) DefaultServiceDiscovererEvent(io.servicetalk.client.api.DefaultServiceDiscovererEvent) Collection(java.util.Collection) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)30 Test (org.junit.jupiter.api.Test)16 CountDownLatch (java.util.concurrent.CountDownLatch)13 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 PublisherSource (io.servicetalk.concurrent.PublisherSource)9 Nullable (javax.annotation.Nullable)7 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 Buffer (io.servicetalk.buffer.api.Buffer)5 TestCompletable (io.servicetalk.concurrent.api.TestCompletable)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)4 ExecutorService (java.util.concurrent.ExecutorService)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 TestCancellable (io.servicetalk.concurrent.api.TestCancellable)3 Thread.currentThread (java.lang.Thread.currentThread)3 ArrayList (java.util.ArrayList)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 LegacyTestSingle (io.servicetalk.concurrent.api.LegacyTestSingle)2