Cleaned up reconnect test

This commit is contained in:
nathan 2018-03-27 23:02:23 +02:00
parent e44ee694c7
commit 9fd33f242f

View File

@ -33,20 +33,42 @@ import dorkbox.util.exceptions.SecurityException;
public public
class ReconnectTest extends BaseTest { class ReconnectTest extends BaseTest {
AtomicInteger serverCount; AtomicInteger receivedCount;
AtomicInteger clientCount;
@Test @Test
public public
void socketReuse() throws SecurityException, IOException { void socketReuseUDP() throws IOException, SecurityException {
this.serverCount = new AtomicInteger(0); socketReuse(false, true);
this.clientCount = new AtomicInteger(0); }
@Test
public
void socketReuseTCP() throws IOException, SecurityException {
socketReuse(true, false);
}
@Test
public
void socketReuseTCPUDP() throws IOException, SecurityException {
socketReuse(true, true);
}
private
void socketReuse(final boolean useTCP, final boolean useUDP) throws SecurityException, IOException {
this.receivedCount = new AtomicInteger(0);
Configuration configuration = new Configuration(); Configuration configuration = new Configuration();
// configuration.tcpPort = tcpPort;
configuration.udpPort = udpPort;
configuration.host = host; configuration.host = host;
if (useTCP) {
configuration.tcpPort = tcpPort;
}
if (useUDP) {
configuration.udpPort = udpPort;
}
Server server = new Server(configuration); Server server = new Server(configuration);
addEndPoint(server); addEndPoint(server);
final Listeners listeners = server.listeners(); final Listeners listeners = server.listeners();
@ -54,17 +76,21 @@ class ReconnectTest extends BaseTest {
@Override @Override
public public
void connected(Connection connection) { void connected(Connection connection) {
// connection.send() if (useTCP) {
// .TCP("-- TCP from server"); connection.send()
connection.send() .TCP("-- TCP from server");
.UDP("-- UDP from server"); }
if (useUDP) {
connection.send()
.UDP("-- UDP from server");
}
} }
}); });
listeners.add(new Listener.OnMessageReceived<Connection, String>() { listeners.add(new Listener.OnMessageReceived<Connection, String>() {
@Override @Override
public public
void received(Connection connection, String object) { void received(Connection connection, String object) {
int incrementAndGet = ReconnectTest.this.serverCount.incrementAndGet(); int incrementAndGet = ReconnectTest.this.receivedCount.incrementAndGet();
System.out.println("----- <S " + connection + "> " + incrementAndGet + " : " + object); System.out.println("----- <S " + connection + "> " + incrementAndGet + " : " + object);
} }
}); });
@ -78,33 +104,40 @@ class ReconnectTest extends BaseTest {
@Override @Override
public public
void connected(Connection connection) { void connected(Connection connection) {
// connection.send() if (useTCP) {
// .TCP("-- TCP from client"); connection.send()
connection.send() .TCP("-- TCP from client");
.UDP("-- UDP from client"); }
if (useUDP) {
connection.send()
.UDP("-- UDP from client");
}
} }
}); });
listeners1.add(new Listener.OnMessageReceived<Connection, String>() { listeners1.add(new Listener.OnMessageReceived<Connection, String>() {
@Override @Override
public public
void received(Connection connection, String object) { void received(Connection connection, String object) {
int incrementAndGet = ReconnectTest.this.clientCount.incrementAndGet(); int incrementAndGet = ReconnectTest.this.receivedCount.incrementAndGet();
System.out.println("----- <C " + connection + "> " + incrementAndGet + " : " + object); System.out.println("----- <C " + connection + "> " + incrementAndGet + " : " + object);
} }
}); });
server.bind(false); server.bind(false);
int count = 100; int count = 10;
int initialCount = 2;
if (useTCP && useUDP) {
initialCount += 2;
}
for (int i = 1; i < count + 1; i++) { for (int i = 1; i < count + 1; i++) {
client.connect(5000); client.connect(5000);
int waitingRetryCount = 10; int waitingRetryCount = 10;
// int target = i * 2; int target = i * initialCount;
int target = i; while (this.receivedCount.get() != target) {
while (this.serverCount.get() != target || this.clientCount.get() != target) {
if (waitingRetryCount-- < 0) { if (waitingRetryCount-- < 0) {
throw new IOException("Unable to reconnect in 5000 ms"); throw new IOException("Invalid target count...");
} }
try { try {
Thread.sleep(100); Thread.sleep(100);
@ -115,7 +148,7 @@ class ReconnectTest extends BaseTest {
client.closeConnections(); client.closeConnections();
} }
assertEquals(count * 2 * 2, this.clientCount.get() + this.serverCount.get()); assertEquals(count * initialCount, this.receivedCount.get());
stopEndPoints(); stopEndPoints();
waitForThreads(10); waitForThreads(10);
@ -124,8 +157,7 @@ class ReconnectTest extends BaseTest {
@Test @Test
public public
void localReuse() throws SecurityException, IOException { void localReuse() throws SecurityException, IOException {
this.serverCount = new AtomicInteger(0); this.receivedCount = new AtomicInteger(0);
this.clientCount = new AtomicInteger(0);
Server server = new Server(); Server server = new Server();
addEndPoint(server); addEndPoint(server);
@ -143,7 +175,7 @@ class ReconnectTest extends BaseTest {
@Override @Override
public public
void received(Connection connection, String object) { void received(Connection connection, String object) {
int incrementAndGet = ReconnectTest.this.serverCount.incrementAndGet(); int incrementAndGet = ReconnectTest.this.receivedCount.incrementAndGet();
System.out.println("----- <S " + connection + "> " + incrementAndGet + " : " + object); System.out.println("----- <S " + connection + "> " + incrementAndGet + " : " + object);
} }
}); });
@ -167,7 +199,7 @@ class ReconnectTest extends BaseTest {
@Override @Override
public public
void received(Connection connection, String object) { void received(Connection connection, String object) {
int incrementAndGet = ReconnectTest.this.clientCount.incrementAndGet(); int incrementAndGet = ReconnectTest.this.receivedCount.incrementAndGet();
System.out.println("----- <C " + connection + "> " + incrementAndGet + " : " + object); System.out.println("----- <C " + connection + "> " + incrementAndGet + " : " + object);
} }
}); });
@ -178,7 +210,7 @@ class ReconnectTest extends BaseTest {
client.connect(5000); client.connect(5000);
int target = i; int target = i;
while (this.serverCount.get() != target || this.clientCount.get() != target) { while (this.receivedCount.get() != target) {
System.out.println("----- Waiting..."); System.out.println("----- Waiting...");
try { try {
Thread.sleep(100); Thread.sleep(100);
@ -189,7 +221,7 @@ class ReconnectTest extends BaseTest {
client.closeConnections(); client.closeConnections();
} }
assertEquals(count * 2, this.clientCount.get() + this.serverCount.get()); assertEquals(count * 2, this.receivedCount.get());
stopEndPoints(); stopEndPoints();
waitForThreads(10); waitForThreads(10);