Search in sources :

Example 1 with SubmissionPublisher

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

the class SubmissionPublisherTest method testRecoveredHandledDroppedTimedOffer.

/**
 * Timed offer succeeds if drop handler forces request
 */
public void testRecoveredHandledDroppedTimedOffer() {
    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;
    long delay = timeoutMillis();
    long startTime = System.nanoTime();
    for (int i = 1; i <= 6; ++i) {
        int d = p.offer(i, delay, MILLISECONDS, (s, x) -> reqHandle(calls, s));
        n = n + 2 + (d < 0 ? d : 0);
    }
    assertTrue(millisElapsedSince(startTime) >= delay);
    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 2 with SubmissionPublisher

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

the class SubmissionPublisherTest method testConstructor2.

/**
 * A new SubmissionPublisher has no subscribers, is not closed,
 * has the given buffer size, and uses the given executor
 */
public void testConstructor2() {
    Executor e = Executors.newFixedThreadPool(1);
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>(e, 8);
    checkInitialState(p);
    assertSame(p.getExecutor(), e);
    assertEquals(8, p.getMaxBufferCapacity());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Executor(java.util.concurrent.Executor) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Example 3 with SubmissionPublisher

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

the class SubmissionPublisherTest method testConstructor4.

/**
 * A negative capacity argument to SubmissionPublisher constructor
 * throws IllegalArgumentException
 */
public void testConstructor4() {
    Executor e = Executors.newFixedThreadPool(1);
    try {
        new SubmissionPublisher<Integer>(e, -1);
        shouldThrow();
    } catch (IllegalArgumentException success) {
    }
}
Also used : Executor(java.util.concurrent.Executor) SubmissionPublisher(java8.util.concurrent.SubmissionPublisher)

Example 4 with SubmissionPublisher

use of java8.util.concurrent.SubmissionPublisher 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 5 with SubmissionPublisher

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

the class SubmissionPublisherTest method testHandledDroppedTimedOffer.

/**
 * Timed offer invokes drop handler if saturated
 */
public void testHandledDroppedTimedOffer() {
    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();
    long delay = timeoutMillis();
    for (int i = 1; i <= 4; ++i) assertTrue(p.offer(i, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) >= 0);
    long startTime = System.nanoTime();
    assertTrue(p.offer(5, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
    s1.sn.request(64);
    assertTrue(p.offer(6, delay, MILLISECONDS, (s, x) -> noopHandle(calls)) < 0);
    assertTrue(millisElapsedSince(startTime) >= delay);
    s2.sn.request(64);
    p.close();
    s2.awaitComplete();
    s1.awaitComplete();
    assertTrue(calls.get() >= 2);
}
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