So I'm having a problem testing my networking, there's a lot of code below but this is as small as I could make an example. The problem I'm having is that it seems the listener I'm registering with the server and client never gets called when when I send a message.
lock.await(5000, TimeUnit.MILLISECONDS)
should wait until the message is received or timeout at 5 seconds. However the listener should tell the lock to continue lock.countDown()
after it sets the response
variable that prevents the test from failing.
As you can guess, this doesn't happen, the message gets sent, the lock continues due to timeout and the test fails because response
is null.
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static junit.framework.TestCase.fail;
public class TestMessage {
private Server server;
private Client client;
private String response;
private CountDownLatch lock = new CountDownLatch(1);
@Before
public void setUp() {
Log.set(Log.LEVEL_DEBUG);
server = new Server();
try {
Log.debug("Binding to port: " + 30454 + "... ");
server.bind(30454);
Log.debug("Bound to port" + 30454);
} catch (IOException e) {
Log.debug("failed");
Log.error("Unable to bind to port: " + e.getMessage());
server.stop();
fail("Unable to start server");
}
Kryo kryo = server.getKryo();
kryo.register(Message.class);
Log.debug("Adding server listener");
server.addListener(new TestListener());
Log.debug("Starting server... ");
server.start();
Log.debug("Server started successfully");
}
@Test
public void testPacketSending() throws IOException, InterruptedException {
client = new Client();
client.addListener(new TestListener());
Kryo kryo = client.getKryo();
kryo.register(Message.class);
client.start();
client.connect(5000, "127.0.0.1", 30454);
client.sendTCP(new Message("RECEIVED"));
lock.await(5000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(response);
}
private class TestListener extends Listener {
@Override
public void received(Connection connection, Object o) {
Message m = (Message) o;
response = m.message;
Log.debug(m.message);
lock.countDown();
}
}
private class Message {
String message;
Message(String message) {
this.message = message;
}
}
}