/
ExceptionsTest.java
588 lines (483 loc) · 18.3 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* Copyright (c) 2015-2021 VMware Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://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 reactor.core;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.util.RaceTestUtils;
import reactor.util.annotation.Nullable;
import static org.assertj.core.api.Assertions.*;
import static reactor.core.Exceptions.*;
/**
* @author Stephane Maldini
*/
public class ExceptionsTest {
//used for two addThrowableXxx tests lower in the class. each test receiving a separate instance of ExceptionsTests,
//there is no need to reset it.
volatile @Nullable Throwable addThrowable;
static final AtomicReferenceFieldUpdater<ExceptionsTest, Throwable> ADD_THROWABLE =
AtomicReferenceFieldUpdater.newUpdater(ExceptionsTest.class, Throwable.class, "addThrowable");
@Test
public void bubble() throws Exception {
Throwable t = new Exception("test");
Throwable w = Exceptions.bubble(Exceptions.propagate(t));
assertThat(Exceptions.unwrap(w)).isSameAs(t);
}
@Test
public void nullBubble() throws Exception {
Throwable w = Exceptions.bubble(null);
assertThat(Exceptions.unwrap(w)).isSameAs(w);
}
@Test
public void duplicateOnSubscribeReferencesSpec() {
IllegalStateException error = duplicateOnSubscribeException();
assertThat(error).hasMessageContaining("Rule 2.12");
}
@Test
public void duplicateOnSubscribeCreatesNewInstances() {
IllegalStateException error1 = duplicateOnSubscribeException();
IllegalStateException error2 = duplicateOnSubscribeException();
assertThat(error1).isNotSameAs(error2);
}
@Test
public void errorCallbackNotImplementedRejectsNull() {
//noinspection ThrowableNotThrown,ConstantConditions
assertThatNullPointerException().isThrownBy(() -> Exceptions.errorCallbackNotImplemented(null));
}
@Test
public void isErrorCallbackNotImplemented() {
UnsupportedOperationException vanillaUnsupported = new UnsupportedOperationException("not error callback");
UnsupportedOperationException t = errorCallbackNotImplemented(new IllegalStateException("in error callback"));
assertThat(Exceptions.isErrorCallbackNotImplemented(vanillaUnsupported))
.as("not error callback")
.isFalse();
assertThat(Exceptions.isErrorCallbackNotImplemented(t))
.as("error callback")
.isTrue();
}
@Test
public void allOverflowIsIllegalState() {
IllegalStateException overflow1 = Exceptions.failWithOverflow();
IllegalStateException overflow2 = Exceptions.failWithOverflow("foo");
assertThat(Exceptions.isOverflow(overflow1)).isTrue();
assertThat(Exceptions.isOverflow(overflow2)).isTrue();
}
@Test
public void allIllegalStateIsntOverflow() {
IllegalStateException ise = new IllegalStateException("foo");
assertThat(Exceptions.isOverflow(ise)).isFalse();
}
@Test
public void failWithRejectedIsSingleton() {
assertThat(Exceptions.failWithRejected())
.isSameAs(Exceptions.failWithRejected())
.isNotSameAs(Exceptions.failWithRejectedNotTimeCapable());
}
@Test
public void failWithRejectedNotTimeCapableIsSingleton() {
assertThat(Exceptions.failWithRejectedNotTimeCapable())
.isSameAs(Exceptions.failWithRejectedNotTimeCapable())
.isNotSameAs(Exceptions.failWithRejected());
}
@Test
public void isBubbling() {
Throwable notBubbling = new ReactiveException("foo");
Throwable bubbling = new BubblingException("foo");
assertThat(Exceptions.isBubbling(notBubbling)).as("not bubbling").isFalse();
assertThat(Exceptions.isBubbling(bubbling)).as("bubbling").isTrue();
}
@Test
public void isCancelAndCancelIsBubbling() {
Throwable notCancel = new BubblingException("foo");
Throwable cancel = new CancelException();
assertThat(Exceptions.isCancel(notCancel)).as("not cancel").isFalse();
assertThat(Exceptions.isCancel(cancel)).as("cancel").isTrue();
assertThat(Exceptions.isBubbling(cancel)).as("cancel are bubbling").isTrue();
}
@Test
public void nullOrNegativeRequestReferencesSpec() {
assertThat(Exceptions.nullOrNegativeRequestException(-3))
.hasMessage("Spec. Rule 3.9 - Cannot request a non strictly positive number: -3");
}
@Test
public void propagateDoesntWrapRuntimeException() {
Throwable t = new RuntimeException("expected");
assertThat(Exceptions.propagate(t)).isSameAs(t);
}
//TODO test terminate
@Test
void isFatalNotJvmFatalBubbling() {
Throwable exception = new BubblingException("expected");
assertThat(Exceptions.isFatal(exception))
.as("isFatal(bubbling)")
.isTrue();
assertThat(Exceptions.isJvmFatal(exception))
.as("isJvmFatal(bubbling)")
.isFalse();
}
@Test
void isFatalNotJvmFatalErrorCallback() {
Throwable exception = new ErrorCallbackNotImplemented(new IllegalStateException("expected cause"));
assertThat(Exceptions.isFatal(exception))
.as("isFatal(ErrorCallbackNotImplemented)")
.isTrue();
assertThat(Exceptions.isJvmFatal(exception))
.as("isJvmFatal(ErrorCallbackNotImplemented)")
.isFalse();
}
@Test
void isFatalAndJvmFatalVirtualMachineError() {
Throwable exception = new VirtualMachineError() { };
assertThat(Exceptions.isFatal(exception))
.as("isFatal(VirtualMachineError)")
.isTrue();
assertThat(Exceptions.isJvmFatal(exception))
.as("isJvmFatal(VirtualMachineError)")
.isTrue();
}
@Test
void isFatalAndJvmFatalLinkageError() {
Throwable exception = new LinkageError();
assertThat(Exceptions.isFatal(exception))
.as("isFatal(LinkageError)")
.isTrue();
assertThat(Exceptions.isJvmFatal(exception))
.as("isJvmFatal(LinkageError)")
.isTrue();
}
@Test
void isFatalAndJvmFatalThreadDeath() {
Throwable exception = new ThreadDeath();
assertThat(Exceptions.isFatal(exception))
.as("isFatal(ThreadDeath)")
.isTrue();
assertThat(Exceptions.isJvmFatal(exception))
.as("isJvmFatal(ThreadDeath)")
.isTrue();
}
@Test
public void throwIfFatalThrowsBubbling() {
BubblingException expected = new BubblingException("expected to be logged");
assertThatExceptionOfType(BubblingException.class)
.isThrownBy(() -> Exceptions.throwIfFatal(expected))
.isSameAs(expected);
}
@Test
public void throwIfFatalThrowsErrorCallbackNotImplemented() {
ErrorCallbackNotImplemented expected = new ErrorCallbackNotImplemented(new IllegalStateException("expected to be logged"));
assertThatExceptionOfType(ErrorCallbackNotImplemented.class)
.isThrownBy(() -> Exceptions.throwIfFatal(expected))
.isSameAs(expected)
.withCause(expected.getCause());
}
@Test
public void throwIfJvmFatal() {
VirtualMachineError fatal1 = new VirtualMachineError("expected to be logged") {};
ThreadDeath fatal2 = new ThreadDeath() {
@Override
public String getMessage() {
return "expected to be logged";
}
};
LinkageError fatal3 = new LinkageError("expected to be logged");
assertThatExceptionOfType(VirtualMachineError.class)
.as("VirtualMachineError")
.isThrownBy(() -> Exceptions.throwIfJvmFatal(fatal1))
.isSameAs(fatal1);
assertThatExceptionOfType(ThreadDeath.class)
.as("ThreadDeath")
.isThrownBy(() -> Exceptions.throwIfJvmFatal(fatal2))
.isSameAs(fatal2);
assertThatExceptionOfType(LinkageError.class)
.as("LinkageError")
.isThrownBy(() -> Exceptions.throwIfJvmFatal(fatal3))
.isSameAs(fatal3);
}
@Test
public void multipleWithNullVararg() {
//noinspection ConstantConditions
assertThat(Exceptions.multiple((Throwable[]) null))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasNoSuppressedExceptions();
}
@Test
public void multipleWithOneVararg() {
IOException e1 = new IOException("boom");
assertThat(Exceptions.multiple(e1))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasSuppressedException(e1);
}
@Test
public void multipleWithTwoVararg() {
IOException e1 = new IOException("boom");
IllegalArgumentException e2 = new IllegalArgumentException("boom");
assertThat(Exceptions.multiple(e1, e2))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasSuppressedException(e1)
.hasSuppressedException(e2);
}
@Test
public void multipleWithNullIterable() {
//noinspection ConstantConditions
assertThat(Exceptions.multiple((Iterable<Throwable>) null))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasNoSuppressedExceptions();
}
@Test
public void multipleWithEmptyIterable() {
assertThat(Exceptions.multiple(Collections.emptyList()))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasNoSuppressedExceptions();
}
@Test
public void multipleWithIterable() {
IOException e1 = new IOException("boom");
IllegalArgumentException e2 = new IllegalArgumentException("boom");
assertThat(Exceptions.multiple(Arrays.asList(e1, e2)))
.isInstanceOf(RuntimeException.class)
.isExactlyInstanceOf(CompositeException.class)
.hasMessage("Multiple exceptions")
.hasSuppressedException(e1)
.hasSuppressedException(e2);
}
@Test
public void isMultiple() {
Exception e1 = new IllegalStateException("1");
Exception e2 = new IllegalArgumentException("2");
Exception composite = Exceptions.multiple(e1, e2);
assertThat(Exceptions.isMultiple(composite)).isTrue();
assertThat(Exceptions.isMultiple(Exceptions.failWithCancel())).isFalse();
assertThat(Exceptions.isMultiple(null)).isFalse();
}
@Test
public void unwrapMultipleNull() {
assertThat(Exceptions.unwrapMultiple(null))
.isEmpty();
}
@Test
public void unwrapMultipleNotComposite() {
RuntimeException e1 = Exceptions.failWithCancel();
assertThat(Exceptions.unwrapMultiple(e1)).containsExactly(e1);
}
@Test
public void addThrowable() {
Throwable e1 = new IllegalStateException("add1");
Throwable e2 = new IllegalArgumentException("add2");
Throwable e3 = new OutOfMemoryError("add3");
assertThat(addThrowable).isNull();
Exceptions.addThrowable(ADD_THROWABLE, this, e1);
assertThat(addThrowable).isSameAs(e1);
Exceptions.addThrowable(ADD_THROWABLE, this, e2);
assertThat(Exceptions.isMultiple(addThrowable)).isTrue();
assertThat(addThrowable)
.hasSuppressedException(e1)
.hasSuppressedException(e2);
Exceptions.addThrowable(ADD_THROWABLE, this, e3);
assertThat(Exceptions.isMultiple(addThrowable)).isTrue();
assertThat(addThrowable)
.hasSuppressedException(e1)
.hasSuppressedException(e2)
.hasSuppressedException(e3);
}
@Test
public void addThrowableRace() throws Exception {
for (int i = 0; i < 10; i++) {
final int idx = i;
RaceTestUtils.race(
() -> Exceptions.addThrowable(ADD_THROWABLE, ExceptionsTest.this, new IllegalStateException("boomState" + idx)),
() -> Exceptions.addThrowable(ADD_THROWABLE, ExceptionsTest.this, new IllegalArgumentException("boomArg" + idx))
);
}
assertThat(addThrowable.getSuppressed())
.hasSize(20);
}
@Test
public void addSuppressedToNormal() {
Exception original = new Exception("foo");
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isSameAs(original)
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedToRejectedInstance() {
Throwable original = new RejectedExecutionException("foo");
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isSameAs(original)
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedToSame() {
Throwable original = new Exception("foo");
assertThat(Exceptions.addSuppressed(original, original))
.isSameAs(original)
.hasNoSuppressedExceptions();
}
@Test
public void addSuppressedToRejectedSingleton1() {
Throwable original = REJECTED_EXECUTION;
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isNotSameAs(original)
.hasMessage(original.getMessage())
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedToRejectedSingleton2() {
Throwable original = NOT_TIME_CAPABLE_REJECTED_EXECUTION;
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isNotSameAs(original)
.hasMessage(original.getMessage())
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedToTERMINATED() {
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(TERMINATED, suppressed))
.hasNoSuppressedExceptions()
.isSameAs(TERMINATED);
}
@Test
public void addSuppressedRuntimeToNormal() {
RuntimeException original = new RuntimeException("foo");
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isSameAs(original)
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedRuntimeToRejectedInstance() {
RuntimeException original = new RejectedExecutionException("foo");
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isSameAs(original)
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedRuntimeToSame() {
RuntimeException original = new RuntimeException("foo");
assertThat(Exceptions.addSuppressed(original, original))
.isSameAs(original)
.hasNoSuppressedExceptions();
}
@Test
public void addSuppressedRuntimeToRejectedSingleton1() {
RuntimeException original = REJECTED_EXECUTION;
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isNotSameAs(original)
.hasMessage(original.getMessage())
.hasSuppressedException(suppressed);
}
@Test
public void addSuppressedRuntimeToRejectedSingleton2() {
RuntimeException original = NOT_TIME_CAPABLE_REJECTED_EXECUTION;
Exception suppressed = new IllegalStateException("boom");
assertThat(Exceptions.addSuppressed(original, suppressed))
.isNotSameAs(original)
.hasMessage(original.getMessage())
.hasSuppressedException(suppressed);
}
@Test
public void failWithRejectedNormalWraps() {
Throwable test = new IllegalStateException("boom");
assertThat(Exceptions.failWithRejected(test))
.isInstanceOf(Exceptions.ReactorRejectedExecutionException.class)
.hasMessage("Scheduler unavailable")
.hasCause(test);
}
@Test
public void failWithRejectedSingletonREEWraps() {
Throwable test = REJECTED_EXECUTION;
assertThat(Exceptions.failWithRejected(test))
.isInstanceOf(Exceptions.ReactorRejectedExecutionException.class)
.hasMessage("Scheduler unavailable")
.hasCause(test);
}
@Test
public void failWithRejectedNormalREEWraps() {
Throwable test = new RejectedExecutionException("boom");
assertThat(Exceptions.failWithRejected(test))
.isInstanceOf(Exceptions.ReactorRejectedExecutionException.class)
.hasMessage("Scheduler unavailable")
.hasCause(test);
}
@Test
public void failWithRejectedReactorREENoOp() {
Throwable test = new Exceptions.ReactorRejectedExecutionException("boom", REJECTED_EXECUTION);
assertThat(Exceptions.failWithRejected(test))
.isSameAs(test)
.hasCause(REJECTED_EXECUTION);
}
@Test
public void unwrapMultipleExcludingTraceback() {
Mono<String> errorMono1 = Mono.error(new IllegalStateException("expected1"));
Mono<String> errorMono2 = Mono.error(new IllegalStateException("expected2"));
Mono<Throwable> mono = Mono.zipDelayError(errorMono1, errorMono2)
.checkpoint("checkpoint")
.<Throwable>map(tuple -> new IllegalStateException("should have failed: " + tuple))
.onErrorResume(Mono::just);
Throwable compositeException = mono.block();
List<Throwable> exceptions = Exceptions.unwrapMultiple(compositeException);
List<Throwable> filteredExceptions = Exceptions.unwrapMultipleExcludingTracebacks(compositeException);
assertThat(exceptions).as("unfiltered composite has traceback")
.hasSize(3);
assertThat(exceptions).filteredOn(e -> !Exceptions.isTraceback(e))
.as("filtered out tracebacks")
.containsExactlyElementsOf(filteredExceptions)
.hasSize(2)
.hasOnlyElementsOfType(IllegalStateException.class);
}
@Test
public void isRetryExhausted() {
Throwable match1 = Exceptions.retryExhausted("only a message", null);
Throwable match2 = Exceptions.retryExhausted("message and cause", new RuntimeException("cause: boom"));
Throwable noMatch = new IllegalStateException("Retry exhausted: 10/10");
assertThat(Exceptions.isRetryExhausted(null)).as("null").isFalse();
assertThat(Exceptions.isRetryExhausted(match1)).as("match1").isTrue();
assertThat(Exceptions.isRetryExhausted(match2)).as("match2").isTrue();
assertThat(Exceptions.isRetryExhausted(noMatch)).as("noMatch").isFalse();
}
@Test
public void retryExhaustedMessageWithNoCause() {
Throwable retryExhausted = Exceptions.retryExhausted("message with no cause", null);
assertThat(retryExhausted).hasMessage("message with no cause")
.hasNoCause();
}
@Test
public void retryExhaustedMessageWithCause() {
Throwable retryExhausted = Exceptions.retryExhausted("message with cause", new RuntimeException("boom"));
assertThat(retryExhausted).hasMessage("message with cause")
.hasCause(new RuntimeException("boom"));
}
}