@Test public void testPingWebServerNotFoundTwice() throws Exception { when(mockClientHttpResponse.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND); when(mockClientHttpRequest.execute()).thenReturn(mockClientHttpResponse); when(mockHttpRequestFactory.createRequest(any(URI.class), eq(HttpMethod.GET))).thenReturn(mockClientHttpRequest); final Identifier<WebServer> id = new Identifier<>(1L); when(mockWebServer.getId()).thenReturn(id); webServerStateSetterWorker.pingWebServer(mockWebServer); verify(mockWebServerService).updateState(any(Identifier.class), eq(WebServerReachableState.WS_UNREACHABLE), contains("failed with a response code")); verify(mockMessagingService).send(any(WebServerState.class)); reset(mockWebServerService); reset(mockMessagingService); webServerStateSetterWorker.pingWebServer(mockWebServer); verify(mockWebServerService, never()).updateState(any(Identifier.class), eq(WebServerReachableState.WS_REACHABLE), eq(StringUtils.EMPTY)); verify(mockMessagingService, never()).send(any(WebServerState.class)); }
@Async("webServerTaskExecutor") public void pingWebServer(final WebServer webServer) { if (!webServerCanBePinged(webServer)) { return; } synchronized (webServersToPing) { if (webServersToPing.contains(webServer.getId())) { LOGGER.debug("List of web servers currently being pinged: {}", webServersToPing); LOGGER.debug("Cannot ping web server {} since it is currently being pinged", webServer.getName(), webServer); return; } webServersToPing.add(webServer.getId()); } LOGGER.debug("Requesting {} for web server {}", webServer.getStatusUri(), webServer.getName()); ClientHttpResponse response = null; try { final ClientHttpRequest request; request = httpRequestFactory.createRequest(webServer.getStatusUri(), HttpMethod.GET); response = request.execute(); LOGGER.debug("Web server {} status code = {}", webServer.getName(), response.getStatusCode()); if (HttpStatus.OK.equals(response.getStatusCode())) { setState(webServer, WebServerReachableState.WS_REACHABLE, StringUtils.EMPTY); } else { setState(webServer, WebServerReachableState.WS_UNREACHABLE, MessageFormat.format(RESPONSE_NOT_OK_MSG, webServer.getStatusUri(), response.getStatusCode())); } } catch (final IOException e) { if (!WebServerReachableState.WS_UNREACHABLE.equals(webServer.getState())) { LOGGER.warn("Failed to ping {}!", webServer.getName(), e); setState(webServer, WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); } } finally { if (response != null) { response.close(); } webServersToPing.remove(webServer.getId()); } }
WebServerStateSetterWorker { @Async("webServerTaskExecutor") public void pingWebServer(final WebServer webServer) { if (!webServerCanBePinged(webServer)) { return; } synchronized (webServersToPing) { if (webServersToPing.contains(webServer.getId())) { LOGGER.debug("List of web servers currently being pinged: {}", webServersToPing); LOGGER.debug("Cannot ping web server {} since it is currently being pinged", webServer.getName(), webServer); return; } webServersToPing.add(webServer.getId()); } LOGGER.debug("Requesting {} for web server {}", webServer.getStatusUri(), webServer.getName()); ClientHttpResponse response = null; try { final ClientHttpRequest request; request = httpRequestFactory.createRequest(webServer.getStatusUri(), HttpMethod.GET); response = request.execute(); LOGGER.debug("Web server {} status code = {}", webServer.getName(), response.getStatusCode()); if (HttpStatus.OK.equals(response.getStatusCode())) { setState(webServer, WebServerReachableState.WS_REACHABLE, StringUtils.EMPTY); } else { setState(webServer, WebServerReachableState.WS_UNREACHABLE, MessageFormat.format(RESPONSE_NOT_OK_MSG, webServer.getStatusUri(), response.getStatusCode())); } } catch (final IOException e) { if (!WebServerReachableState.WS_UNREACHABLE.equals(webServer.getState())) { LOGGER.warn("Failed to ping {}!", webServer.getName(), e); setState(webServer, WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); } } finally { if (response != null) { response.close(); } webServersToPing.remove(webServer.getId()); } } }
WebServerStateSetterWorker { @Async("webServerTaskExecutor") public void pingWebServer(final WebServer webServer) { if (!webServerCanBePinged(webServer)) { return; } synchronized (webServersToPing) { if (webServersToPing.contains(webServer.getId())) { LOGGER.debug("List of web servers currently being pinged: {}", webServersToPing); LOGGER.debug("Cannot ping web server {} since it is currently being pinged", webServer.getName(), webServer); return; } webServersToPing.add(webServer.getId()); } LOGGER.debug("Requesting {} for web server {}", webServer.getStatusUri(), webServer.getName()); ClientHttpResponse response = null; try { final ClientHttpRequest request; request = httpRequestFactory.createRequest(webServer.getStatusUri(), HttpMethod.GET); response = request.execute(); LOGGER.debug("Web server {} status code = {}", webServer.getName(), response.getStatusCode()); if (HttpStatus.OK.equals(response.getStatusCode())) { setState(webServer, WebServerReachableState.WS_REACHABLE, StringUtils.EMPTY); } else { setState(webServer, WebServerReachableState.WS_UNREACHABLE, MessageFormat.format(RESPONSE_NOT_OK_MSG, webServer.getStatusUri(), response.getStatusCode())); } } catch (final IOException e) { if (!WebServerReachableState.WS_UNREACHABLE.equals(webServer.getState())) { LOGGER.warn("Failed to ping {}!", webServer.getName(), e); setState(webServer, WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); } } finally { if (response != null) { response.close(); } webServersToPing.remove(webServer.getId()); } } WebServerStateSetterWorker(@Qualifier("webServerInMemoryStateManagerService")
                                      final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                      final WebServerService webServerService,
                                      final MessagingService messagingService,
                                      final GroupStateNotificationService groupStateNotificationService,
                                      @Qualifier("httpRequestFactory")
                                      final HttpComponentsClientHttpRequestFactory httpRequestFactory); }
WebServerStateSetterWorker { @Async("webServerTaskExecutor") public void pingWebServer(final WebServer webServer) { if (!webServerCanBePinged(webServer)) { return; } synchronized (webServersToPing) { if (webServersToPing.contains(webServer.getId())) { LOGGER.debug("List of web servers currently being pinged: {}", webServersToPing); LOGGER.debug("Cannot ping web server {} since it is currently being pinged", webServer.getName(), webServer); return; } webServersToPing.add(webServer.getId()); } LOGGER.debug("Requesting {} for web server {}", webServer.getStatusUri(), webServer.getName()); ClientHttpResponse response = null; try { final ClientHttpRequest request; request = httpRequestFactory.createRequest(webServer.getStatusUri(), HttpMethod.GET); response = request.execute(); LOGGER.debug("Web server {} status code = {}", webServer.getName(), response.getStatusCode()); if (HttpStatus.OK.equals(response.getStatusCode())) { setState(webServer, WebServerReachableState.WS_REACHABLE, StringUtils.EMPTY); } else { setState(webServer, WebServerReachableState.WS_UNREACHABLE, MessageFormat.format(RESPONSE_NOT_OK_MSG, webServer.getStatusUri(), response.getStatusCode())); } } catch (final IOException e) { if (!WebServerReachableState.WS_UNREACHABLE.equals(webServer.getState())) { LOGGER.warn("Failed to ping {}!", webServer.getName(), e); setState(webServer, WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); } } finally { if (response != null) { response.close(); } webServersToPing.remove(webServer.getId()); } } WebServerStateSetterWorker(@Qualifier("webServerInMemoryStateManagerService")
                                      final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                      final WebServerService webServerService,
                                      final MessagingService messagingService,
                                      final GroupStateNotificationService groupStateNotificationService,
                                      @Qualifier("httpRequestFactory")
                                      final HttpComponentsClientHttpRequestFactory httpRequestFactory); @Async("webServerTaskExecutor") void pingWebServer(final WebServer webServer); }
WebServerStateSetterWorker { @Async("webServerTaskExecutor") public void pingWebServer(final WebServer webServer) { if (!webServerCanBePinged(webServer)) { return; } synchronized (webServersToPing) { if (webServersToPing.contains(webServer.getId())) { LOGGER.debug("List of web servers currently being pinged: {}", webServersToPing); LOGGER.debug("Cannot ping web server {} since it is currently being pinged", webServer.getName(), webServer); return; } webServersToPing.add(webServer.getId()); } LOGGER.debug("Requesting {} for web server {}", webServer.getStatusUri(), webServer.getName()); ClientHttpResponse response = null; try { final ClientHttpRequest request; request = httpRequestFactory.createRequest(webServer.getStatusUri(), HttpMethod.GET); response = request.execute(); LOGGER.debug("Web server {} status code = {}", webServer.getName(), response.getStatusCode()); if (HttpStatus.OK.equals(response.getStatusCode())) { setState(webServer, WebServerReachableState.WS_REACHABLE, StringUtils.EMPTY); } else { setState(webServer, WebServerReachableState.WS_UNREACHABLE, MessageFormat.format(RESPONSE_NOT_OK_MSG, webServer.getStatusUri(), response.getStatusCode())); } } catch (final IOException e) { if (!WebServerReachableState.WS_UNREACHABLE.equals(webServer.getState())) { LOGGER.warn("Failed to ping {}!", webServer.getName(), e); setState(webServer, WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); } } finally { if (response != null) { response.close(); } webServersToPing.remove(webServer.getId()); } } WebServerStateSetterWorker(@Qualifier("webServerInMemoryStateManagerService")
                                      final InMemoryStateManagerService<Identifier<WebServer>, WebServerReachableState> inMemoryStateManagerService,
                                      final WebServerService webServerService,
                                      final MessagingService messagingService,
                                      final GroupStateNotificationService groupStateNotificationService,
                                      @Qualifier("httpRequestFactory")
                                      final HttpComponentsClientHttpRequestFactory httpRequestFactory); @Async("webServerTaskExecutor") void pingWebServer(final WebServer webServer);  }