/** * {@inheritDoc} */ @Override protected RetryStrategy getRetryStrategy() { return new FailFastRetryStrategy(); } }
/** * Returns the selected retry strategy. * * @return The retry strategy. */ protected RetryStrategy getRetryStrategy() { return new AdditiveWaitRetryStrategy(); }
/** * {@inheritDoc} */ @Override public void disconnect() { stopClient(); agentStorageService = null; // NOPMD agentService = null; // NOPMD keepAliveService = null; // NOPMD }
/** * {@inheritDoc} */ @Override public void sendDataObjects(List<? extends DefaultData> measurements) throws ServerUnavailableException { if (!isConnected()) { throw new ServerUnavailableException(); } if ((null != measurements) && !measurements.isEmpty()) { try { AddDataObjects remote = new AddDataObjects(agentStorageService, measurements); remote.makeCall(); } catch (ExecutionException executionException) { // there should be no execution exception log.error("Could not send data objects", executionException); } catch (ServerUnavailableException e) { if (!e.isServerTimeout()) { disconnectClient(); } throw e; } } }
/** * {@inheritDoc} */ @Override public void instrumentationApplied(final long platformIdent, Map<Long, long[]> methodToSensorMap) throws ServerUnavailableException { if (!isConnected()) { throw new ServerUnavailableException(); } if (MapUtils.isNotEmpty(methodToSensorMap)) { try { InstrumentationAppliedCall call = new InstrumentationAppliedCall(agentService, platformIdent, methodToSensorMap); call.makeCall(); } catch (ExecutionException executionException) { // there should be no execution exception log.error("Could not sent instrumented method ids", executionException); } catch (ServerUnavailableException e) { if (!e.isServerTimeout()) { disconnectClient(); } throw e; } } }
@Override public void run() { sendKeepAlive(); }
/** * Reconnects the client if it's not already connected. * * @throws IOException * If {@link IOException} occurs during the connection. */ private void reconnectClient() throws IOException { if (!isConnected()) { client.reconnect(); bindServices(); } }
@Test public void alreadyConnected() throws ConnectException { when(client.isConnected()).thenReturn(true); connection.reconnect(); verify(client).isConnected(); verifyNoMoreInteractions(client); }
/** * Checks whether an attempt to reconnect should be executed. This method also handles the * counter variables to keep track of the state. * * @return If we should execute reconnect. */ private boolean shouldReconnect() { noConnectionCount++; boolean shouldReconnect = noConnectionCount >= nextReconnectAt; if (shouldReconnect) { noConnectionCount = 0; increaseNextReconnectAt(); } return shouldReconnect; }
/** * Closes the client (disconnects the client) if the client is connected. */ private void disconnectClient() { if (isConnected()) { client.close(); } }
/** * Starts the client and tries to make a connection to the given host/port. * * @param host * Host IP address. * @param port * Port to connect to. * * @throws IOException * If {@link IOException} occurs during the connection. */ private void startClient(String host, int port) throws IOException { if (null != client) { client.start(); client.connect(5000, host, port); bindServices(); } }
@Test public void successful() { messageFetcher.postConstruct(); verify(executorService).scheduleAtFixedRate(messageFetcher, 30L, 30L, TimeUnit.SECONDS); verifyNoMoreInteractions(executorService); } }
@Test public void disconnect() { connection.disconnect(); verify(client).stop(); } }
@Test public void notConnected() { when(connection.isConnected()).thenReturn(false); messageFetcher.run(); verify(connection).isConnected(); verifyNoMoreInteractions(connection); verifyZeroInteractions(eventPublisher, platformManager); } }
@Test public void alreadyConnected() throws ConnectException { when(client.isConnected()).thenReturn(true); connection.connect("host", 22); verify(client).isConnected(); verifyNoMoreInteractions(client); }
@Test public void keepAlive() throws Exception { when(client.isConnected()).thenReturn(true); long id = 3L; connection.sendKeepAlive(id); verify(keepAliveService, times(1)).sendKeepAlive(id); verifyNoMoreInteractions(keepAliveService); }
@Test public void reconnect() throws Exception { when(connection.isConnected()).thenReturn(false); doThrow(ConnectException.class).when(connection).reconnect(); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(2)).isConnected(); verify(connection).reconnect(); verifyNoMoreInteractions(connection); }
@Test(expectedExceptions = ConnectException.class) public void ioException() throws IOException { when(client.isConnected()).thenReturn(false); doThrow(IOException.class).when(client).reconnect(); connection.reconnect(); } }
@Test public void connected() throws Exception { when(connection.isConnected()).thenReturn(true); when(platformManager.getPlatformId()).thenReturn(PLATFORM_ID); manager.sendKeepAlive(); verify(connection).isConnected(); verify(connection).sendKeepAlive(PLATFORM_ID); verifyNoMoreInteractions(connection); }
@Test public void reconnect() throws IOException { when(client.isConnected()).thenReturn(false); connection.reconnect(); verify(client).reconnect(); verify(client, atLeast(1)).isConnected(); verifyNoMoreInteractions(client); }