|
23 | 23 | import static org.junit.Assert.assertFalse; |
24 | 24 | import static org.junit.Assert.assertNotNull; |
25 | 25 | import static org.junit.Assert.assertTrue; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.when; |
26 | 28 |
|
27 | 29 | import java.io.ByteArrayOutputStream; |
28 | 30 | import java.io.IOException; |
|
35 | 37 | import java.net.HttpURLConnection; |
36 | 38 | import java.net.URI; |
37 | 39 | import java.net.URL; |
| 40 | +import java.net.SocketTimeoutException; |
| 41 | +import java.util.Collections; |
38 | 42 | import java.util.Enumeration; |
39 | 43 | import java.util.List; |
40 | 44 | import java.util.Map; |
41 | 45 |
|
| 46 | +import javax.servlet.ServletConfig; |
| 47 | +import javax.servlet.ServletContext; |
42 | 48 | import javax.servlet.ServletException; |
43 | 49 | import javax.servlet.http.HttpServlet; |
44 | 50 | import javax.servlet.http.HttpServletRequest; |
@@ -98,6 +104,7 @@ public static void start() throws Exception { |
98 | 104 | context.setContextPath("/foo"); |
99 | 105 | server.setHandler(context); |
100 | 106 | context.addServlet(new ServletHolder(TestServlet.class), "/bar"); |
| 107 | + context.addServlet(new ServletHolder(TimeOutTestServlet.class), "/timeout"); |
101 | 108 | ((ServerConnector)server.getConnectors()[0]).setHost("localhost"); |
102 | 109 | server.start(); |
103 | 110 | originalPort = ((ServerConnector)server.getConnectors()[0]).getLocalPort(); |
@@ -145,6 +152,29 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
145 | 152 | } |
146 | 153 | } |
147 | 154 |
|
| 155 | + @SuppressWarnings("serial") |
| 156 | + public static class TimeOutTestServlet extends HttpServlet { |
| 157 | + |
| 158 | + @Override |
| 159 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 160 | + throws ServletException, IOException { |
| 161 | + try { |
| 162 | + Thread.sleep(10 * 1000); |
| 163 | + } catch (InterruptedException e) { |
| 164 | + LOG.warn("doGet() interrupted", e); |
| 165 | + resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 166 | + return; |
| 167 | + } |
| 168 | + resp.setStatus(HttpServletResponse.SC_OK); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| 173 | + throws ServletException, IOException { |
| 174 | + resp.setStatus(HttpServletResponse.SC_OK); |
| 175 | + } |
| 176 | + } |
| 177 | + |
148 | 178 | @Test(timeout=5000) |
149 | 179 | public void testWebAppProxyServlet() throws Exception { |
150 | 180 | configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090"); |
@@ -256,6 +286,45 @@ public void testWebAppProxyServlet() throws Exception { |
256 | 286 | } |
257 | 287 | } |
258 | 288 |
|
| 289 | + @Test(expected = SocketTimeoutException.class) |
| 290 | + public void testWebAppProxyConnectionTimeout() |
| 291 | + throws IOException, ServletException{ |
| 292 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 293 | + when(request.getMethod()).thenReturn("GET"); |
| 294 | + when(request.getRemoteUser()).thenReturn("dr.who"); |
| 295 | + when(request.getPathInfo()).thenReturn("/application_00_0"); |
| 296 | + when(request.getHeaderNames()).thenReturn(Collections.emptyEnumeration()); |
| 297 | + |
| 298 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 299 | + when(response.getOutputStream()).thenReturn(null); |
| 300 | + |
| 301 | + WebAppProxyServlet servlet = new WebAppProxyServlet(); |
| 302 | + YarnConfiguration conf = new YarnConfiguration(); |
| 303 | + conf.setBoolean(YarnConfiguration.RM_PROXY_TIMEOUT_ENABLED, |
| 304 | + true); |
| 305 | + conf.setInt(YarnConfiguration.RM_PROXY_CONNECTION_TIMEOUT, |
| 306 | + 1000); |
| 307 | + |
| 308 | + servlet.setConf(conf); |
| 309 | + |
| 310 | + ServletConfig config = mock(ServletConfig.class); |
| 311 | + ServletContext context = mock(ServletContext.class); |
| 312 | + when(config.getServletContext()).thenReturn(context); |
| 313 | + |
| 314 | + AppReportFetcherForTest appReportFetcher = |
| 315 | + new AppReportFetcherForTest(new YarnConfiguration()); |
| 316 | + |
| 317 | + when(config.getServletContext() |
| 318 | + .getAttribute(WebAppProxy.FETCHER_ATTRIBUTE)) |
| 319 | + .thenReturn(appReportFetcher); |
| 320 | + |
| 321 | + appReportFetcher.answer = 7; |
| 322 | + |
| 323 | + servlet.init(config); |
| 324 | + servlet.doGet(request, response); |
| 325 | + |
| 326 | + } |
| 327 | + |
259 | 328 | @Test(timeout=5000) |
260 | 329 | public void testAppReportForEmptyTrackingUrl() throws Exception { |
261 | 330 | configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090"); |
@@ -391,9 +460,9 @@ public void testWebAppProxyServerMainMethod() throws Exception { |
391 | 460 |
|
392 | 461 | @Test(timeout=5000) |
393 | 462 | public void testCheckHttpsStrictAndNotProvided() throws Exception { |
394 | | - HttpServletResponse resp = Mockito.mock(HttpServletResponse.class); |
| 463 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
395 | 464 | StringWriter sw = new StringWriter(); |
396 | | - Mockito.when(resp.getWriter()).thenReturn(new PrintWriter(sw)); |
| 465 | + when(resp.getWriter()).thenReturn(new PrintWriter(sw)); |
397 | 466 | YarnConfiguration conf = new YarnConfiguration(); |
398 | 467 | final URI httpLink = new URI("http://foo.com"); |
399 | 468 | final URI httpsLink = new URI("https://foo.com"); |
@@ -566,6 +635,12 @@ public FetchedAppReport getApplicationReport(ApplicationId appId) |
566 | 635 | return result; |
567 | 636 | } else if (answer == 6) { |
568 | 637 | return getDefaultApplicationReport(appId, false); |
| 638 | + } else if (answer == 7) { |
| 639 | + // test connection timeout |
| 640 | + FetchedAppReport result = getDefaultApplicationReport(appId); |
| 641 | + result.getApplicationReport().setOriginalTrackingUrl("localhost:" |
| 642 | + + originalPort + "/foo/timeout?a=b#main"); |
| 643 | + return result; |
569 | 644 | } |
570 | 645 | return null; |
571 | 646 | } |
|
0 commit comments