Improve test stability with wait/notify instead of sleeping

This commit is contained in:
Ingo Bauersachs 2021-11-09 20:58:21 +01:00
parent cd2d21a8d0
commit 0811007990
5 changed files with 55 additions and 21 deletions

View File

@ -485,8 +485,11 @@ public class TransactionSupportTests
clientAddress,
responseCollector);
//wait a while
Thread.sleep(1200);
//wait until a response arrives
synchronized (responseCollector)
{
responseCollector.wait(5000);
}
//verify
Vector<StunMessageEvent> reqs
@ -496,7 +499,7 @@ public class TransactionSupportTests
"Not all retransmissions were made for the expected period of time");
//wait for a send
Thread.sleep(1800);
requestCollector.waitForRequest(1800);
//verify
reqs = requestCollector.getRequestsForTransaction(
@ -559,15 +562,25 @@ public class TransactionSupportTests
* Blocks until a request arrives or 50 ms pass.
*/
public void waitForRequest()
{
waitForRequest(50);
}
/**
* Blocks until a request arrives or the timeout passes.
*/
public void waitForRequest(long timeoutMillis)
{
synchronized(this)
{
try
{
wait(50);
wait(timeoutMillis);
}
catch (InterruptedException e)
{}
{
Thread.currentThread().interrupt();
}
}
}
}
@ -592,7 +605,7 @@ public class TransactionSupportTests
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected void processFailure(BaseStunMessageEvent event)
protected synchronized void processFailure(BaseStunMessageEvent event)
{
String receivedResponse;
@ -603,6 +616,7 @@ public class TransactionSupportTests
else
receivedResponse = "failure";
receivedResponses.add(receivedResponse);
notifyAll();
}
/**
@ -610,9 +624,10 @@ public class TransactionSupportTests
*
* @param response the event to log.
*/
public void processResponse(StunResponseEvent response)
public synchronized void processResponse(StunResponseEvent response)
{
receivedResponses.add(response);
notifyAll();
}
}
}

View File

@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;
import org.junit.jupiter.api.*;
@ -37,6 +38,7 @@ public class PseudoTcpStreamTest
* Test one-way transfer with @link(PseudoTcpStream)
*/
@Test
@Timeout(10)
public void testConnectTransferClose()
throws IOException
{
@ -57,6 +59,8 @@ public class PseudoTcpStreamTest
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
final InetSocketAddress serverAddress =
new InetSocketAddress(InetAddress.getLoopbackAddress(), server.getLocalPort());
AtomicBoolean clientThreadEnded = new AtomicBoolean();
AtomicBoolean serverThreadEnded = new AtomicBoolean();
Thread serverThread = new Thread(() ->
{
try
@ -76,6 +80,7 @@ public class PseudoTcpStreamTest
receiveBuffer(server.getInputStream(), sizeB);
assertArrayEquals(bufferB, recvdBufferB);
// server.close();
serverThreadEnded.set(true);
}
catch (IOException ex)
{
@ -121,6 +126,7 @@ public class PseudoTcpStreamTest
assertEquals(sizeB, written);
os.flush();
client.close();
clientThreadEnded.set(true);
}
catch (IOException ex)
{
@ -137,8 +143,16 @@ public class PseudoTcpStreamTest
transferTimeout);
if (success)
{
clientThread.join();
serverThread.join();
clientThread.join(10_000);
if (!clientThreadEnded.get())
{
fail("client thread did not end");
}
serverThread.join(10_000);
if (!serverThreadEnded.get())
{
fail("server thread did not end");
}
server.close();
}
else

View File

@ -608,9 +608,9 @@ public abstract class PseudoTcpTestBase
runClocks = false;
localClockThread.interrupt();
remoteClockThread.interrupt();
localClockThread.join();
localClockThread.join(5000);
localClockThread = null;
remoteClockThread.join();
remoteClockThread.join(5000);
remoteClockThread = null;
}
catch (InterruptedException ex)

View File

@ -63,6 +63,11 @@ public class DatagramCollector
}
public void waitForPacket()
{
waitForPacket(50);
}
public void waitForPacket(long timeoutMillis)
{
synchronized(this)
{
@ -71,12 +76,12 @@ public class DatagramCollector
try
{
wait(50);
wait(timeoutMillis);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}

View File

@ -18,6 +18,7 @@
package org.ice4j.stack;
import java.net.*;
import java.util.concurrent.*;
import java.util.logging.*;
import static org.junit.jupiter.api.Assertions.*;
@ -50,7 +51,7 @@ public class ShallowStackTest
private TransportAddress dummyServerAddress = null;
private TransportAddress localAddress = null;
private DatagramCollector dgramCollector = new DatagramCollector();
private final DatagramCollector dgramCollector = new DatagramCollector();
private IceSocketWrapper localSock = null;
@ -127,7 +128,7 @@ public class ShallowStackTest
new SimpleResponseCollector());
//wait for its arrival
dgramCollector.waitForPacket();
dgramCollector.waitForPacket(1000);
DatagramPacket receivedPacket = dgramCollector.collectPacket();
@ -145,7 +146,7 @@ public class ShallowStackTest
dgramCollector.startListening(dummyServerSocket);
dgramCollector.waitForPacket();
dgramCollector.waitForPacket(1000);
receivedPacket = dgramCollector.collectPacket();
@ -318,7 +319,7 @@ public class ShallowStackTest
dgramCollector.startListening(dummyServerSocket);
long firstTime = System.currentTimeMillis();
long firstTime = System.nanoTime();
stunStack.sendRequest(bindingRequest,
dummyServerAddress,
@ -326,7 +327,7 @@ public class ShallowStackTest
new SimpleResponseCollector());
//wait for its arrival
dgramCollector.waitForPacket();
dgramCollector.waitForPacket(1000);
DatagramPacket receivedPacket = dgramCollector.collectPacket();
assertTrue((receivedPacket.getLength() > 0),
@ -341,7 +342,7 @@ public class ShallowStackTest
// wait for the 1st retransmission with originalWait
dgramCollector.startListening(dummyServerSocket);
dgramCollector.waitForPacket();
dgramCollector.waitForPacket(1000);
receivedPacket = dgramCollector.collectPacket();
assertTrue((receivedPacket.getLength() > 0),
@ -355,8 +356,7 @@ public class ShallowStackTest
"The retransmitted request did not match the original.");
// verify the retransmission is longer than the originalWait
long secondTime = System.currentTimeMillis();
assertTrue((secondTime - firstTime) >= originalWait);
assertTrue(System.nanoTime() - firstTime >= TimeUnit.MILLISECONDS.toNanos(originalWait));
System.clearProperty(StackProperties.FIRST_CTRAN_RETRANS_AFTER);
}