Replace static with random port in tests (#224)
This commit is contained in:
parent
5a299cd694
commit
f176d021f8
@ -19,6 +19,11 @@ package org.ice4j.pseudotcp;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class enables multi thread tests where main thread waits in loop for
|
* This class enables multi thread tests where main thread waits in loop for
|
||||||
* specified condition to be met, while others perform some operations. For
|
* specified condition to be met, while others perform some operations. For
|
||||||
@ -50,18 +55,13 @@ public class MultiThreadSupportTest implements Thread.UncaughtExceptionHandler
|
|||||||
|
|
||||||
private static final long ASSERT_WAIT_INTERVAL = 100;
|
private static final long ASSERT_WAIT_INTERVAL = 100;
|
||||||
|
|
||||||
protected interface WaitUntilDone
|
protected boolean assert_wait_until(BooleanSupplier wait, long timeoutMs)
|
||||||
{
|
|
||||||
boolean isDone();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean assert_wait_until(WaitUntilDone wait, long timeoutMs)
|
|
||||||
{
|
{
|
||||||
|
long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMs);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
long start = System.currentTimeMillis();
|
long start = System.nanoTime();
|
||||||
while (!wait.isDone()
|
while (!wait.getAsBoolean() && (System.nanoTime() - start) < timeoutNanos)
|
||||||
&& (System.currentTimeMillis() - start) < timeoutMs)
|
|
||||||
{
|
{
|
||||||
synchronized (testLock)
|
synchronized (testLock)
|
||||||
{
|
{
|
||||||
@ -74,7 +74,7 @@ public class MultiThreadSupportTest implements Thread.UncaughtExceptionHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return wait.isDone();
|
return wait.getAsBoolean();
|
||||||
}
|
}
|
||||||
catch (InterruptedException ex)
|
catch (InterruptedException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -137,14 +137,9 @@ public class PseudoTcpStreamTest
|
|||||||
clientThread.start();
|
clientThread.start();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
boolean success = assert_wait_until(new WaitUntilDone()
|
boolean success = assert_wait_until(
|
||||||
{
|
() -> client.getState() == PseudoTcpState.TCP_CLOSED,
|
||||||
@Override
|
transferTimeout);
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return client.getState() == PseudoTcpState.TCP_CLOSED;
|
|
||||||
}
|
|
||||||
}, transferTimeout);
|
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
clientThread.join();
|
clientThread.join();
|
||||||
@ -207,17 +202,11 @@ public class PseudoTcpStreamTest
|
|||||||
Thread.setDefaultUncaughtExceptionHandler(this);
|
Thread.setDefaultUncaughtExceptionHandler(this);
|
||||||
final PseudoTcpSocketImpl server;
|
final PseudoTcpSocketImpl server;
|
||||||
final PseudoTcpSocketImpl client;
|
final PseudoTcpSocketImpl client;
|
||||||
final int server_port = 49998;
|
DatagramSocket serverSocket = new DatagramSocket(0, InetAddress.getLoopbackAddress());
|
||||||
final InetSocketAddress serverAddress =
|
server = new PseudoTcpSocketImpl(0, serverSocket);
|
||||||
new InetSocketAddress(InetAddress.getLocalHost(), server_port);
|
|
||||||
server =
|
|
||||||
new PseudoTcpSocketImpl(0,new DatagramSocket(serverAddress));
|
|
||||||
client = new PseudoTcpSocketImpl(0);
|
client = new PseudoTcpSocketImpl(0);
|
||||||
//Servers thread waiting for connection
|
//Servers thread waiting for connection
|
||||||
new Thread(new Runnable()
|
new Thread(() ->
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -230,7 +219,7 @@ public class PseudoTcpStreamTest
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
testOperation.testTimeout(server);
|
testOperation.testTimeout(server);
|
||||||
fail("No expected timeout occured on operation");
|
fail("No expected timeout occurred on operation");
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
@ -244,17 +233,16 @@ public class PseudoTcpStreamTest
|
|||||||
throw new RuntimeException(exc);
|
throw new RuntimeException(exc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}).start();
|
}).start();
|
||||||
//Clients thread connects and closes socket
|
//Clients thread connects and closes socket
|
||||||
new Thread(new Runnable()
|
new Thread(() ->
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.connect(serverAddress, 2000);
|
client.connect(new InetSocketAddress(
|
||||||
|
InetAddress.getLoopbackAddress(),
|
||||||
|
serverSocket.getLocalPort()),
|
||||||
|
2000);
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
@ -262,17 +250,10 @@ public class PseudoTcpStreamTest
|
|||||||
{
|
{
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}).start();
|
}).start();
|
||||||
//Waits for server to close socket
|
//Waits for server to close socket
|
||||||
boolean done = assert_wait_until(new WaitUntilDone()
|
boolean done = assert_wait_until(()
|
||||||
{
|
-> server.getState() == PseudoTcpState.TCP_CLOSED, 3000);
|
||||||
@Override
|
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return server.getState() == PseudoTcpState.TCP_CLOSED;
|
|
||||||
}
|
|
||||||
}, 3000);
|
|
||||||
if(!done)
|
if(!done)
|
||||||
{
|
{
|
||||||
fail("Test timed out");
|
fail("Test timed out");
|
||||||
|
|||||||
@ -633,14 +633,9 @@ public abstract class PseudoTcpTestBase
|
|||||||
*/
|
*/
|
||||||
protected boolean assert_Connected_wait(int kConnectTimeoutMs)
|
protected boolean assert_Connected_wait(int kConnectTimeoutMs)
|
||||||
{
|
{
|
||||||
return assert_wait_until(new WaitUntilDone()
|
return assert_wait_until(
|
||||||
{
|
() -> PseudoTcpTestBase.this.have_connected_,
|
||||||
@Override
|
kConnectTimeoutMs);
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return PseudoTcpTestBase.this.have_connected_;
|
|
||||||
}
|
|
||||||
}, kConnectTimeoutMs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -652,14 +647,9 @@ public abstract class PseudoTcpTestBase
|
|||||||
*/
|
*/
|
||||||
protected boolean assert_Disconnected_wait(long kTransferTimeoutMs)
|
protected boolean assert_Disconnected_wait(long kTransferTimeoutMs)
|
||||||
{
|
{
|
||||||
return assert_wait_until(new WaitUntilDone()
|
return assert_wait_until(
|
||||||
{
|
() -> PseudoTcpTestBase.this.have_disconnected_,
|
||||||
@Override
|
kTransferTimeoutMs);
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return PseudoTcpTestBase.this.have_disconnected_;
|
|
||||||
}
|
|
||||||
}, kTransferTimeoutMs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user