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);
}
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());
}
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) {
}
}
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);
}
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);
}
Aggregations