Skip to content

Commit

Permalink
Use a latch and communicate different test outcomes to user.
Browse files Browse the repository at this point in the history
  • Loading branch information
bickelj committed Jan 30, 2016
1 parent dd10989 commit 500c6e9
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions test/org/jitsi/sctp4j/SctpTransferTest.java
Expand Up @@ -16,9 +16,11 @@
package org.jitsi.sctp4j;

import org.junit.*;
import static org.junit.Assert.*;

import java.io.*;
import java.util.*;
import java.util.concurrent.*;

import static org.junit.Assert.assertArrayEquals;

Expand All @@ -37,17 +39,16 @@ public class SctpTransferTest

private final int portB = 5001;

private final Object transferLock = new Object();

/** @GuardedBy("transferLock") */
private byte[] receivedData = null;

/** @GuardedBy("transferLock") */
private boolean dataReady = false;

/** Set random generator seed for consistent tests. */
private static final Random rand = new Random(12345);

private final int ITERATIONS = 10;

/** how long to wait for data during lossy send. */
private final long SECONDS_TO_WAIT = 10;

@Before
public void setUp()
{
Expand Down Expand Up @@ -94,11 +95,15 @@ public void testSocketBrokenLink()
peerB.connect(portA);

byte[] toSendA = createRandomData(2*1024);
for(int i=0; i < 10; i++)
for(int i=0; i < ITERATIONS; i++)
{
System.out.println("Broken Link Test " + (i+1) + " of "
+ ITERATIONS +
". NOTE: IOExceptions may be visible " +
"during this test, and are expected.");
try
{
testTransferPart(peerA, peerB, toSendA);
testTransferPart(peerA, peerB, toSendA, SECONDS_TO_WAIT);
}
catch (Exception e)
{
Expand All @@ -108,37 +113,43 @@ public void testSocketBrokenLink()
}

private void testTransferPart(SctpSocket sender, SctpSocket receiver,
byte[] testData)
byte[] testData, long timeoutInSeconds)
throws Exception
{
final CountDownLatch dataReceivedLatch = new CountDownLatch(1);
boolean noTimeoutOccurred;
receiver.setDataCallback(new SctpDataCallback()
{
@Override
public void onSctpPacket(byte[] data, int sid, int ssn, int tsn,
long ppid,
int context, int flags)
{
synchronized (transferLock)
{
receivedData = data;
dataReady = true;
transferLock.notifyAll();
}
int context, int flags) {
receivedData = data;
dataReceivedLatch.countDown();
}
});

sender.send(testData, true, 0, 0);

synchronized (transferLock)
try
{
noTimeoutOccurred = dataReceivedLatch.await(timeoutInSeconds,
TimeUnit.SECONDS);
if (noTimeoutOccurred) {
assertArrayEquals(testData, receivedData);
} else {
fail("Test data did not get received within " +
timeoutInSeconds + " seconds.");
}
}
catch (InterruptedException ie)
{
while (!dataReady)
transferLock.wait();
assertArrayEquals(testData, receivedData);
dataReady = false;
fail("Test was interrupted: " + ie.toString());
throw ie;
}
}

private static Random getRandom() {
private static Random getRandom()
{
return rand;
}
}

0 comments on commit 500c6e9

Please sign in to comment.