-
Notifications
You must be signed in to change notification settings - Fork 534
Add SubmissionPublisher TCK test #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/************************************************************************ | ||
* Licensed under Public Domain (CC0) * | ||
* * | ||
* To the extent possible under law, the person who associated CC0 with * | ||
* this code has waived all copyright and related or neighboring * | ||
* rights to this code. * | ||
* * | ||
* You should have received a copy of the CC0 legalcode along with this * | ||
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.* | ||
************************************************************************/ | ||
|
||
package org.reactivestreams.tck.flow; | ||
|
||
import org.reactivestreams.tck.TestEnvironment; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.concurrent.*; | ||
|
||
@Test | ||
public class SubmissionPublisherTest extends FlowPublisherVerification<Integer> { | ||
|
||
public SubmissionPublisherTest() { | ||
super(new TestEnvironment()); | ||
} | ||
|
||
@Override | ||
public Flow.Publisher<Integer> createFlowPublisher(final long elements) { | ||
final SubmissionPublisher<Integer> sp = new SubmissionPublisher<Integer>(); | ||
|
||
Thread t = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
while (sp.getNumberOfSubscribers() == 0) { | ||
try { | ||
Thread.sleep(1); | ||
} catch (InterruptedException ex) { | ||
return; | ||
} | ||
} | ||
|
||
for (int i = 0; i < elements; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if there are no subscribers, the loop will rush through without blocking. |
||
sp.submit(i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this stop blocking if all subscribers cancel their subscriptions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess so. This test passes for me: SubmissionPublisher<Integer> sp = new SubmissionPublisher<>(
ForkJoinPool.commonPool(), 1);
TestConsumer<Integer> tc1 = new TestConsumer<>(0L);
TestConsumer<Integer> tc2 = new TestConsumer<>(0L);
sp.subscribe(tc1);
sp.subscribe(tc2);
CountDownLatch cdl = new CountDownLatch(1);
SchedulerServices.single().schedule(() -> {
for (int i = 0; i < 512; i++) {
sp.submit(i);
}
cdl.countDown();
});
Thread.sleep(1000);
assertEquals(1, cdl.getCount());
tc1.cancel();
tc2.cancel();
assertTrue(cdl.await(5, TimeUnit.SECONDS)); |
||
} | ||
|
||
sp.close(); | ||
} | ||
}); | ||
t.setDaemon(true); | ||
t.start(); | ||
|
||
return sp; | ||
} | ||
|
||
@Override | ||
public Flow.Publisher<Integer> createFailedFlowPublisher() { | ||
SubmissionPublisher<Integer> sp = new SubmissionPublisher<Integer>(); | ||
sp.closeExceptionally(new Exception()); | ||
return sp; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thread.onSpinWait()? :)