| 1 |
/************************************************************************ |
| 2 |
* Licensed under Public Domain (CC0) * |
| 3 |
* * |
| 4 |
* To the extent possible under law, the person who associated CC0 with * |
| 5 |
* this code has waived all copyright and related or neighboring * |
| 6 |
* rights to this code. * |
| 7 |
* * |
| 8 |
* You should have received a copy of the CC0 legalcode along with this * |
| 9 |
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.* |
| 10 |
************************************************************************/ |
| 11 |
|
| 12 |
package org.reactivestreams.tck.flow; |
| 13 |
|
| 14 |
import org.reactivestreams.tck.TestEnvironment; |
| 15 |
import org.reactivestreams.tck.flow.FlowSubscriberBlackboxVerification; |
| 16 |
import org.reactivestreams.tck.flow.support.SyncTriggeredDemandFlowSubscriber; |
| 17 |
import org.testng.annotations.AfterClass; |
| 18 |
import org.testng.annotations.BeforeClass; |
| 19 |
import org.testng.annotations.Test; |
| 20 |
|
| 21 |
import java.util.concurrent.ExecutorService; |
| 22 |
import java.util.concurrent.Executors; |
| 23 |
import java.util.concurrent.Flow; |
| 24 |
|
| 25 |
@Test // Must be here for TestNG to find and run this, do not remove |
| 26 |
public class SyncTriggeredDemandSubscriberTest extends FlowSubscriberBlackboxVerification<Integer> { |
| 27 |
|
| 28 |
private ExecutorService e; |
| 29 |
@BeforeClass void before() { e = Executors.newFixedThreadPool(4); } |
| 30 |
@AfterClass void after() { if (e != null) e.shutdown(); } |
| 31 |
|
| 32 |
public SyncTriggeredDemandSubscriberTest() { |
| 33 |
super(new TestEnvironment()); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void triggerFlowRequest(Flow.Subscriber<? super Integer> subscriber) { |
| 38 |
((SyncTriggeredDemandFlowSubscriber<? super Integer>) subscriber).triggerDemand(1); |
| 39 |
} |
| 40 |
|
| 41 |
@Override public Flow.Subscriber<Integer> createFlowSubscriber() { |
| 42 |
return new SyncTriggeredDemandFlowSubscriber<Integer>() { |
| 43 |
private long acc; |
| 44 |
@Override protected long foreach(final Integer element) { |
| 45 |
acc += element; |
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
@Override public void onComplete() { |
| 50 |
} |
| 51 |
}; |
| 52 |
} |
| 53 |
|
| 54 |
@Override public Integer createElement(int element) { |
| 55 |
return element; |
| 56 |
} |
| 57 |
} |