@Test public void test_connection_closed_on_fetch_failure() throws ConnectionException, EntityVersionMismatchException, EntityNotProvidedException, EntityNotFoundException, IOException { when(connection.<Diagnostics, Object, Properties>getEntityRef(eq(Diagnostics.class), eq(1L), eq("root"))).thenReturn(entityRef); when(entityRef.fetchEntity(any(Properties.class))).thenThrow(new EntityNotFoundException("", "")); exception.expect(ConnectionException.class); DiagnosticServiceFactory.fetch( connectionService, InetSocketAddress.createUnresolved("localhost", 9410), "ConnectionName", Duration.ofSeconds(2), Duration.ofSeconds(3), null, new ObjectMapperFactory()); verify(connection, times(1)).close(); }
public static DiagnosticService fetch(InetSocketAddress nodeAddress, String connectionName, Duration connectTimeout, Duration diagnosticInvokeTimeout, String securityRootDirectory, ObjectMapperFactory objectMapperFactory) throws ConnectionException { Properties properties = buildProperties(connectionName, connectTimeout, securityRootDirectory); Connection connection = ConnectionFactory.connect(Collections.singletonList(nodeAddress), properties); try { return fetch(connection, diagnosticInvokeTimeout, objectMapperFactory); } catch (EntityException e) { try { connection.close(); } catch (IOException ignored) { } throw new ConnectionException(e); } }
DiagnosticServiceFactory { public static DiagnosticService fetch(InetSocketAddress nodeAddress, String connectionName, Duration connectTimeout, Duration diagnosticInvokeTimeout, String securityRootDirectory, ObjectMapperFactory objectMapperFactory) throws ConnectionException { Properties properties = buildProperties(connectionName, connectTimeout, securityRootDirectory); Connection connection = ConnectionFactory.connect(Collections.singletonList(nodeAddress), properties); try { return fetch(connection, diagnosticInvokeTimeout, objectMapperFactory); } catch (EntityException e) { try { connection.close(); } catch (IOException ignored) { } throw new ConnectionException(e); } } }
DiagnosticServiceFactory { public static DiagnosticService fetch(InetSocketAddress nodeAddress, String connectionName, Duration connectTimeout, Duration diagnosticInvokeTimeout, String securityRootDirectory, ObjectMapperFactory objectMapperFactory) throws ConnectionException { Properties properties = buildProperties(connectionName, connectTimeout, securityRootDirectory); Connection connection = ConnectionFactory.connect(Collections.singletonList(nodeAddress), properties); try { return fetch(connection, diagnosticInvokeTimeout, objectMapperFactory); } catch (EntityException e) { try { connection.close(); } catch (IOException ignored) { } throw new ConnectionException(e); } }  }
DiagnosticServiceFactory { public static DiagnosticService fetch(InetSocketAddress nodeAddress, String connectionName, Duration connectTimeout, Duration diagnosticInvokeTimeout, String securityRootDirectory, ObjectMapperFactory objectMapperFactory) throws ConnectionException { Properties properties = buildProperties(connectionName, connectTimeout, securityRootDirectory); Connection connection = ConnectionFactory.connect(Collections.singletonList(nodeAddress), properties); try { return fetch(connection, diagnosticInvokeTimeout, objectMapperFactory); } catch (EntityException e) { try { connection.close(); } catch (IOException ignored) { } throw new ConnectionException(e); } }  static DiagnosticService fetch(InetSocketAddress nodeAddress,
                                        String connectionName,
                                        Duration connectTimeout,
                                        Duration diagnosticInvokeTimeout,
                                        String securityRootDirectory,
                                        ObjectMapperFactory objectMapperFactory); static DiagnosticService fetch(ConnectionService connectionService,
                                        InetSocketAddress nodeAddress,
                                        String connectionName,
                                        Duration connectionTimeout,
                                        Duration diagnosticInvokeTimeout,
                                        String securityRootDirectory,
                                        ObjectMapperFactory objectMapperFactory); static DiagnosticService getDiagnosticService(Connection connection, Diagnostics delegate, ObjectMapperFactory objectMapperFactory); static DiagnosticService getDiagnosticService(Connection connection, Diagnostics delegate, DiagnosticCodec<?> codec); }
DiagnosticServiceFactory { public static DiagnosticService fetch(InetSocketAddress nodeAddress, String connectionName, Duration connectTimeout, Duration diagnosticInvokeTimeout, String securityRootDirectory, ObjectMapperFactory objectMapperFactory) throws ConnectionException { Properties properties = buildProperties(connectionName, connectTimeout, securityRootDirectory); Connection connection = ConnectionFactory.connect(Collections.singletonList(nodeAddress), properties); try { return fetch(connection, diagnosticInvokeTimeout, objectMapperFactory); } catch (EntityException e) { try { connection.close(); } catch (IOException ignored) { } throw new ConnectionException(e); } }  static DiagnosticService fetch(InetSocketAddress nodeAddress,
                                        String connectionName,
                                        Duration connectTimeout,
                                        Duration diagnosticInvokeTimeout,
                                        String securityRootDirectory,
                                        ObjectMapperFactory objectMapperFactory); static DiagnosticService fetch(ConnectionService connectionService,
                                        InetSocketAddress nodeAddress,
                                        String connectionName,
                                        Duration connectionTimeout,
                                        Duration diagnosticInvokeTimeout,
                                        String securityRootDirectory,
                                        ObjectMapperFactory objectMapperFactory); static DiagnosticService getDiagnosticService(Connection connection, Diagnostics delegate, ObjectMapperFactory objectMapperFactory); static DiagnosticService getDiagnosticService(Connection connection, Diagnostics delegate, DiagnosticCodec<?> codec);  }