18
18
import java .util .List ;
19
19
import java .util .concurrent .TimeUnit ;
20
20
21
- import rx .Observer ;
22
- import rx .Producer ;
21
+ import rx .* ;
22
+ import rx .annotations . Experimental ;
23
23
import rx .functions .Action0 ;
24
24
25
- public interface AssertableSubscriber <T > extends Observer <T > {
25
+ /**
26
+ * Interface for asserting the state of a sequence under testing with a {@code test()}
27
+ * method of a reactive base class.
28
+ * <p>
29
+ * This interface is not intended to be implemented outside of RxJava.
30
+ * <p>
31
+ * This interface extends {@link Observer} and allows injecting onXXX signals into
32
+ * the testing process.
33
+ * @param <T> the value type consumed by this Observer
34
+ * @since 1.2.3
35
+ */
36
+ @ Experimental
37
+ public interface AssertableSubscriber <T > extends Observer <T >, Subscription {
26
38
39
+ /**
40
+ * Allows manually calling the {@code onStart} method of the underlying Subscriber.
41
+ */
27
42
void onStart ();
28
43
44
+ /**
45
+ * Allows manually calling the {@code setProducer} method of the underlying Subscriber.
46
+ * @param p the producer to use, not null
47
+ */
29
48
void setProducer (Producer p );
30
49
50
+ /**
51
+ * Returns the number of {@code onCompleted} signals received by this Observer.
52
+ * @return the number of {@code onCompleted} signals received
53
+ */
31
54
int getCompletions ();
32
55
56
+ /**
57
+ * Returns a list of received {@code onError} signals.
58
+ * @return this
59
+ */
33
60
List <Throwable > getOnErrorEvents ();
34
61
62
+ /**
63
+ * Returns the number of {@code onNext} signals received by this Observer in
64
+ * a thread-safe manner; one can read up to this number of elements from
65
+ * the {@code List} returned by {@link #getOnNextEvents()}.
66
+ * @return the number of {@code onNext} signals received.
67
+ */
35
68
int getValueCount ();
36
69
70
+ /**
71
+ * Requests the specified amount of items from upstream.
72
+ * @param n the amount requested, non-negative
73
+ * @return this
74
+ */
37
75
AssertableSubscriber <T > requestMore (long n );
38
76
77
+ /**
78
+ * Returns the list of received {@code onNext} events.
79
+ * <p>If the sequence hasn't completed yet and is asynchronous, use the
80
+ * {@link #getValueCount()} method to determine how many elements are safe
81
+ * to be read from the list returned by this method.
82
+ * @return the List of received {@code onNext} events.
83
+ */
39
84
List <T > getOnNextEvents ();
40
85
86
+ /**
87
+ * Assert that this Observer received the given list of items as {@code onNext} signals
88
+ * in the same order and with the default null-safe object equals comparison.
89
+ * @param items the List of items expected
90
+ * @return this
91
+ */
41
92
AssertableSubscriber <T > assertReceivedOnNext (List <T > items );
42
93
43
- boolean awaitValueCount (int expected , long timeout , TimeUnit unit );
94
+ /**
95
+ * Assert that this Observer receives at least the given number of {@code onNext}
96
+ * signals within the specified timeout period.
97
+ * <p>
98
+ * Note that it is possible the AssertionError thrown by this method will
99
+ * contain an actual value >= to the expected one in case there is an emission
100
+ * race or unexpected delay on the emitter side. In this case, increase the timeout
101
+ * amount to avoid false positives.
102
+ *
103
+ * @param expected the expected (at least) number of {@code onNext} signals
104
+ * @param timeout the timeout to wait to receive the given number of {@code onNext} events
105
+ * @param unit the time unit
106
+ * @return this
107
+ */
108
+ AssertableSubscriber <T > awaitValueCount (int expected , long timeout , TimeUnit unit );
44
109
110
+ /**
111
+ * Assert that this Observer received either an {@code onError} or {@code onCompleted} signal.
112
+ * @return this
113
+ */
45
114
AssertableSubscriber <T > assertTerminalEvent ();
46
115
116
+ /**
117
+ * Assert that this Observer has been unsubscribed via {@code unsubscribe()} or by a wrapping
118
+ * {@code SafeSubscriber}.
119
+ * @return this
120
+ */
47
121
AssertableSubscriber <T > assertUnsubscribed ();
48
122
123
+ /**
124
+ * Assert that this Observer has not received any {@code onError} signal.
125
+ * @return this
126
+ */
49
127
AssertableSubscriber <T > assertNoErrors ();
50
128
129
+ /**
130
+ * Waits for an {@code onError} or {code onCompleted} terminal event indefinitely.
131
+ * @return this
132
+ */
51
133
AssertableSubscriber <T > awaitTerminalEvent ();
52
134
135
+
136
+ /**
137
+ * Waits for an {@code onError} or {code onCompleted} terminal event for the given
138
+ * amount of timeout.
139
+ * @param timeout the time to wait for the terminal event
140
+ * @param unit the time unit of the wait time
141
+ * @return this
142
+ */
53
143
AssertableSubscriber <T > awaitTerminalEvent (long timeout , TimeUnit unit );
54
144
145
+ /**
146
+ * Waits for an {@code onError} or {code onCompleted} terminal event for the given
147
+ * amount of timeout and unsubscribes the sequence if the timeout passed or the
148
+ * wait itself is interrupted.
149
+ * @param timeout the time to wait for the terminal event
150
+ * @param unit the time unit of the wait time
151
+ * @return this
152
+ */
55
153
AssertableSubscriber <T > awaitTerminalEventAndUnsubscribeOnTimeout (long timeout ,
56
154
TimeUnit unit );
57
155
156
+ /**
157
+ * Returns the Thread that has called the last {@code onNext}, {@code onError} or
158
+ * {@code onCompleted} methods of this Observer.
159
+ * @return this
160
+ */
58
161
Thread getLastSeenThread ();
59
162
163
+ /**
164
+ * Assert that this Observer received exaclty one {@code onCompleted} signal.
165
+ * @return this
166
+ */
60
167
AssertableSubscriber <T > assertCompleted ();
61
168
169
+ /**
170
+ * Assert that this Observer received no {@code onCompleted} signal.
171
+ * @return this
172
+ */
62
173
AssertableSubscriber <T > assertNotCompleted ();
63
174
175
+ /**
176
+ * Assert that this Observer received one {@code onError} signal with
177
+ * the given subclass of a Throwable as type.
178
+ * @param clazz the expected type of the {@code onError} signal received
179
+ * @return this
180
+ */
64
181
AssertableSubscriber <T > assertError (Class <? extends Throwable > clazz );
65
182
183
+ /**
184
+ * Assert that this Observer received one {@code onError} signal with the
185
+ * object-equals of the given Throwable instance
186
+ * @param throwable the Throwable instance expected
187
+ * @return this
188
+ */
66
189
AssertableSubscriber <T > assertError (Throwable throwable );
67
190
191
+ /**
192
+ * Assert that no {@code onError} or {@code onCompleted} signals were received so far.
193
+ * @return this
194
+ */
68
195
AssertableSubscriber <T > assertNoTerminalEvent ();
69
196
197
+ /**
198
+ * Assert that no {@code onNext} signals were received so far.
199
+ * @return this
200
+ */
70
201
AssertableSubscriber <T > assertNoValues ();
71
202
203
+ /**
204
+ * Assert that this Observer received exactly the given count of
205
+ * {@code onNext} signals.
206
+ * @param count the expected number of {@code onNext} signals
207
+ * @return this
208
+ */
72
209
AssertableSubscriber <T > assertValueCount (int count );
73
210
211
+ /**
212
+ * Assert that this Observer received exactly the given expected values
213
+ * (compared via null-safe object equals) in the given order.
214
+ * @param values the expected values
215
+ * @return this
216
+ */
74
217
AssertableSubscriber <T > assertValues (T ... values );
75
218
219
+ /**
220
+ * Assert that this Observer received exactly the given single expected value
221
+ * (compared via null-safe object equals).
222
+ * @param value the single value expected
223
+ * @return this
224
+ */
76
225
AssertableSubscriber <T > assertValue (T value );
77
226
227
+ /**
228
+ * Assert that this Observer received exactly the given values (compared via
229
+ * null-safe object equals) and if so, clears the internal buffer of the
230
+ * underlying Subscriber of these values.
231
+ * @param expectedFirstValue the first value expected
232
+ * @param expectedRestValues the rest of the values expected
233
+ * @return this
234
+ */
78
235
AssertableSubscriber <T > assertValuesAndClear (T expectedFirstValue ,
79
236
T ... expectedRestValues );
80
237
238
+ /**
239
+ * Performs an action given by the Action0 callback in a fluent manner.
240
+ * @param action the action to perform, not null
241
+ * @return this
242
+ */
81
243
AssertableSubscriber <T > perform (Action0 action );
82
-
244
+
245
+ @ Override
83
246
void unsubscribe ();
84
-
247
+
248
+ @ Override
85
249
boolean isUnsubscribed ();
86
250
251
+ /**
252
+ * Assert that this Observer received the specified items in the given order followed
253
+ * by a completion signal and no errors.
254
+ * @param values the values expected
255
+ * @return this
256
+ */
257
+ AssertableSubscriber <T > assertResult (T ... values );
258
+
259
+ /**
260
+ * Assert that this Observer received the specified items in the given order followed
261
+ * by an error signal of the given type (but no completion signal).
262
+ * @param errorClass the expected Throwable subclass type
263
+ * @param values the expected values
264
+ * @return this
265
+ */
266
+ AssertableSubscriber <T > assertFailure (Class <? extends Throwable > errorClass , T ... values );
267
+
268
+ /**
269
+ * Assert that this Observer received the specified items in the given order followed
270
+ * by an error signal of the given type and with the exact error message (but no completion signal).
271
+ * @param errorClass the expected Throwable subclass type
272
+ * @param message the expected error message returned by {@link Throwable#getMessage()}
273
+ * @param values the expected values
274
+ * @return this
275
+ */
276
+ AssertableSubscriber <T > assertFailureAndMessage (Class <? extends Throwable > errorClass , String message , T ... values );
87
277
}
0 commit comments