Skip to content

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

Merged
merged 3 commits into from
Nov 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread.onSpinWait()? :)

} catch (InterruptedException ex) {
return;
}
}

for (int i = 0; i < elements; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add && sp.getNumberOfSubscribers() > 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this stop blocking if all subscribers cancel their subscriptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}