ServiceUnavailableHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) { if (r instanceof ServerRunnable) { Socket socket = ((ServerRunnable) r).getSocket(); try { (new HttpError503Handler()).serve(responseFactory.createFromSocket(socket)); } catch (IOException e) { } finally { IOUtilities.closeSilently(socket); } } } ServiceUnavailableHandler(final HttpServletResponseImplFactory responseFactory); @Override void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor);  }
@Test public void shouldIgnoreRunnableThatIsNotServerRunnable() throws Exception { serviceUnavailableHandler.rejectedExecution(mock(Runnable.class), null); verify(factory, never()).createFromSocket(any(Socket.class)); }
@Test public void shouldHandleServerRunnable() throws Exception { ServerRunnable runnable = new ServerRunnable(mock(Socket.class), mock(ServerConfig.class), mock(HttpServletRequestImplFactory.class), factory, mock(HttpErrorHandlerResolver.class), mock(PathHelper.class)); serviceUnavailableHandler.rejectedExecution(runnable, null); verify(factory, times(1)).createFromSocket(any(Socket.class)); printWriter.flush(); assertThat(outputStream.toString(), containsString("503")); }