-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathFlowAdaptersTest.java
234 lines (184 loc) · 7.37 KB
/
FlowAdaptersTest.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/************************************************************************
* 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 FlowAdaptersTest {
@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>();
FlowAdapters.toFlowPublisher(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>();
FlowAdapters.toFlowPublisher(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>();
FlowAdapters.toPublisher(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>();
FlowAdapters.toPublisher(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 = FlowAdapters.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 = FlowAdapters.toSubscriber(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]);
}
@Test
public void stableConversionForSubscriber() {
Subscriber<Integer> rsSub = new Subscriber<Integer>() {
@Override public void onSubscribe(Subscription s) {};
@Override public void onNext(Integer i) {};
@Override public void onError(Throwable t) {};
@Override public void onComplete() {};
};
Flow.Subscriber<Integer> fSub = new Flow.Subscriber<Integer>() {
@Override public void onSubscribe(Flow.Subscription s) {};
@Override public void onNext(Integer i) {};
@Override public void onError(Throwable t) {};
@Override public void onComplete() {};
};
Assert.assertSame(FlowAdapters.toSubscriber(FlowAdapters.toFlowSubscriber(rsSub)), rsSub);
Assert.assertSame(FlowAdapters.toFlowSubscriber(FlowAdapters.toSubscriber(fSub)), fSub);
}
@Test
public void stableConversionForProcessor() {
Processor<Integer, Integer> rsPro = new Processor<Integer, Integer>() {
@Override public void onSubscribe(Subscription s) {};
@Override public void onNext(Integer i) {};
@Override public void onError(Throwable t) {};
@Override public void onComplete() {};
@Override public void subscribe(Subscriber s) {};
};
Flow.Processor<Integer, Integer> fPro = new Flow.Processor<Integer, Integer>() {
@Override public void onSubscribe(Flow.Subscription s) {};
@Override public void onNext(Integer i) {};
@Override public void onError(Throwable t) {};
@Override public void onComplete() {};
@Override public void subscribe(Flow.Subscriber s) {};
};
Assert.assertSame(FlowAdapters.toProcessor(FlowAdapters.toFlowProcessor(rsPro)), rsPro);
Assert.assertSame(FlowAdapters.toFlowProcessor(FlowAdapters.toProcessor(fPro)), fPro);
}
@Test
public void stableConversionForPublisher() {
Publisher<Integer> rsPub = new Publisher<Integer>() {
@Override public void subscribe(Subscriber s) {};
};
Flow.Publisher<Integer> fPub = new Flow.Publisher<Integer>() {
@Override public void subscribe(Flow.Subscriber s) {};
};
Assert.assertSame(FlowAdapters.toPublisher(FlowAdapters.toFlowPublisher(rsPub)), rsPub);
Assert.assertSame(FlowAdapters.toFlowPublisher(FlowAdapters.toPublisher(fPub)), fPub);
}
}