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