-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathTestEitherConsumer.java
174 lines (147 loc) · 5.6 KB
/
TestEitherConsumer.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
/************************************************************************
* 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Flow;
import java.util.concurrent.TimeUnit;
/**
* Class that provides basic state assertions on received elements and
* terminal signals from either a Reactive Streams Publisher or a
* Flow Publisher.
* <p>
* As with standard {@link Subscriber}s, an instance of this class
* should be subscribed to at most one (Reactive Streams or
* Flow) Publisher.
* </p>
* @param <T> the element type
*/
class TestEitherConsumer<T> implements Flow.Subscriber<T>, Subscriber<T> {
protected final List<T> values;
protected final List<Throwable> errors;
protected int completions;
protected Flow.Subscription subscription;
protected Subscription subscriptionRs;
protected final CountDownLatch done;
final long initialRequest;
public TestEitherConsumer() {
this(Long.MAX_VALUE);
}
public TestEitherConsumer(long initialRequest) {
this.values = new ArrayList<T>();
this.errors = new ArrayList<Throwable>();
this.done = new CountDownLatch(1);
this.initialRequest = initialRequest;
}
@Override
public final void onSubscribe(Flow.Subscription s) {
this.subscription = s;
s.request(initialRequest);
}
@Override
public void onSubscribe(Subscription s) {
this.subscriptionRs = s;
s.request(initialRequest);
}
@Override
public void onNext(T item) {
values.add(item);
if (subscription == null && subscriptionRs == null) {
errors.add(new IllegalStateException("onSubscribe not called"));
}
}
@Override
public void onError(Throwable throwable) {
errors.add(throwable);
if (subscription == null && subscriptionRs == null) {
errors.add(new IllegalStateException("onSubscribe not called"));
}
done.countDown();
}
@Override
public void onComplete() {
completions++;
if (subscription == null && subscriptionRs == null) {
errors.add(new IllegalStateException("onSubscribe not called"));
}
done.countDown();
}
public final void cancel() {
// FIXME implement deferred cancellation
}
public final List<T> values() {
return values;
}
public final List<Throwable> errors() {
return errors;
}
public final int completions() {
return completions;
}
public final boolean await(long timeout, TimeUnit unit) throws InterruptedException {
return done.await(timeout, unit);
}
public final TestEitherConsumer<T> assertResult(T... items) {
if (!values.equals(Arrays.asList(items))) {
throw new AssertionError("Expected: " + Arrays.toString(items) + ", Actual: " + values + ", Completions: " + completions);
}
if (completions != 1) {
throw new AssertionError("Not completed: " + completions);
}
return this;
}
public final TestEitherConsumer<T> assertFailure(Class<? extends Throwable> errorClass, T... items) {
if (!values.equals(Arrays.asList(items))) {
throw new AssertionError("Expected: " + Arrays.toString(items) + ", Actual: " + values + ", Completions: " + completions);
}
if (completions != 0) {
throw new AssertionError("Completed: " + completions);
}
if (errors.isEmpty()) {
throw new AssertionError("No errors");
}
if (!errorClass.isInstance(errors.get(0))) {
AssertionError ae = new AssertionError("Wrong throwable");
ae.initCause(errors.get(0));
throw ae;
}
return this;
}
public final TestEitherConsumer<T> awaitDone(long timeout, TimeUnit unit) {
try {
if (!done.await(timeout, unit)) {
subscription.cancel();
throw new RuntimeException("Timed out. Values: " + values.size()
+ ", Errors: " + errors.size() + ", Completions: " + completions);
}
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted");
}
return this;
}
public final TestEitherConsumer<T> assertRange(int start, int count) {
if (values.size() != count) {
throw new AssertionError("Expected: " + count + ", Actual: " + values.size());
}
for (int i = 0; i < count; i++) {
if ((Integer)values.get(i) != start + i) {
throw new AssertionError("Index: " + i + ", Expected: "
+ (i + start) + ", Actual: " +values.get(i));
}
}
if (completions != 1) {
throw new AssertionError("Not completed: " + completions);
}
return this;
}
}