use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class CachedDriverManagerMT method borrowAndDontReturn.
@Test
void borrowAndDontReturn() throws InterruptedException {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
PooledDriverManager driverManager = new PooledDriverManager();
for (int i = 1; i <= 10_000; i++) {
pool.submit(() -> {
try {
PlcConnection conn = driverManager.getConnection(PLC_IP);
System.out.println("Successfully got a Connection...");
PlcReadResponse response = conn.readRequestBuilder().addItem("asdf", "%DB444:14.0:BOOL").build().execute().get(500, TimeUnit.MILLISECONDS);
System.out.println("Response: " + response.getBoolean("asdf"));
} catch (PlcConnectionException | InterruptedException | ExecutionException | TimeoutException e) {
// Intentionally do Nothing...
}
});
Thread.sleep(1);
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.DAYS);
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class CachedDriverManagerTest method useClosedConnection.
@Test
void useClosedConnection() throws Exception {
PlcConnectionFactory mock = Mockito.mock(PlcConnectionFactory.class);
CachedDriverManager driverManager = new CachedDriverManager("", mock);
// Get Connmection
PlcConnection connection = driverManager.getConnection("");
// Close the Connection
connection.close();
assertThrows(IllegalStateException.class, () -> connection.readRequestBuilder());
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class CachedDriverManagerTest method twoRequests_firstTakesLong_secondsTimesOut.
@Test
@Disabled
void twoRequests_firstTakesLong_secondsTimesOut() throws PlcConnectionException, InterruptedException, ExecutionException, TimeoutException {
PlcConnectionFactory mock = Mockito.mock(PlcConnectionFactory.class);
MockConnection plcMockConnection = mock(MockConnection.class);
when(mock.create()).thenReturn(plcMockConnection);
when(plcMockConnection.readRequestBuilder()).thenReturn(Mockito.mock(PlcReadRequest.Builder.class));
CachedDriverManager driverManager = new CachedDriverManager("", mock, 5_000);
CompletableFuture<PlcConnection> future1 = CompletableFuture.supplyAsync(() -> {
try {
return driverManager.getConnection("");
} catch (PlcConnectionException e) {
throw new RuntimeException();
}
});
CompletableFuture<PlcConnection> future2 = CompletableFuture.supplyAsync(() -> {
try {
return driverManager.getConnection("");
} catch (PlcConnectionException e) {
throw new RuntimeException();
}
});
PlcConnection conn1 = future1.get(1, TimeUnit.SECONDS);
assertThrows(Exception.class, () -> future2.get());
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class CachedDriverManagerTest method killBorrowedConnectionWhenRunningLong.
@Test
void killBorrowedConnectionWhenRunningLong() throws PlcConnectionException, InterruptedException {
PlcConnectionFactory mock = Mockito.mock(PlcConnectionFactory.class);
MockConnection plcMockConnection = mock(MockConnection.class);
when(mock.create()).thenReturn(plcMockConnection);
when(plcMockConnection.readRequestBuilder()).thenReturn(Mockito.mock(PlcReadRequest.Builder.class));
CachedDriverManager driverManager = new CachedDriverManager("", mock);
// Get Connmection
PlcConnection connection = driverManager.getConnection("");
// This should work
connection.readRequestBuilder();
TimeUnit.SECONDS.sleep(6);
// If we wait to long, the connection should become invalid
assertThrows(IllegalStateException.class, () -> connection.readRequestBuilder());
// And the Pool should once again have a connection
assertThat(driverManager.getState()).isEqualTo(CachedDriverManager.ConnectionState.DISCONNECTED);
}
use of org.apache.plc4x.java.api.PlcConnection in project plc4x by apache.
the class CachedDriverManagerTest method multipleRequests_allbutfirstFail.
@Test
void multipleRequests_allbutfirstFail() throws PlcException {
PlcConnectionFactory mock = Mockito.mock(PlcConnectionFactory.class);
MockConnection plcMockConnection = mock(MockConnection.class);
when(mock.create()).thenReturn(plcMockConnection);
when(plcMockConnection.readRequestBuilder()).thenReturn(Mockito.mock(PlcReadRequest.Builder.class));
CachedDriverManager driverManager = new CachedDriverManager("", mock);
// Get Connmection
PlcConnection connection = driverManager.getConnection("");
// Try to get another one -> should failt
assertThrows(PlcConnectionException.class, () -> driverManager.getConnection(""));
}
Aggregations