forked from reactive-streams/reactive-streams-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactiveStreamsFlowBridgeTest.java
178 lines (137 loc) · 5.04 KB
/
ReactiveStreamsFlowBridgeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/************************************************************************
* 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;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;
public class ReactiveStreamsFlowBridgeTest {
@Test
public void reactiveToFlowNormal() {
MulticastPublisher<Integer> p = new MulticastPublisher<Integer>(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, Flow.defaultBufferSize());
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
ReactiveStreamsFlowBridge.toFlow(p).subscribe(tc);
p.offer(1);
p.offer(2);
p.offer(3);
p.offer(4);
p.offer(5);
p.complete();
tc.assertRange(1, 5);
}
@Test
public void reactiveToFlowError() {
MulticastPublisher<Integer> p = new MulticastPublisher<Integer>(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, Flow.defaultBufferSize());
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
ReactiveStreamsFlowBridge.toFlow(p).subscribe(tc);
p.offer(1);
p.offer(2);
p.offer(3);
p.offer(4);
p.offer(5);
p.completeExceptionally(new IOException());
tc.assertFailure(IOException.class, 1, 2, 3, 4, 5);
}
@Test
public void flowToReactiveNormal() {
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, Flow.defaultBufferSize());
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
ReactiveStreamsFlowBridge.toReactiveStreams(p).subscribe(tc);
p.submit(1);
p.submit(2);
p.submit(3);
p.submit(4);
p.submit(5);
p.close();
tc.assertRange(1, 5);
}
@Test
public void flowToReactiveError() {
SubmissionPublisher<Integer> p = new SubmissionPublisher<Integer>(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, Flow.defaultBufferSize());
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
ReactiveStreamsFlowBridge.toReactiveStreams(p).subscribe(tc);
p.submit(1);
p.submit(2);
p.submit(3);
p.submit(4);
p.submit(5);
p.closeExceptionally(new IOException());
tc.assertFailure(IOException.class, 1, 2, 3, 4, 5);
}
@Test
public void reactiveStreamsToFlowSubscriber() {
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
Flow.Subscriber<Integer> fs = ReactiveStreamsFlowBridge.toFlowSubscriber(tc);
final Object[] state = { null, null };
fs.onSubscribe(new Flow.Subscription() {
@Override
public void request(long n) {
state[0] = n;
}
@Override
public void cancel() {
state[1] = true;
}
});
Assert.assertEquals(state[0], Long.MAX_VALUE);
fs.onNext(1);
fs.onNext(2);
fs.onNext(3);
fs.onComplete();
tc.assertResult(1, 2, 3);
Assert.assertNull(state[1]);
}
@Test
public void flowToReactiveStreamsSubscriber() {
TestEitherConsumer<Integer> tc = new TestEitherConsumer<Integer>();
org.reactivestreams.Subscriber<Integer> fs = ReactiveStreamsFlowBridge.toReactiveStreamsSubscriber(tc);
final Object[] state = { null, null };
fs.onSubscribe(new org.reactivestreams.Subscription() {
@Override
public void request(long n) {
state[0] = n;
}
@Override
public void cancel() {
state[1] = true;
}
});
Assert.assertEquals(state[0], Long.MAX_VALUE);
fs.onNext(1);
fs.onNext(2);
fs.onNext(3);
fs.onComplete();
tc.assertResult(1, 2, 3);
Assert.assertNull(state[1]);
}
}