| 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.Subscriber; |
| 15 |
import org.reactivestreams.Subscription; |
| 16 |
import org.reactivestreams.tck.TestEnvironment; |
| 17 |
import org.reactivestreams.tck.flow.support.SyncTriggeredDemandFlowSubscriber; |
| 18 |
import org.testng.annotations.AfterClass; |
| 19 |
import org.testng.annotations.BeforeClass; |
| 20 |
import org.testng.annotations.Test; |
| 21 |
|
| 22 |
import java.util.concurrent.ExecutorService; |
| 23 |
import java.util.concurrent.Executors; |
| 24 |
import java.util.concurrent.Flow; |
| 25 |
|
| 26 |
@Test // Must be here for TestNG to find and run this, do not remove |
| 27 |
public class SyncTriggeredDemandSubscriberWhiteboxTest extends FlowSubscriberWhiteboxVerification<Integer> { |
| 28 |
|
| 29 |
private ExecutorService e; |
| 30 |
@BeforeClass void before() { e = Executors.newFixedThreadPool(4); } |
| 31 |
@AfterClass void after() { if (e != null) e.shutdown(); } |
| 32 |
|
| 33 |
public SyncTriggeredDemandSubscriberWhiteboxTest() { |
| 34 |
super(new TestEnvironment()); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public Flow.Subscriber<Integer> createFlowSubscriber(final WhiteboxSubscriberProbe<Integer> probe) { |
| 39 |
return new SyncTriggeredDemandFlowSubscriber<Integer>() { |
| 40 |
@Override |
| 41 |
public void onSubscribe(final Flow.Subscription s) { |
| 42 |
super.onSubscribe(s); |
| 43 |
|
| 44 |
probe.registerOnSubscribe(new SubscriberPuppet() { |
| 45 |
@Override |
| 46 |
public void triggerRequest(long elements) { |
| 47 |
s.request(elements); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void signalCancel() { |
| 52 |
s.cancel(); |
| 53 |
} |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onNext(Integer element) { |
| 59 |
super.onNext(element); |
| 60 |
probe.registerOnNext(element); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onError(Throwable cause) { |
| 65 |
super.onError(cause); |
| 66 |
probe.registerOnError(cause); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onComplete() { |
| 71 |
super.onComplete(); |
| 72 |
probe.registerOnComplete(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected long foreach(Integer element) { |
| 77 |
return 1; |
| 78 |
} |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
@Override public Integer createElement(int element) { |
| 83 |
return element; |
| 84 |
} |
| 85 |
|
| 86 |
} |