Search in sources :

Example 6 with Subscription

use of io.servicetalk.concurrent.PublisherSource.Subscription in project servicetalk by apple.

the class ConcurrentSubscriptionTest method singleThreadMultipleRequest.

@Test
void singleThreadMultipleRequest() throws InterruptedException {
    Subscription concurrent = ConcurrentSubscription.wrap(subscription);
    final int demand = 100;
    for (int i = 0; i < demand; ++i) {
        concurrent.request(1);
    }
    subscription.awaitRequestN(demand);
    assertFalse(subscription.isCancelled());
}
Also used : Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 7 with Subscription

use of io.servicetalk.concurrent.PublisherSource.Subscription in project servicetalk by apple.

the class ConcurrentSubscriptionTest method singleThreadSingleRequest.

@Test
void singleThreadSingleRequest() throws InterruptedException {
    Subscription concurrent = ConcurrentSubscription.wrap(subscription);
    final long demand = Long.MAX_VALUE;
    concurrent.request(demand);
    subscription.awaitRequestN(demand);
    assertFalse(subscription.isCancelled());
}
Also used : Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 8 with Subscription

use of io.servicetalk.concurrent.PublisherSource.Subscription in project servicetalk by apple.

the class ConcurrentSubscriptionTest method singleThreadReentrant.

@Test
void singleThreadReentrant() {
    final ReentrantSubscription reentrantSubscription = new ReentrantSubscription(50);
    final Subscription concurrent = ConcurrentSubscription.wrap(reentrantSubscription);
    reentrantSubscription.outerSubscription(concurrent);
    concurrent.request(1);
    assertEquals(reentrantSubscription.reentrantLimit, reentrantSubscription.innerSubscription.requested());
}
Also used : Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 9 with Subscription

use of io.servicetalk.concurrent.PublisherSource.Subscription in project servicetalk by apple.

the class ConcurrentSubscriptionTest method singleThreadCancelDeliveredIfRequestThrows.

@Test
void singleThreadCancelDeliveredIfRequestThrows() throws InterruptedException {
    CountDownLatch cancelledLatch = new CountDownLatch(1);
    Subscription concurrent = ConcurrentSubscription.wrap(new Subscription() {

        @Override
        public void request(final long n) {
            throw DELIBERATE_EXCEPTION;
        }

        @Override
        public void cancel() {
            cancelledLatch.countDown();
        }
    });
    try {
        concurrent.request(1);
        fail();
    } catch (DeliberateException e) {
        assertSame(DELIBERATE_EXCEPTION, e);
    }
    concurrent.cancel();
    cancelledLatch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test)

Example 10 with Subscription

use of io.servicetalk.concurrent.PublisherSource.Subscription in project servicetalk by apple.

the class TimeoutPublisherTest method justSubscribeTimeout.

@ParameterizedTest(name = "{displayName} [{index}] {arguments}")
@EnumSource(TimerBehaviorParam.class)
void justSubscribeTimeout(TimerBehaviorParam params) {
    DelayedOnSubscribePublisher<Integer> delayedPublisher = new DelayedOnSubscribePublisher<>();
    init(delayedPublisher, params, Duration.ofNanos(1), false);
    testExecutor.advanceTimeBy(1, NANOSECONDS);
    assertThat(testExecutor.scheduledTasksPending(), is(0));
    assertThat(testExecutor.scheduledTasksExecuted(), is(1));
    Subscription mockSubscription = mock(Subscription.class);
    Subscriber<? super Integer> subscriber = delayedPublisher.subscriber;
    assertNotNull(subscriber);
    subscriber.onSubscribe(mockSubscription);
    verify(mockSubscription).cancel();
    assertThat(this.subscriber.awaitOnError(), instanceOf(TimeoutException.class));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) TimeoutException(java.util.concurrent.TimeoutException) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)181 Test (org.junit.jupiter.api.Test)107 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)78 CountDownLatch (java.util.concurrent.CountDownLatch)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)44 AtomicReference (java.util.concurrent.atomic.AtomicReference)44 TestPublisherSubscriber (io.servicetalk.concurrent.test.internal.TestPublisherSubscriber)38 DELIBERATE_EXCEPTION (io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION)31 Nullable (javax.annotation.Nullable)31 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)30 MethodSource (org.junit.jupiter.params.provider.MethodSource)28 ValueSource (org.junit.jupiter.params.provider.ValueSource)28 PublisherSource (io.servicetalk.concurrent.PublisherSource)27 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)27 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)27 Matchers.is (org.hamcrest.Matchers.is)27 Subscriber (io.servicetalk.concurrent.PublisherSource.Subscriber)26 CyclicBarrier (java.util.concurrent.CyclicBarrier)25 ExecutorService (java.util.concurrent.ExecutorService)25 Mockito.mock (org.mockito.Mockito.mock)25