/** * 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(); } }
if (!isConnected()) { throw new ServerUnavailableException(); } catch (ServerUnavailableException e) { if (!e.isServerTimeout()) { disconnectClient();
if (!isConnected()) { throw new ServerUnavailableException(); networkInterfaces = getNetworkInterfaces(); } catch (ServerUnavailableException e) { if (!e.isServerTimeout()) { disconnectClient();
/** * {@inheritDoc} */ @Override public void reconnect() throws ConnectException { if ((null != client) && !isConnected()) { try { reconnectClient(); log.info("KryoNet: Reconnection successful!"); connectionException = false; synchronized (reconnectionMonitor) { reconnectionMonitor.notifyAll(); } } catch (Exception exception) { connectionException = true; ConnectException e = new ConnectException(exception.getMessage()); e.initCause(exception); throw e; // NOPMD root cause exception is set } } }
/** * {@inheritDoc} */ @Override public void connect(String host, int port) throws ConnectException { if (!isConnected()) { try { if (!connectionException) { log.info("KryoNet: Connecting to " + host + ":" + port); } startClient(host, port); log.info("KryoNet: Connection established!"); connectionException = false; } catch (Exception exception) { if (!connectionException) { log.info("KryoNet: Connection to the server failed."); } connectionException = true; stopClient(); if (log.isTraceEnabled()) { log.trace("connect()", exception); } ConnectException e = new ConnectException(exception.getMessage()); e.initCause(exception); throw e; // NOPMD root cause exception is set } } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); long id = 7; Collection<JmxAttributeDescriptor> descriptors = Collections.emptyList(); try { connection.analyzeJmxAttributes(id, descriptors); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { verifyZeroInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); long id = 7; String hash = "hash"; Type type = mock(Type.class); try { connection.analyze(id, hash, type); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { verifyZeroInteractions(agentService); } } }
@Test public void fetchAgentMessages() throws Exception { IAgentMessage<?> message = mock(IAgentMessage.class); List<IAgentMessage<?>> messages = Arrays.<IAgentMessage<?>> asList(message); when(client.isConnected()).thenReturn(true); long id = 7; when(agentService.fetchAgentMessages(id)).thenReturn(messages); List<IAgentMessage<?>> result = connection.fetchAgentMessages(id); assertThat(result, is(equalTo(messages))); verify(agentService).fetchAgentMessages(id); verifyNoMoreInteractions(agentService); }
@Test public void instrumentationApplied() throws Exception { when(client.isConnected()).thenReturn(true); Map<Long, long[]> methodToSensorMap = mock(Map.class); when(methodToSensorMap.isEmpty()).thenReturn(false); long id = 7; connection.instrumentationApplied(id, methodToSensorMap); verify(agentService, times(1)).instrumentationApplied(id, methodToSensorMap); verifyNoMoreInteractions(agentService); }
@Test(expectedExceptions = ConnectException.class) public void ioException() throws IOException { when(client.isConnected()).thenReturn(false); String host = "host"; int port = 22; doThrow(IOException.class).when(client).connect(anyInt(), eq(host), eq(port)); connection.connect(host, port); } }
/** * 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 disconnect() { connection.disconnect(); verify(client).stop(); } }
@Test public void analyzeJmxAttributes() throws Exception { Collection<JmxAttributeDescriptor> result = mock(Collection.class); when(client.isConnected()).thenReturn(true); doReturn(result).when(agentService).analyzeJmxAttributes(anyLong(), Matchers.<Collection<JmxAttributeDescriptor>> any()); long id = 7; Collection<JmxAttributeDescriptor> descriptors = Collections.emptyList(); Collection<JmxAttributeDescriptor> receivedResult = connection.analyzeJmxAttributes(id, descriptors); assertThat(receivedResult, is(result)); verify(agentService, times(1)).analyzeJmxAttributes(id, descriptors); verifyNoMoreInteractions(agentService); }
@Test public void analyzeAndInstrument() throws Exception { InstrumentationDefinition instrumentationResult = mock(InstrumentationDefinition.class); when(client.isConnected()).thenReturn(true); doReturn(instrumentationResult).when(agentService).analyze(anyLong(), anyString(), Matchers.<Type> any()); long id = 7; String hash = "hash"; Type type = mock(Type.class); InstrumentationDefinition receivedResult = connection.analyze(id, hash, type); assertThat(receivedResult, is(instrumentationResult)); verify(agentService, times(1)).analyze(id, hash, type); verifyNoMoreInteractions(agentService); }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); long id = 7; try { connection.fetchAgentMessages(id); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { verifyZeroInteractions(agentService); } } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); Map<Long, long[]> methodToSensorMap = mock(Map.class); long id = 7; try { connection.instrumentationApplied(id, methodToSensorMap); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { verifyZeroInteractions(agentService); } } }
@Test public void alreadyConnected() throws ConnectException { when(client.isConnected()).thenReturn(true); connection.connect("host", 22); verify(client).isConnected(); verifyNoMoreInteractions(client); }
if (!isConnected()) { throw new ServerUnavailableException(); } catch (ServerUnavailableException e) { if (!e.isServerTimeout()) { disconnectClient();
@Test(expectedExceptions = { BusinessException.class }) public void businessException() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(BusinessException.class).when(agentService).analyzeJmxAttributes(anyLong(), Matchers.<Collection<JmxAttributeDescriptor>> any()); long id = 7; Collection<JmxAttributeDescriptor> descriptors = Collections.emptyList(); try { connection.analyzeJmxAttributes(id, descriptors); } finally { verify(agentService, times(1)).analyzeJmxAttributes(id, descriptors); verifyNoMoreInteractions(agentService); } }