|
16 | 16 |
|
17 | 17 | package com.linecorp.armeria.server;
|
18 | 18 |
|
19 |
| -import static org.assertj.core.api.Assertions.assertThat; |
20 |
| -import static org.awaitility.Awaitility.await; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.reset; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.when; |
21 | 24 |
|
22 |
| -import java.time.Duration; |
| 25 | +import java.nio.channels.ClosedChannelException; |
23 | 26 |
|
24 |
| -import org.junit.jupiter.api.AfterEach; |
25 | 27 | import org.junit.jupiter.api.BeforeEach;
|
26 | 28 | import org.junit.jupiter.api.Test;
|
27 |
| -import org.mockito.Spy; |
28 |
| -import org.slf4j.LoggerFactory; |
29 |
| - |
30 |
| -import com.linecorp.armeria.client.BlockingWebClient; |
31 |
| -import com.linecorp.armeria.common.HttpResponse; |
32 |
| -import com.linecorp.armeria.common.HttpStatus; |
33 |
| -import com.linecorp.armeria.server.logging.LoggingService; |
34 |
| - |
35 |
| -import ch.qos.logback.classic.Logger; |
36 |
| -import ch.qos.logback.classic.spi.ILoggingEvent; |
37 |
| -import ch.qos.logback.core.read.ListAppender; |
| 29 | +import org.mockito.Mock; |
38 | 30 |
|
39 | 31 | class ExceptionReportingServiceErrorHandlerTest {
|
40 | 32 |
|
41 |
| - @Spy |
42 |
| - final ListAppender<ILoggingEvent> logAppender = new ListAppender<>(); |
43 |
| - final Logger errorHandlerLogger = |
44 |
| - (Logger) LoggerFactory.getLogger(DefaultUnhandledExceptionsReporter.class); |
45 |
| - private static final long reportIntervalMillis = 1000; |
46 |
| - private static final long awaitIntervalMillis = 2000; |
| 33 | + @Mock |
| 34 | + private ServiceErrorHandler delegate; |
| 35 | + @Mock |
| 36 | + private UnhandledExceptionsReporter reporter; |
| 37 | + @Mock |
| 38 | + private ServiceRequestContext ctx; |
| 39 | + |
| 40 | + private ExceptionReportingServiceErrorHandler serviceErrorHandler; |
47 | 41 |
|
48 | 42 | @BeforeEach
|
49 |
| - public void attachAppender() { |
50 |
| - logAppender.start(); |
51 |
| - errorHandlerLogger.addAppender(logAppender); |
| 43 | + void setUp() { |
| 44 | + reset(reporter); |
| 45 | + reset(ctx); |
| 46 | + serviceErrorHandler = new ExceptionReportingServiceErrorHandler(delegate, reporter); |
52 | 47 | }
|
53 | 48 |
|
54 |
| - @AfterEach |
55 |
| - public void detachAppender() { |
56 |
| - errorHandlerLogger.detachAppender(logAppender); |
57 |
| - logAppender.list.clear(); |
| 49 | + @Test |
| 50 | + void onServiceExceptionShouldNotReportWhenShouldReportUnhandledExceptionsIsFalse() { |
| 51 | + when(ctx.shouldReportUnhandledExceptions()).thenReturn(false); |
| 52 | + serviceErrorHandler.onServiceException(ctx, new IllegalArgumentException("Test")); |
| 53 | + verify(reporter, times(0)).report(any()); |
58 | 54 | }
|
59 | 55 |
|
60 | 56 | @Test
|
61 |
| - void httpStatusExceptionWithCauseLogged() throws Exception { |
62 |
| - final Server server = Server.builder() |
63 |
| - .service("/hello", (ctx, req) -> { |
64 |
| - throw HttpStatusException.of(HttpStatus.BAD_REQUEST, |
65 |
| - new IllegalArgumentException("test")); |
66 |
| - }) |
67 |
| - .unhandledExceptionsReportInterval(Duration.ofMillis(reportIntervalMillis)) |
68 |
| - .build(); |
69 |
| - try { |
70 |
| - server.start().join(); |
71 |
| - BlockingWebClient.of("http://127.0.0.1:" + server.activeLocalPort()).get("/hello"); |
72 |
| - await().atMost(Duration.ofMillis(reportIntervalMillis + awaitIntervalMillis)) |
73 |
| - .untilAsserted(() -> assertThat(logAppender.list).isNotEmpty()); |
74 |
| - |
75 |
| - assertThat(logAppender.list |
76 |
| - .stream() |
77 |
| - .filter(event -> event.getFormattedMessage().contains( |
78 |
| - "Observed 1 unhandled exceptions")) |
79 |
| - .findAny() |
80 |
| - ).isNotEmpty(); |
81 |
| - } finally { |
82 |
| - server.stop(); |
83 |
| - } |
| 57 | + void onServiceExceptionShouldNotReportWhenCauseIsExpected() { |
| 58 | + when(ctx.shouldReportUnhandledExceptions()).thenReturn(true); |
| 59 | + serviceErrorHandler.onServiceException(ctx, new ClosedChannelException()); |
| 60 | + verify(reporter, times(0)).report(any()); |
84 | 61 | }
|
85 | 62 |
|
86 | 63 | @Test
|
87 |
| - void httpStatusExceptionWithoutCauseIsIgnored() throws Exception { |
88 |
| - final Server server = Server.builder() |
89 |
| - .service("/hello", (ctx, req) -> { |
90 |
| - throw HttpStatusException.of(HttpStatus.BAD_REQUEST); |
91 |
| - }) |
92 |
| - .unhandledExceptionsReportInterval(Duration.ofMillis(reportIntervalMillis)) |
93 |
| - .build(); |
94 |
| - try { |
95 |
| - server.start().join(); |
96 |
| - BlockingWebClient.of("http://127.0.0.1:" + server.activeLocalPort()).get("/hello"); |
97 |
| - Thread.sleep(reportIntervalMillis + awaitIntervalMillis); |
98 |
| - |
99 |
| - assertThat(logAppender.list).isEmpty(); |
100 |
| - } finally { |
101 |
| - server.stop(); |
102 |
| - } |
| 64 | + void onServiceExceptionShouldReportWhenCauseIsNotExpected() { |
| 65 | + when(ctx.shouldReportUnhandledExceptions()).thenReturn(true); |
| 66 | + serviceErrorHandler.onServiceException(ctx, new IllegalArgumentException("Test")); |
| 67 | + verify(reporter, times(1)).report(any()); |
103 | 68 | }
|
104 | 69 |
|
105 | 70 | @Test
|
106 |
| - void exceptionShouldNotBeLoggedWhenDecoratedWithLoggingService() throws Exception { |
107 |
| - final Server server = Server.builder() |
108 |
| - .service("/hello", (ctx, req) -> { |
109 |
| - throw new IllegalArgumentException("test"); |
110 |
| - }) |
111 |
| - .unhandledExceptionsReportInterval(Duration.ofMillis(reportIntervalMillis)) |
112 |
| - .decorator(LoggingService.newDecorator()) |
113 |
| - .build(); |
114 |
| - |
115 |
| - try { |
116 |
| - server.start().join(); |
117 |
| - BlockingWebClient.of("http://127.0.0.1:" + server.activeLocalPort()).get("/hello"); |
118 |
| - Thread.sleep(reportIntervalMillis + awaitIntervalMillis); |
119 |
| - |
120 |
| - assertThat(logAppender.list).isEmpty(); |
121 |
| - } finally { |
122 |
| - server.stop(); |
123 |
| - } |
| 71 | + void onServiceExceptionShouldNotReportWhenCauseIsHttpStatusExceptionAndCauseNull() { |
| 72 | + when(ctx.shouldReportUnhandledExceptions()).thenReturn(true); |
| 73 | + serviceErrorHandler.onServiceException(ctx, HttpStatusException.of(500)); |
| 74 | + verify(reporter, times(0)).report(any()); |
124 | 75 | }
|
125 | 76 |
|
126 | 77 | @Test
|
127 |
| - void exceptionShouldNotBeLoggedWhenNoExceptionIsThrown() throws Exception { |
128 |
| - final Server server = Server.builder() |
129 |
| - .service("/hello", (ctx, req) -> HttpResponse.of(HttpStatus.OK)) |
130 |
| - .decorator(LoggingService.newDecorator()) |
131 |
| - .build(); |
132 |
| - try { |
133 |
| - server.start().join(); |
134 |
| - BlockingWebClient.of("http://127.0.0.1:" + server.activeLocalPort()).get("/hello"); |
135 |
| - Thread.sleep(reportIntervalMillis + awaitIntervalMillis); |
136 |
| - |
137 |
| - assertThat(logAppender.list).isEmpty(); |
138 |
| - } finally { |
139 |
| - server.stop(); |
140 |
| - } |
| 78 | + void onServiceExceptionShouldReportWhenCauseIsHttpStatusExceptionAndCauseNonNull() { |
| 79 | + when(ctx.shouldReportUnhandledExceptions()).thenReturn(true); |
| 80 | + serviceErrorHandler.onServiceException( |
| 81 | + ctx, HttpStatusException.of(500, new IllegalArgumentException("test"))); |
| 82 | + verify(reporter, times(1)).report(any()); |
141 | 83 | }
|
142 | 84 | }
|
0 commit comments