Skip to content

Commit

Permalink
fix(plc4j/connection-cache): fix issue with timing of thread during d…
Browse files Browse the repository at this point in the history
…ouble connection test. (#796)
  • Loading branch information
hutcheb committed Feb 13, 2023
1 parent 22273ad commit b58ae5d
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mockito.Mockito;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;

public class CachedPlcConnectionManagerTest {

Expand Down Expand Up @@ -121,20 +122,21 @@ public void testDoubleConnectionRequestTimeoutTest() throws Exception {
// Create a connectionManager with a maximum wait time of 50ms
PlcConnectionManager mockConnectionManager = Mockito.mock(PlcConnectionManager.class);
CachedPlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder(mockConnectionManager).withMaxWaitTime(Duration.ofMillis(50)).build();
CountDownLatch startSignal = new CountDownLatch(1);

// Get the connection for the first time.
(new Thread(() -> {
try (PlcConnection connection = connectionManager.getConnection("test")) {
try {
PlcConnection connection = connectionManager.getConnection("test");
startSignal.countDown();
Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
// Sleep for a second.
Thread.sleep(100L);
} catch (Exception e) {
Assertions.fail("Not expecting an exception here", e);
}
})).start();

// This is needed as starting the previous thread seems to take a little-bit of time.
Thread.sleep(10L);
startSignal.await();

// Get the same connection a second time.
try (PlcConnection ignored = connectionManager.getConnection("test")) {
Expand Down

0 comments on commit b58ae5d

Please sign in to comment.