forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestHelper.java
253 lines (213 loc) · 7.95 KB
/
TestHelper.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* 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 io.reactivex;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.reactivestreams.*;
import io.reactivex.exceptions.CompositeException;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.util.ExceptionHelper;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Common methods for helping with tests from 1.x mostly.
*/
public enum TestHelper {
;
/**
* Mocks a subscriber and prepares it to request Long.MAX_VALUE.
* @param <T> the value type
* @return the mocked subscriber
*/
@SuppressWarnings("unchecked")
public static <T> Subscriber<T> mockSubscriber() {
Subscriber<T> w = mock(Subscriber.class);
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock a) throws Throwable {
Subscription s = a.getArgumentAt(0, Subscription.class);
s.request(Long.MAX_VALUE);
return null;
}
}).when(w).onSubscribe((Subscription)any());
return w;
}
/**
* Mocks an Observer with the proper receiver type.
* @param <T> the value type
* @return the mocked observer
*/
@SuppressWarnings("unchecked")
public static <T> Observer<T> mockObserver() {
return mock(Observer.class);
}
/**
* Validates that the given class, when forcefully instantiated throws
* an IllegalArgumentException("No instances!") exception.
* @param clazz the class to test, not null
*/
public static void checkUtilityClass(Class<?> clazz) {
try {
Constructor<?> c = clazz.getDeclaredConstructor();
c.setAccessible(true);
try {
c.newInstance();
fail("Should have thrown InvocationTargetException(IllegalStateException)");
} catch (InvocationTargetException ex) {
assertEquals("No instances!", ex.getCause().getMessage());
}
} catch (Exception ex) {
AssertionError ae = new AssertionError(ex.toString());
ae.initCause(ex);
throw ae;
}
}
public static List<Throwable> trackPluginErrors() {
final List<Throwable> list = Collections.synchronizedList(new ArrayList<Throwable>());
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
list.add(t);
}
});
return list;
}
public static void assertError(List<Throwable> list, int index, Class<? extends Throwable> clazz, String message) {
assertTrue(list.get(index).toString(), clazz.isInstance(list.get(index)));
assertEquals(message, list.get(index).getMessage());
}
/**
* Verify that a specific enum type has no enum constants.
* @param <E> the enum type
* @param e the enum class instance
*/
public static <E extends Enum<E>> void assertEmptyEnum(Class<E> e) {
assertEquals(0, e.getEnumConstants().length);
try {
Method m = e.getDeclaredMethod("valueOf", String.class);
try {
m.invoke("INSTANCE");
fail("Should have thrown!");
} catch (InvocationTargetException ex) {
fail(ex.toString());
} catch (IllegalAccessException ex) {
fail(ex.toString());
} catch (IllegalArgumentException ex) {
// we expected this
}
} catch (NoSuchMethodException ex) {
fail(ex.toString());
}
}
public static void assertBadRequestReported(Publisher<?> source) {
List<Throwable> list = trackPluginErrors();
try {
final CountDownLatch cdl = new CountDownLatch(1);
source.subscribe(new Subscriber<Object>() {
@Override
public void onSubscribe(Subscription s) {
try {
s.request(-99);
s.cancel();
s.cancel();
} finally {
cdl.countDown();
}
}
@Override
public void onNext(Object t) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
try {
assertTrue(cdl.await(5, TimeUnit.SECONDS));
} catch (InterruptedException ex) {
throw new AssertionError(ex.getMessage());
}
assertTrue(list.toString(), list.get(0) instanceof IllegalArgumentException);
assertEquals("n > 0 required but it was -99", list.get(0).getMessage());
} finally {
RxJavaPlugins.setErrorHandler(null);
}
}
/**
* Synchronizes the execution of two runnables (as much as possible)
* to test race conditions.
* <p>The method blocks until both have run to completion.
* @param r1 the first runnable
* @param r2 the second runnable
* @param s the scheduler to use
*/
public static void race(final Runnable r1, final Runnable r2, Scheduler s) {
final AtomicInteger count = new AtomicInteger(2);
final CountDownLatch cdl = new CountDownLatch(2);
final Throwable[] errors = { null, null };
s.scheduleDirect(new Runnable() {
@Override
public void run() {
if (count.decrementAndGet() != 0) {
while (count.get() != 0);
}
try {
try {
r1.run();
} catch (Throwable ex) {
errors[0] = ex;
}
} finally {
cdl.countDown();
}
}
});
if (count.decrementAndGet() != 0) {
while (count.get() != 0);
}
try {
try {
r2.run();
} catch (Throwable ex) {
errors[1] = ex;
}
} finally {
cdl.countDown();
}
try {
if (!cdl.await(5, TimeUnit.SECONDS)) {
throw new AssertionError("The wait timed out!");
}
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
if (errors[0] != null && errors[1] == null) {
throw ExceptionHelper.wrapOrThrow(errors[0]);
}
if (errors[0] == null && errors[1] != null) {
throw ExceptionHelper.wrapOrThrow(errors[1]);
}
if (errors[0] != null && errors[1] != null) {
throw new CompositeException(errors);
}
}
}