关闭

mina基础及服务端接口测试

发表于:2013-12-16 11:06

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:陈磊    来源:51Testing软件测试网采编

  3、针对服务端通信规则重写编解码方法,支持断包粘包
public class ClientDecoder extends CumulativeProtocolDecoder {
public static Logger logger = Logger.getLogger(ClientDecoder.class);
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
IoBuffer buf = IoBuffer.allocate(0).setAutoExpand(true);
IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
byte [] head = new byte[16];
logger.info(in.remaining());
if (in.remaining() > 0) {
int msgLen = 16;// 16字节判断消息长度
if (in.remaining() > msgLen) {
in.mark();
in.get(head, 0, 16);
buf.put(XorParamCodec.decryptXOR(head));
buf.flip();
int length=buf.getInt();
if (length - msgLen > in.remaining()) {
in.reset();
return false;
} else {
in.position(0);
while (in.hasRemaining()) {
buffer.put(in.get());
}
buffer.flip();
out.write(buffer);
return false;
}
}
}
return false;
}
}
  4、编写Test发送数据
@Before
public void beforeTest() {
ip = PropertiesHandle.readValue("ip");
port = Integer.valueOf(PropertiesHandle.readValue("port"));
mmc = new MyMinaClient();
}
@Test
public void testUpdate() throws Exception {
IoConnector connector = mmc.creatClient();
IoSession session = mmc.getIoSession(connector, ip, port);
mmc.sendMsg(session, ClientRegister.getRegisterInfo()); \\ClientRegister和ClientUpdate类是客户端发送的数据
mmc.sendMsg(session, ClientUpdate.getUpdateInfo());
mmc.close(session, connector);
Assert.assertEquals(AcctGuardData.Registerr_Rec, HeaderInfo.response);
}
33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号