Added tests with multiple clients

This commit is contained in:
senft-lap 2012-11-12 07:44:37 +01:00
parent 3dfbc5c71f
commit 2ccc3cf772

View File

@ -0,0 +1,153 @@
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 BufferedNetworkStackMultipleClientsTests {
private static final String HOST = "localhost";
private static final int PORT = 9999;
private BufferedNetworkStack server;
private BufferedNetworkStackClient client1;
private BufferedNetworkStackClient client2;
@Before
public void setUp() {
client1 = new BufferedNetworkStackClient();
client2 = 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 = i + 2) {
String uuid1 = UUID.randomUUID().toString();
values.add(uuid1);
String uuid2 = UUID.randomUUID().toString();
values.add(uuid2);
try {
client1.push(uuid1, HOST, PORT);
client2.push(uuid2, 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), client1.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 numOfIds1 = 1 + (int) (Math.random() * 50);
StringBuilder uuid1Builder = new StringBuilder(numOfIds1 * 128);
for (int j = 0; j < numOfIds1; j++) {
uuid1Builder.append(UUID.randomUUID().toString());
}
String uuid1 = uuid1Builder.toString().trim();
int numOfIds2 = 1 + (int) (Math.random() * 50);
StringBuilder uuid2Builder = new StringBuilder(numOfIds1 * 128);
for (int j = 0; j < numOfIds2; j++) {
uuid2Builder.append(UUID.randomUUID().toString());
}
String uuid2 = uuid1Builder.toString().trim();
values.add(uuid1);
values.add(uuid2);
try {
client1.push(uuid1, HOST, PORT);
client2.push(uuid2, 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), client1.pop(HOST, PORT));
} catch (IOException | TimeoutException e) {
fail("Couldn't send to server" + e);
}
}
}
@Test
public void testRandomLongLengthWithSpaces() {
List<String> values = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
StringBuilder uuid1Builder = new StringBuilder(4 * 128);
for (int j = 0; j < 4; j++) {
uuid1Builder.append(UUID.randomUUID().toString()).append(" ");
}
String uuid1 = uuid1Builder.toString().trim();
StringBuilder uuid2Builder = new StringBuilder(4 * 128);
for (int j = 0; j < 4; j++) {
uuid2Builder.append(UUID.randomUUID().toString()).append(" ");
}
String uuid2 = uuid1Builder.toString().trim();
values.add(uuid1);
values.add(uuid2);
try {
client1.push(uuid1, HOST, PORT);
client2.push(uuid2, 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), client1.pop(HOST, PORT));
} catch (IOException | TimeoutException e) {
fail("Couldn't send to server" + e);
}
}
}
}