PooledPlcDriverManager extends PlcDriverManager { @Override public PlcConnection getConnection(String url) throws PlcConnectionException { return getConnection(url, noPlcAuthentication); } PooledPlcDriverManager();  PooledPlcDriverManager(PoolKeyFactory poolKeyFactory);  PooledPlcDriverManager(ClassLoader classLoader);  PooledPlcDriverManager(ClassLoader classLoader, PoolKeyFactory poolKeyFactory);  PooledPlcDriverManager(PoolCreator poolCreator);  PooledPlcDriverManager(PoolCreator poolCreator, PoolKeyFactory poolKeyFactory);  PooledPlcDriverManager(ClassLoader classLoader, PoolCreator poolCreator); @Override PlcConnection getConnection(String url); @Override PlcConnection getConnection(String url, PlcAuthentication authentication); Map<String, Number> getStatistics();  }
@Test void getConnection() throws Exception { when(plcDriver.getConnection(anyString())).then(invocationOnMock -> new DummyPlcConnection(invocationOnMock.getArgument(0))); LinkedList<Callable<PlcConnection>> callables = new LinkedList<>(); IntStream.range(0, 8).forEach(i -> callables.add(() -> { try { return SUT.getConnection("dummydummy:single/socket1/socket2?fancyOption=true"); } catch (PlcConnectionException e) { throw new RuntimeException(e); } })); IntStream.range(0, 5).forEach(i -> callables.add(() -> { try { return SUT.getConnection("dummydummy:multi-" + i + "/socket1/socket2?fancyOption=true"); } catch (PlcConnectionException e) { throw new RuntimeException(e); } })); List<Future<PlcConnection>> futures = executorService.invokeAll(callables); futures.forEach(plcConnectionFuture1 -> { try { plcConnectionFuture1.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }); LOGGER.info("Statistics after execution {}", SUT.getStatistics()); verify(plcDriver, times(13)).getConnection(anyString()); assertThat(SUT.getStatistics()).contains( entry("PoolKey{url='dummydummy:single/socket1/socket2?fancyOption=true'}.numActive", 8) ); futures.forEach(plcConnectionFuture -> { try { plcConnectionFuture.get().close(); } catch (Exception e) { throw new RuntimeException(e); } }); assertThat(SUT.getStatistics()).contains( entry("PoolKey{url='dummydummy:single/socket1/socket2?fancyOption=true'}.numActive", 0) ); }
@Test void connectionInvalidation() throws Exception { when(plcDriver.getConnection(anyString())).then(invocationOnMock -> new DummyPlcConnection(invocationOnMock.getArgument(0))); PlcConnection connection = SUT.getConnection("dummydummy:single/socket1/socket2?fancyOption=true"); assertThat(connection.isConnected()).isEqualTo(true); assertThat(connection.getMetadata().canRead()).isEqualTo(false); assertThat(connection.getMetadata().canWrite()).isEqualTo(false); assertThat(connection.getMetadata().canSubscribe()).isEqualTo(false); connection.close(); assertThatThrownBy(connection::connect).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::isConnected).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::close).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::getMetadata).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::readRequestBuilder).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::writeRequestBuilder).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::subscriptionRequestBuilder).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); assertThatThrownBy(connection::unsubscriptionRequestBuilder).isInstanceOf(IllegalStateException.class).hasMessage("Proxy not valid anymore"); }