Search in sources :

Example 1 with Subscriber

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

the class SubmissionPublisherTest method testMissedSignal_8187947.

/**
 * Tests scenario for
 * JDK-8187947: A race condition in SubmissionPublisher
 * cvs update -D '2017-11-25' src/main/java/util/concurrent/SubmissionPublisher.java && ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=SubmissionPublisherTest -Djsr166.methodFilter=testMissedSignal tck; cvs update -A src/main/java/util/concurrent/SubmissionPublisher.java
 */
public void testMissedSignal_8187947() throws Exception {
    final int N = expensiveTests ? (1 << 20) : (1 << 10);
    final CountDownLatch finished = new CountDownLatch(1);
    final SubmissionPublisher<Boolean> pub = new SubmissionPublisher<>();
    class Sub implements Subscriber<Boolean> {

        int received;

        public void onSubscribe(Subscription s) {
            s.request(N);
        }

        public void onNext(Boolean item) {
            if (++received == N)
                finished.countDown();
            else
                CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE));
        }

        public void onError(Throwable t) {
            throw new AssertionError(t);
        }

        public void onComplete() {
        }
    }
    pub.subscribe(new Sub());
    CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE));
    await(finished);
}
Also used : SubmissionPublisher(java8.util.concurrent.SubmissionPublisher) Subscriber(java8.util.concurrent.Flow.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java8.util.concurrent.Flow.Subscription)

Example 2 with Subscriber

use of java8.util.concurrent.Flow.Subscriber 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)

Aggregations

SubmissionPublisher (java8.util.concurrent.SubmissionPublisher)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Subscriber (java8.util.concurrent.Flow.Subscriber)1 Subscription (java8.util.concurrent.Flow.Subscription)1