Implemented JUnit tests
This commit is contained in:
parent
bc6b9d1563
commit
e0d8285b57
@ -2,5 +2,6 @@
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
||||
@ -0,0 +1,99 @@
|
||||
package tests;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import server.BufferedNetworkStack;
|
||||
import client.BufferedNetworkStackClient;
|
||||
import client.TimeoutException;
|
||||
|
||||
public class BufferedNetworkStackTests {
|
||||
|
||||
private static final String HOST = "localhost";
|
||||
private static final int PORT = 9999;
|
||||
|
||||
private BufferedNetworkStack server;
|
||||
private BufferedNetworkStackClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
client = new BufferedNetworkStackClient();
|
||||
try {
|
||||
server = new BufferedNetworkStack(PORT);
|
||||
} catch (IOException e) {
|
||||
fail("Couldn't listen on port " + PORT + "." + e);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedShortLength() {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
values.add(uuid);
|
||||
|
||||
try {
|
||||
client.push(uuid, HOST, PORT);
|
||||
} catch (IOException | TimeoutException e) {
|
||||
fail("Couldn't send to server" + e);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = values.size() - 1; i >= 0; i--) {
|
||||
try {
|
||||
assertEquals(values.get(i), client.pop(HOST, PORT));
|
||||
} catch (IOException | TimeoutException e) {
|
||||
fail("Couldn't send to server" + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomLongLength() {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
// We generate messages of random sizes so the buffers are filled
|
||||
// differently every time
|
||||
int numOfIds = 1 + (int) (Math.random() * 50);
|
||||
|
||||
StringBuilder uuidBuilder = new StringBuilder(numOfIds * 128);
|
||||
for (int j = 0; j < numOfIds; j++) {
|
||||
uuidBuilder.append(UUID.randomUUID().toString());
|
||||
}
|
||||
String uuid = uuidBuilder.toString();
|
||||
|
||||
values.add(uuid);
|
||||
try {
|
||||
client.push(uuid, HOST, PORT);
|
||||
} catch (IOException e) {
|
||||
fail("Couldn't send to server. " + e);
|
||||
} catch (TimeoutException e) {
|
||||
fail("Connection timed out. " + e);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = values.size() - 1; i >= 0; i--) {
|
||||
try {
|
||||
assertEquals(values.get(i), client.pop(HOST, PORT));
|
||||
} catch (IOException | TimeoutException e) {
|
||||
fail("Couldn't send to server" + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user