Commit 4df6aff9 authored by Winfried Lötzsch's avatar Winfried Lötzsch

Merge branch 'improve-clientLogin-test' into 'master'

Improve client login test

The client login test should not fail that often now.

See merge request !11
parents 745edbdc e184dced
......@@ -3,7 +3,7 @@ plugins {
id 'net.ltgt.errorprone' version '0.0.8'
}
version = '0.2.0'
version = '0.2.1'
group = 'gone.corewars.library'
apply plugin: 'java'
......@@ -23,6 +23,7 @@ dependencies {
compile 'org.json:json:20160810'
compile 'org.everit.json:org.everit.json.schema:1.+'
testCompile 'junit:junit:4.+'
testCompile 'org.awaitility:awaitility:2.+'
}
checkstyle {
......
......@@ -67,7 +67,7 @@ public class CwClient implements IClient, ITcpListener {
}
@Override
public void receivedMessage(String sessionId, String data) {
public void receiveMessage(String sessionId, String data) {
CwMessageType type = interpreter.getMessageType(data);
if (type == CwMessageType.LoginOk) {
......
......@@ -68,7 +68,7 @@ public class CwServer implements IServer, ITcpListener {
}
@Override
public void receivedMessage(String sessionId, String data) {
public void receiveMessage(String sessionId, String data) {
CwMessageType type = interpreter.getMessageType(data);
if (type == CwMessageType.Login) {
......
......@@ -4,5 +4,5 @@ package gone.lib.network.contracts;
* Created by Winfried on 22.09.2016.
*/
public interface ITcpListener {
void receivedMessage(String sessionId, String data);
void receiveMessage(String sessionId, String data);
}
......@@ -57,7 +57,7 @@ public class OutgoingMessagesSerializer {
.key("content")
.object()
.key("warriorRedcode").value(String.join(
NetworkConstants.REDCODESEPARATOR, content.warriorRedcode))
NetworkConstants.REDCODESEPARATOR, content.warriorRedcode))
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("ready")
......@@ -84,14 +84,14 @@ public class OutgoingMessagesSerializer {
public String serializeStartMessage(CwStartContent content) {
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("fullState").value(String.join(
NetworkConstants.REDCODESEPARATOR, content.fullState))
.key("players").value(content.players)
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("start")
.key("content")
.object()
.key("fullState").value(String.join(
NetworkConstants.REDCODESEPARATOR, content.fullState))
.key("players").value(content.players)
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("start")
.endObject().toString();
return result;
}
......
......@@ -71,9 +71,9 @@ public class TcpClientModule implements ITcpClientModule, ITcpListener {
}
@Override
public void receivedMessage(String sessionId, String data) {
public void receiveMessage(String sessionId, String data) {
for (ITcpListener listener: listeners) {
listener.receivedMessage(sessionId, data);
listener.receiveMessage(sessionId, data);
}
}
}
\ No newline at end of file
......@@ -44,7 +44,7 @@ public class TcpReceiveMessage implements Runnable {
while (!line.equals(NetworkConstants.FINISHINGLINE));
for (ITcpListener listener : listeners) {
listener.receivedMessage(this.sessionId, clientData.toString());
listener.receiveMessage(this.sessionId, clientData.toString());
}
}
} catch (Exception exception) {
......
......@@ -76,9 +76,9 @@ public class TcpServerModule implements ITcpServerModule, ITcpServerAccepted, IT
}
@Override
public void receivedMessage(String sessionId, String data) {
public void receiveMessage(String sessionId, String data) {
for (ITcpListener listener: listeners) {
listener.receivedMessage(sessionId, data);
listener.receiveMessage(sessionId, data);
}
}
}
\ No newline at end of file
package gone.lib.network.test;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Awaitility.fieldIn;
import static org.junit.Assert.assertTrue;
import gone.lib.common.ClientRole;
import gone.lib.network.contracts.ITcpListener;
import gone.lib.network.json.CwLoginContent;
......@@ -8,9 +13,11 @@ import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
import gone.lib.network.tcp.TcpClientModule;
import gone.lib.network.tcp.TcpServerModule;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.Callable;
/**
* Created by winfr on 26.09.2016.
*/
......@@ -21,40 +28,34 @@ public class ClientLoginTest {
}
@Override
public void receivedMessage(String sessionId, String data) {
public void receiveMessage(String sessionId, String data) {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
receivedLogin = interpreter.getMessageType(data) == CwMessageType.Login;
}
public boolean receivedLogin;
private boolean receivedLogin;
}
@Test
public void testSingleClientLogin() throws InterruptedException {
TcpServerModule server = new TcpServerModule();
ServerListener listener = new ServerListener();
server.registerListener(listener);
server.start(5556);
TcpClientModule client = new TcpClientModule();
client.connect("localhost", 5556);
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
CwLoginContent content = new CwLoginContent();
content.name = "testWarrior";
content.role = ClientRole.Player;
Thread.sleep(100);
client.sendMessage(serializer.serializeLoginMessage(content));
Thread.sleep(100);
Callable<Boolean> hasReceivedLogin = fieldIn(listener).ofType(boolean.class).andWithName("receivedLogin");
await().atMost(1, SECONDS).until(hasReceivedLogin);
server.stop();
client.disconnect();
Assert.assertTrue(listener.receivedLogin);
assertTrue(listener.receivedLogin);
}
}
......@@ -8,7 +8,6 @@ import gone.lib.network.contracts.ITcpServerAccepted;
import gone.lib.network.tcp.TcpClientModule;
import gone.lib.network.tcp.TcpServerAccept;
import org.junit.Assert;
import org.junit.Test;
/**
......
package gone.lib.network.test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import gone.lib.common.ClientRole;
import gone.lib.common.RcStandard;
import gone.lib.network.json.*;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedList;
......@@ -15,7 +22,7 @@ import java.util.LinkedList;
*/
public class SerializationTest {
@Test
public void TestGameStatusMessage() {
public void testGameStatusMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
......@@ -31,15 +38,15 @@ public class SerializationTest {
CwGameStatusContent newContent = interpreter.parseGameStatusMessage(parsedMessage);
Assert.assertEquals(content.indexOfChangedCoreElement, newContent.indexOfChangedCoreElement);
Assert.assertEquals(content.newIpIndex, newContent.newIpIndex);
Assert.assertEquals(content.oldIpIndex, newContent.oldIpIndex);
Assert.assertEquals(content.newOpCode, newContent.newOpCode);
Assert.assertEquals(content.playerOneActive, newContent.playerOneActive);
assertThat(content.indexOfChangedCoreElement, is(newContent.indexOfChangedCoreElement));
assertThat(content.newIpIndex, is(newContent.newIpIndex));
assertThat(content.oldIpIndex, is(newContent.oldIpIndex));
assertThat(content.newOpCode, is(newContent.newOpCode));
assertThat(content.playerOneActive, is(newContent.playerOneActive));
}
@Test
public void TestLoginMessage() {
public void testLoginMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
......@@ -52,12 +59,12 @@ public class SerializationTest {
CwLoginContent newContent = interpreter.parseLoginMessage(parsedMessage);
Assert.assertEquals(content.name, newContent.name);
Assert.assertEquals(content.role, newContent.role);
assertThat(content.name, is(newContent.name));
assertThat(content.role, is(newContent.role));
}
@Test
public void TestLoginOkMessage() {
public void testLoginOkMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
......@@ -72,13 +79,13 @@ public class SerializationTest {
CwLoginOkContent newContent = interpreter.parseLoginOkMessage(parsedMessage);
Assert.assertEquals(content.lineLength, newContent.lineLength);
Assert.assertEquals(content.coreSize, newContent.coreSize);
Assert.assertEquals(content.clientMustWait, newContent.clientMustWait);
assertThat(content.lineLength, is(newContent.lineLength));
assertThat(content.coreSize, is(newContent.coreSize));
assertThat(content.clientMustWait, is(newContent.clientMustWait));
}
@Test
public void TestReadyMessage() {
public void testReadyMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
......@@ -93,15 +100,14 @@ public class SerializationTest {
CwReadyContent newContent = interpreter.parseReadyMessage(parsedMessage);
Assert.assertEquals(newContent.warriorRedcode.size(), content.warriorRedcode.size());
newContent.warriorRedcode.get(0).equals(content.warriorRedcode.get(0));
newContent.warriorRedcode.get(1).equals(content.warriorRedcode.get(1));
newContent.warriorRedcode.get(2).equals(content.warriorRedcode.get(2));
assertThat(newContent.warriorRedcode.size(), is(content.warriorRedcode.size()));
assertThat(newContent.warriorRedcode.get(0), is(content.warriorRedcode.get(0)));
assertThat(newContent.warriorRedcode.get(1), is(content.warriorRedcode.get(1)));
assertThat(newContent.warriorRedcode.get(2), is(content.warriorRedcode.get(2)));
}
@Test
public void TestStartMessage() {
public void testStartMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
......@@ -119,13 +125,13 @@ public class SerializationTest {
CwStartContent newContent = interpreter.parseStartMessage(parsedMessage);
Assert.assertEquals(content.players, newContent.players);
Assert.assertEquals(newContent.fullState.size(), content.fullState.size());
assertThat(content.players, is(newContent.players));
assertThat(content.fullState, is(newContent.fullState));
assertThat(newContent.fullState.size(), is(content.fullState.size()));
newContent.fullState.get(0).equals(content.fullState.get(0));
newContent.fullState.get(1).equals(content.fullState.get(1));
newContent.fullState.get(2).equals(content.fullState.get(2));
newContent.fullState.get(3).equals(content.fullState.get(3));
assertThat(newContent.fullState.get(0), is(content.fullState.get(0)));
assertThat(newContent.fullState.get(2), is(content.fullState.get(2)));
assertThat(newContent.fullState.get(3), is(content.fullState.get(3)));
assertThat(newContent.fullState.get(1), is(content.fullState.get(1)));
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment