Search in sources :

Example 6 with SubmissionPublisher

use of java8.util.concurrent.SubmissionPublisher in project streamsupport by stefan-zobel.

the class SubmissionPublisherTest method testConstructor1.

/**
 * A default-constructed SubmissionPublisher has no subscribers,
 * is not closed, has default buffer size, and uses the
 * defaultExecutor
 */
public void testConstructor1() {
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>();
    checkInitialState(p);
    assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
    Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
    if (ForkJoinPool.getCommonPoolParallelism() > 1)
        assertSame(e, c);
    else
        assertNotSame(e, c);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Executor(java.util.concurrent.Executor) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Example 7 with SubmissionPublisher

use of java8.util.concurrent.SubmissionPublisher in project streamsupport by stefan-zobel.

the class SubmissionPublisherTest method testRecoveredHandledDroppedOffer.

/**
 * offer succeeds if drop handler forces request
 */
public void testRecoveredHandledDroppedOffer() {
    AtomicInteger calls = new AtomicInteger();
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4);
    TestSubscriber s1 = new TestSubscriber();
    s1.request = false;
    TestSubscriber s2 = new TestSubscriber();
    s2.request = false;
    p.subscribe(s1);
    p.subscribe(s2);
    s2.awaitSubscribe();
    s1.awaitSubscribe();
    int n = 0;
    for (int i = 1; i <= 8; ++i) {
        int d = p.offer(i, (s, x) -> reqHandle(calls, s));
        n = n + 2 + (d < 0 ? d : 0);
    }
    p.close();
    s2.awaitComplete();
    s1.awaitComplete();
    assertEquals(n, s1.nexts + s2.nexts);
    assertTrue(calls.get() >= 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Example 8 with SubmissionPublisher

use of java8.util.concurrent.SubmissionPublisher in project streamsupport by stefan-zobel.

the class SubmissionPublisherTest method testThrowOnNextHandler.

/**
 * If a handler is supplied in constructor, it is invoked when
 * subscriber throws an exception in onNext
 */
public void testThrowOnNextHandler() {
    AtomicInteger calls = new AtomicInteger();
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 8, (s, e) -> calls.getAndIncrement());
    TestSubscriber s1 = new TestSubscriber();
    TestSubscriber s2 = new TestSubscriber();
    p.subscribe(s1);
    p.subscribe(s2);
    s1.awaitSubscribe();
    p.submit(1);
    s1.throwOnCall = true;
    p.submit(2);
    p.close();
    s2.awaitComplete();
    assertEquals(2, s2.nexts);
    assertEquals(1, s2.completes);
    s1.awaitError();
    assertEquals(1, s1.errors);
    assertEquals(1, calls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Example 9 with SubmissionPublisher

use of java8.util.concurrent.SubmissionPublisher in project streamsupport by stefan-zobel.

the class SubmissionPublisherTest method testHandledDroppedOffer.

/**
 * offer invokes drop handler if saturated
 */
public void testHandledDroppedOffer() {
    AtomicInteger calls = new AtomicInteger();
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>(basicExecutor, 4);
    TestSubscriber s1 = new TestSubscriber();
    s1.request = false;
    TestSubscriber s2 = new TestSubscriber();
    s2.request = false;
    p.subscribe(s1);
    p.subscribe(s2);
    s2.awaitSubscribe();
    s1.awaitSubscribe();
    for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, (s, x) -> noopHandle(calls)) >= 0);
    p.offer(4, (s, x) -> noopHandle(calls));
    assertTrue(p.offer(6, (s, x) -> noopHandle(calls)) < 0);
    s1.sn.request(64);
    assertTrue(p.offer(7, (s, x) -> noopHandle(calls)) < 0);
    s2.sn.request(64);
    p.close();
    s2.awaitComplete();
    s1.awaitComplete();
    assertTrue(calls.get() >= 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(junit.framework.Test) Subscription(java8.util.concurrent.Flow.Subscription) Executor(java.util.concurrent.Executor) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Subscriber(java8.util.concurrent.Flow.Subscriber) Executors(java.util.concurrent.Executors) Flow(java8.util.concurrent.Flow) TestSuite(junit.framework.TestSuite) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java8.util.concurrent.CompletableFuture) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java8.util.concurrent.ForkJoinPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Aggregations

SubmissionPublisher (java8.util.concurrent.SubmissionPublisher)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Executor (java.util.concurrent.Executor)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 Subscriber (java8.util.concurrent.Flow.Subscriber)3 Subscription (java8.util.concurrent.Flow.Subscription)3 Executors (java.util.concurrent.Executors)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2 CompletableFuture (java8.util.concurrent.CompletableFuture)2 Flow (java8.util.concurrent.Flow)2 ForkJoinPool (java8.util.concurrent.ForkJoinPool)2 Test (junit.framework.Test)2 TestSuite (junit.framework.TestSuite)2