所以我在测试我的网络时遇到了问题,下面有很多代码,但这个问题就像我可以做的那么小。我遇到的问题是,当我发送消息时,我正在向服务器注册并且客户端永远不会被调用。
lock.await(5000, TimeUnit.MILLISECONDS)
应该等到收到消息或者在5秒后超时。但是,在设置阻止测试失败的lock.countDown()
变量后,侦听器应该告诉锁定继续response
。
正如您所猜测的那样,这不会发生,消息会被发送,锁定会因超时而继续,并且测试失败,因为response
为空。
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;
}
}
}
答案 0 :(得分:1)
这里的问题是:
00:00 ERROR: [kryonet] Error reading TCP from connection: Connection 1
com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Server.update(Server.java:205)
at com.esotericsoftware.kryonet.Server.run(Server.java:372)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): org.glytching.sandbox.kryo.TestMessage$MessageA
at com.esotericsoftware.kryo.Kryo$DefaultInstantiatorStrategy.newInstantiatorOf(Kryo.java:1308)
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1127)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1136)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:562)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:538)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:816)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:55)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
Kryonet不能反序列化为Message
,因为(a)它是一个非静态内部类,(b)它没有公共零参数构造函数。 FWIW,有一些background here解释为什么Kryonet不支持非静态内部类的序列化。
如果你(a)将Message
重构为自己的类或使其静态,(b)给它一个零参数构造函数,那么你的测试将通过。
进行测试通过所需的最小更改是替换此...
private class Message {
String message;
Message(String message) {
this.message = message;
}
}
......用这个:
private static class Message {
String message;
Message() {
}
Message(String message) {
this.message = message;
}
}
重新:
下面有很多代码,但这是我可以做的一个小例子
你问题中的复制案例绰绰有余,谢谢。