Search in sources :

Example 11 with PlcConnection

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);
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException) PlcConnection(org.apache.plc4x.java.api.PlcConnection) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 12 with PlcConnection

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());
}
Also used : PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Example 13 with PlcConnection

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());
}
Also used : PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 14 with PlcConnection

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);
}
Also used : MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Example 15 with PlcConnection

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(""));
}
Also used : MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test)

Aggregations

PlcConnection (org.apache.plc4x.java.api.PlcConnection)80 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)36 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)30 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)23 Test (org.junit.jupiter.api.Test)22 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)20 ExecutionException (java.util.concurrent.ExecutionException)18 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)12 TimeoutException (java.util.concurrent.TimeoutException)9 BigInteger (java.math.BigInteger)8 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)8 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)8 Map (java.util.Map)7 HashMap (java.util.HashMap)6 MockConnection (org.apache.plc4x.java.mock.connection.MockConnection)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Condition (org.assertj.core.api.Condition)5 Disabled (org.junit.jupiter.api.Disabled)5 Field (java.lang.reflect.Field)4 LocalDateTime (java.time.LocalDateTime)4