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