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 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
|
||||
* 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;
|
||||
|
||||
protected interface WaitUntilDone
|
||||
{
|
||||
boolean isDone();
|
||||
}
|
||||
|
||||
protected boolean assert_wait_until(WaitUntilDone wait, long timeoutMs)
|
||||
protected boolean assert_wait_until(BooleanSupplier wait, long timeoutMs)
|
||||
{
|
||||
long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMs);
|
||||
try
|
||||
{
|
||||
long start = System.currentTimeMillis();
|
||||
while (!wait.isDone()
|
||||
&& (System.currentTimeMillis() - start) < timeoutMs)
|
||||
long start = System.nanoTime();
|
||||
while (!wait.getAsBoolean() && (System.nanoTime() - start) < timeoutNanos)
|
||||
{
|
||||
synchronized (testLock)
|
||||
{
|
||||
@ -74,7 +74,7 @@ public class MultiThreadSupportTest implements Thread.UncaughtExceptionHandler
|
||||
}
|
||||
}
|
||||
}
|
||||
return wait.isDone();
|
||||
return wait.getAsBoolean();
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
|
||||
@ -137,14 +137,9 @@ public class PseudoTcpStreamTest
|
||||
clientThread.start();
|
||||
try
|
||||
{
|
||||
boolean success = assert_wait_until(new WaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return client.getState() == PseudoTcpState.TCP_CLOSED;
|
||||
}
|
||||
}, transferTimeout);
|
||||
boolean success = assert_wait_until(
|
||||
() -> client.getState() == PseudoTcpState.TCP_CLOSED,
|
||||
transferTimeout);
|
||||
if (success)
|
||||
{
|
||||
clientThread.join();
|
||||
@ -207,72 +202,58 @@ public class PseudoTcpStreamTest
|
||||
Thread.setDefaultUncaughtExceptionHandler(this);
|
||||
final PseudoTcpSocketImpl server;
|
||||
final PseudoTcpSocketImpl client;
|
||||
final int server_port = 49998;
|
||||
final InetSocketAddress serverAddress =
|
||||
new InetSocketAddress(InetAddress.getLocalHost(), server_port);
|
||||
server =
|
||||
new PseudoTcpSocketImpl(0,new DatagramSocket(serverAddress));
|
||||
DatagramSocket serverSocket = new DatagramSocket(0, InetAddress.getLoopbackAddress());
|
||||
server = new PseudoTcpSocketImpl(0, serverSocket);
|
||||
client = new PseudoTcpSocketImpl(0);
|
||||
//Servers thread waiting for connection
|
||||
new Thread(new Runnable()
|
||||
new Thread(() ->
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
try
|
||||
{
|
||||
server.accept(2000);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try
|
||||
{
|
||||
testOperation.testTimeout(server);
|
||||
fail("No expected timeout occurred on operation");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
//success
|
||||
try
|
||||
{
|
||||
server.accept(2000);
|
||||
server.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try
|
||||
{
|
||||
testOperation.testTimeout(server);
|
||||
fail("No expected timeout occured on operation");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
//success
|
||||
try
|
||||
{
|
||||
server.close();
|
||||
}
|
||||
catch (IOException exc)
|
||||
{
|
||||
throw new RuntimeException(exc);
|
||||
}
|
||||
throw new RuntimeException(exc);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
//Clients thread connects and closes socket
|
||||
new Thread(new Runnable()
|
||||
new Thread(() ->
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
client.connect(serverAddress, 2000);
|
||||
Thread.sleep(500);
|
||||
client.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
client.connect(new InetSocketAddress(
|
||||
InetAddress.getLoopbackAddress(),
|
||||
serverSocket.getLocalPort()),
|
||||
2000);
|
||||
Thread.sleep(500);
|
||||
client.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
//Waits for server to close socket
|
||||
boolean done = assert_wait_until(new WaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return server.getState() == PseudoTcpState.TCP_CLOSED;
|
||||
}
|
||||
}, 3000);
|
||||
boolean done = assert_wait_until(()
|
||||
-> server.getState() == PseudoTcpState.TCP_CLOSED, 3000);
|
||||
if(!done)
|
||||
{
|
||||
fail("Test timed out");
|
||||
|
||||
@ -633,14 +633,9 @@ public abstract class PseudoTcpTestBase
|
||||
*/
|
||||
protected boolean assert_Connected_wait(int kConnectTimeoutMs)
|
||||
{
|
||||
return assert_wait_until(new WaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return PseudoTcpTestBase.this.have_connected_;
|
||||
}
|
||||
}, kConnectTimeoutMs);
|
||||
return assert_wait_until(
|
||||
() -> PseudoTcpTestBase.this.have_connected_,
|
||||
kConnectTimeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -652,14 +647,9 @@ public abstract class PseudoTcpTestBase
|
||||
*/
|
||||
protected boolean assert_Disconnected_wait(long kTransferTimeoutMs)
|
||||
{
|
||||
return assert_wait_until(new WaitUntilDone()
|
||||
{
|
||||
@Override
|
||||
public boolean isDone()
|
||||
{
|
||||
return PseudoTcpTestBase.this.have_disconnected_;
|
||||
}
|
||||
}, kTransferTimeoutMs);
|
||||
return assert_wait_until(
|
||||
() -> PseudoTcpTestBase.this.have_disconnected_,
|
||||
kTransferTimeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user