forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertableSubscriber.java
More file actions
276 lines (240 loc) · 9.45 KB
/
AssertableSubscriber.java
File metadata and controls
276 lines (240 loc) · 9.45 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.observers;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.*;
import rx.functions.Action0;
/**
* Interface for asserting the state of a sequence under testing with a {@code test()}
* method of a reactive base class.
* <p>
* This interface is not intended to be implemented outside of RxJava.
* <p>
* This interface extends {@link Observer} and allows injecting onXXX signals into
* the testing process.
* <p>History: 1.2.3 - experimental
* @param <T> the value type consumed by this Observer
* @since 1.3
*/
public interface AssertableSubscriber<T> extends Observer<T>, Subscription {
/**
* Allows manually calling the {@code onStart} method of the underlying Subscriber.
*/
void onStart();
/**
* Allows manually calling the {@code setProducer} method of the underlying Subscriber.
* @param p the producer to use, not null
*/
void setProducer(Producer p);
/**
* Returns the number of {@code onCompleted} signals received by this Observer.
* @return the number of {@code onCompleted} signals received
*/
int getCompletions();
/**
* Returns a list of received {@code onError} signals.
* @return this
*/
List<Throwable> getOnErrorEvents();
/**
* Returns the number of {@code onNext} signals received by this Observer in
* a thread-safe manner; one can read up to this number of elements from
* the {@code List} returned by {@link #getOnNextEvents()}.
* @return the number of {@code onNext} signals received.
*/
int getValueCount();
/**
* Requests the specified amount of items from upstream.
* @param n the amount requested, non-negative
* @return this
*/
AssertableSubscriber<T> requestMore(long n);
/**
* Returns the list of received {@code onNext} events.
* <p>If the sequence hasn't completed yet and is asynchronous, use the
* {@link #getValueCount()} method to determine how many elements are safe
* to be read from the list returned by this method.
* @return the List of received {@code onNext} events.
*/
List<T> getOnNextEvents();
/**
* Assert that this Observer received the given list of items as {@code onNext} signals
* in the same order and with the default null-safe object equals comparison.
* @param items the List of items expected
* @return this
*/
AssertableSubscriber<T> assertReceivedOnNext(List<T> items);
/**
* Assert that this Observer receives at least the given number of {@code onNext}
* signals within the specified timeout period.
* <p>
* Note that it is possible the AssertionError thrown by this method will
* contain an actual value >= to the expected one in case there is an emission
* race or unexpected delay on the emitter side. In this case, increase the timeout
* amount to avoid false positives.
*
* @param expected the expected (at least) number of {@code onNext} signals
* @param timeout the timeout to wait to receive the given number of {@code onNext} events
* @param unit the time unit
* @return this
*/
AssertableSubscriber<T> awaitValueCount(int expected, long timeout, TimeUnit unit);
/**
* Assert that this Observer received either an {@code onError} or {@code onCompleted} signal.
* @return this
*/
AssertableSubscriber<T> assertTerminalEvent();
/**
* Assert that this Observer has been unsubscribed via {@code unsubscribe()} or by a wrapping
* {@code SafeSubscriber}.
* @return this
*/
AssertableSubscriber<T> assertUnsubscribed();
/**
* Assert that this Observer has not received any {@code onError} signal.
* @return this
*/
AssertableSubscriber<T> assertNoErrors();
/**
* Waits for an {@code onError} or {code onCompleted} terminal event indefinitely.
* @return this
*/
AssertableSubscriber<T> awaitTerminalEvent();
/**
* Waits for an {@code onError} or {code onCompleted} terminal event for the given
* amount of timeout.
* @param timeout the time to wait for the terminal event
* @param unit the time unit of the wait time
* @return this
*/
AssertableSubscriber<T> awaitTerminalEvent(long timeout, TimeUnit unit);
/**
* Waits for an {@code onError} or {code onCompleted} terminal event for the given
* amount of timeout and unsubscribes the sequence if the timeout passed or the
* wait itself is interrupted.
* @param timeout the time to wait for the terminal event
* @param unit the time unit of the wait time
* @return this
*/
AssertableSubscriber<T> awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
TimeUnit unit);
/**
* Returns the Thread that has called the last {@code onNext}, {@code onError} or
* {@code onCompleted} methods of this Observer.
* @return this
*/
Thread getLastSeenThread();
/**
* Assert that this Observer received exaclty one {@code onCompleted} signal.
* @return this
*/
AssertableSubscriber<T> assertCompleted();
/**
* Assert that this Observer received no {@code onCompleted} signal.
* @return this
*/
AssertableSubscriber<T> assertNotCompleted();
/**
* Assert that this Observer received one {@code onError} signal with
* the given subclass of a Throwable as type.
* @param clazz the expected type of the {@code onError} signal received
* @return this
*/
AssertableSubscriber<T> assertError(Class<? extends Throwable> clazz);
/**
* Assert that this Observer received one {@code onError} signal with the
* object-equals of the given Throwable instance
* @param throwable the Throwable instance expected
* @return this
*/
AssertableSubscriber<T> assertError(Throwable throwable);
/**
* Assert that no {@code onError} or {@code onCompleted} signals were received so far.
* @return this
*/
AssertableSubscriber<T> assertNoTerminalEvent();
/**
* Assert that no {@code onNext} signals were received so far.
* @return this
*/
AssertableSubscriber<T> assertNoValues();
/**
* Assert that this Observer received exactly the given count of
* {@code onNext} signals.
* @param count the expected number of {@code onNext} signals
* @return this
*/
AssertableSubscriber<T> assertValueCount(int count);
/**
* Assert that this Observer received exactly the given expected values
* (compared via null-safe object equals) in the given order.
* @param values the expected values
* @return this
*/
AssertableSubscriber<T> assertValues(T... values);
/**
* Assert that this Observer received exactly the given single expected value
* (compared via null-safe object equals).
* @param value the single value expected
* @return this
*/
AssertableSubscriber<T> assertValue(T value);
/**
* Assert that this Observer received exactly the given values (compared via
* null-safe object equals) and if so, clears the internal buffer of the
* underlying Subscriber of these values.
* @param expectedFirstValue the first value expected
* @param expectedRestValues the rest of the values expected
* @return this
*/
AssertableSubscriber<T> assertValuesAndClear(T expectedFirstValue,
T... expectedRestValues);
/**
* Performs an action given by the Action0 callback in a fluent manner.
* @param action the action to perform, not null
* @return this
*/
AssertableSubscriber<T> perform(Action0 action);
@Override
void unsubscribe();
@Override
boolean isUnsubscribed();
/**
* Assert that this Observer received the specified items in the given order followed
* by a completion signal and no errors.
* @param values the values expected
* @return this
*/
AssertableSubscriber<T> assertResult(T... values);
/**
* Assert that this Observer received the specified items in the given order followed
* by an error signal of the given type (but no completion signal).
* @param errorClass the expected Throwable subclass type
* @param values the expected values
* @return this
*/
AssertableSubscriber<T> assertFailure(Class<? extends Throwable> errorClass, T... values);
/**
* Assert that this Observer received the specified items in the given order followed
* by an error signal of the given type and with the exact error message (but no completion signal).
* @param errorClass the expected Throwable subclass type
* @param message the expected error message returned by {@link Throwable#getMessage()}
* @param values the expected values
* @return this
*/
AssertableSubscriber<T> assertFailureAndMessage(Class<? extends Throwable> errorClass, String message, T... values);
}