forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExceptionsTest.java
516 lines (414 loc) · 15.7 KB
/
ExceptionsTest.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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.exceptions;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.*;
import io.reactivex.internal.util.ExceptionHelper;
import io.reactivex.observables.GroupedObservable;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.subjects.PublishSubject;
public class ExceptionsTest {
@Ignore("Exceptions is not an enum")
@Test
public void constructorShouldBePrivate() {
TestHelper.checkUtilityClass(ExceptionHelper.class);
}
@Test
public void testOnErrorNotImplementedIsThrown() {
List<Throwable> errors = TestHelper.trackPluginErrors();
Observable.just(1, 2, 3).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer t1) {
throw new RuntimeException("hello");
}
});
TestHelper.assertError(errors, 0, RuntimeException.class, "hello");
RxJavaPlugins.reset();
}
/**
* https://github.com/ReactiveX/RxJava/issues/3885
*/
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnCompletedExceptionIsThrown() {
Observable.empty()
.subscribe(new Observer<Object>() {
@Override
public void onComplete() {
throw new RuntimeException();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Object o) {
}
@Override
public void onSubscribe(Disposable d) {
}
});
}
@Test
public void testStackOverflowWouldOccur() {
final PublishSubject<Integer> a = PublishSubject.create();
final PublishSubject<Integer> b = PublishSubject.create();
final int MAX_STACK_DEPTH = 800;
final AtomicInteger depth = new AtomicInteger();
a.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer n) {
b.onNext(n + 1);
}
});
b.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
// TODO Auto-generated method stub
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer n) {
if (depth.get() < MAX_STACK_DEPTH) {
depth.set(Thread.currentThread().getStackTrace().length);
a.onNext(n + 1);
}
}
});
a.onNext(1);
assertTrue(depth.get() >= MAX_STACK_DEPTH);
}
@Test(expected = StackOverflowError.class)
public void testStackOverflowErrorIsThrown() {
Observable.just(1).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer t) {
throw new StackOverflowError();
}
});
}
@Test(expected = ThreadDeath.class)
public void testThreadDeathIsThrown() {
Observable.just(1).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Integer t) {
throw new ThreadDeath();
}
});
}
/**
* https://github.com/ReactiveX/RxJava/issues/969
*/
@Ignore("v2 components should not throw")
@Test
public void testOnErrorExceptionIsThrown() {
try {
Observable.error(new IllegalArgumentException("original exception")).subscribe(new Observer<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
throw new IllegalStateException("This should be thrown");
}
@Override
public void onNext(Object o) {
}
});
fail("expecting an exception to be thrown");
} catch (RuntimeException t) {
CompositeException cause = (CompositeException) t.getCause();
assertTrue(cause.getExceptions().get(0) instanceof IllegalArgumentException);
assertTrue(cause.getExceptions().get(1) instanceof IllegalStateException);
}
}
/**
* https://github.com/ReactiveX/RxJava/issues/2998
* @throws Exception on arbitrary errors
*/
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromGroupBy() throws Exception {
Observable
.just(1)
.groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
throw new RuntimeException();
}
})
.subscribe(new Observer<GroupedObservable<Integer, Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
throw new RuntimeException();
}
@Override
public void onNext(GroupedObservable<Integer, Integer> integerIntegerGroupedObservable) {
}
});
}
/**
* https://github.com/ReactiveX/RxJava/issues/2998
* @throws Exception on arbitrary errors
*/
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromOnNext() throws Exception {
Observable
.just(1)
.doOnNext(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
throw new RuntimeException();
}
})
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
throw new RuntimeException();
}
@Override
public void onNext(Integer integer) {
}
});
}
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromSubscribe() {
Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> s1) {
Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> s2) {
throw new IllegalArgumentException("original exception");
}
}).subscribe(s1);
}
}
).subscribe(new OnErrorFailedSubscriber());
}
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromUnsafeSubscribe() {
Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> s1) {
Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> s2) {
throw new IllegalArgumentException("original exception");
}
}).subscribe(s1);
}
}
).subscribe(new OnErrorFailedSubscriber());
}
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromSingleDoOnSuccess() throws Exception {
Single.just(1)
.doOnSuccess(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
throw new RuntimeException();
}
})
.toObservable().subscribe(new OnErrorFailedSubscriber());
}
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromSingleSubscribe() {
Single.unsafeCreate(new SingleSource<Integer>() {
@Override
public void subscribe(SingleObserver<? super Integer> s1) {
Single.unsafeCreate(new SingleSource<Integer>() {
@Override
public void subscribe(SingleObserver<? super Integer> s2) {
throw new IllegalArgumentException("original exception");
}
}).subscribe(s1);
}
}
).toObservable().subscribe(new OnErrorFailedSubscriber());
}
@Ignore("v2 components should not throw")
@Test(expected = RuntimeException.class)
public void testOnErrorExceptionIsThrownFromSingleUnsafeSubscribe() {
Single.unsafeCreate(new SingleSource<Integer>() {
@Override
public void subscribe(final SingleObserver<? super Integer> s1) {
Single.unsafeCreate(new SingleSource<Integer>() {
@Override
public void subscribe(SingleObserver<? super Integer> s2) {
throw new IllegalArgumentException("original exception");
}
}).toFlowable().subscribe(new FlowableSubscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
s1.onError(e);
}
@Override
public void onNext(Integer v) {
s1.onSuccess(v);
}
});
}
}
).toObservable().subscribe(new OnErrorFailedSubscriber());
}
private class OnErrorFailedSubscriber implements Observer<Integer> {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
throw new RuntimeException();
}
@Override
public void onNext(Integer value) {
}
}
@Test
public void utilityClass() {
TestHelper.checkUtilityClass(Exceptions.class);
}
@Test
public void manualThrowIfFatal() {
try {
Exceptions.throwIfFatal(new ThreadDeath());
fail("Didn't throw fatal exception");
} catch (ThreadDeath ex) {
// expected
}
try {
Exceptions.throwIfFatal(new LinkageError());
fail("Didn't throw fatal error");
} catch (LinkageError ex) {
// expected
}
try {
ExceptionHelper.wrapOrThrow(new LinkageError());
fail("Didn't propagate Error");
} catch (LinkageError ex) {
// expected
}
}
@Test
public void manualPropagate() {
try {
Exceptions.propagate(new InternalError());
fail("Didn't throw exception");
} catch (InternalError ex) {
// expected
}
try {
throw Exceptions.propagate(new IllegalArgumentException());
} catch (IllegalArgumentException ex) {
// expected
}
try {
throw ExceptionHelper.wrapOrThrow(new IOException());
} catch (RuntimeException ex) {
if (!(ex.getCause() instanceof IOException)) {
fail(ex.toString() + ": should have thrown RuntimeException(IOException)");
}
}
}
@Test
public void errorNotImplementedNull1() {
OnErrorNotImplementedException ex = new OnErrorNotImplementedException(null);
assertTrue("" + ex.getCause(), ex.getCause() instanceof NullPointerException);
}
@Test
public void errorNotImplementedNull2() {
OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", null);
assertTrue("" + ex.getCause(), ex.getCause() instanceof NullPointerException);
}
@Test
public void errorNotImplementedWithCause() {
OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", new TestException("Forced failure"));
assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException);
assertEquals("" + ex.getCause(), "Forced failure", ex.getCause().getMessage());
}
}