use of io.servicecomb.transport.highway.message.RequestHeader in project java-chassis by ServiceComb.
the class TestHighwayClient method testCreateLogin.
@Test
public void testCreateLogin() throws Exception {
HighwayClient client = new HighwayClient(false);
TcpOutputStream os = client.createLogin();
ByteBuf buf = os.getBuffer().getByteBuf();
byte[] magic = new byte[TcpParser.TCP_MAGIC.length];
buf.readBytes(magic);
Assert.assertArrayEquals(TcpParser.TCP_MAGIC, magic);
Assert.assertEquals(os.getMsgId(), buf.readLong());
int start = TcpParser.TCP_HEADER_LENGTH;
int totalLen = buf.readInt();
int headerLen = buf.readInt();
Buffer headerBuffer = os.getBuffer().slice(start, start + headerLen);
int end = start + totalLen;
start += headerLen;
Buffer bodyBuffer = os.getBuffer().slice(start, end);
RequestHeader header = RequestHeader.readObject(headerBuffer);
Assert.assertEquals(MsgType.LOGIN, header.getMsgType());
LoginRequest login = LoginRequest.readObject(bodyBuffer);
Assert.assertEquals(HighwayTransport.NAME, login.getProtocol());
}
use of io.servicecomb.transport.highway.message.RequestHeader in project java-chassis by ServiceComb.
the class TestHighwayCodec method testReadRequestHeader.
@Test
public void testReadRequestHeader() {
boolean status = true;
try {
new MockUp<NotWrapSchema>() {
@Mock
public Object readObject(Input input) throws IOException {
return new RequestHeader();
}
};
bodyBuffer = Buffer.buffer("\"abc\"");
RequestHeader requestHeader = HighwayCodec.readRequestHeader(bodyBuffer);
Assert.assertNotNull(requestHeader);
Assert.assertEquals(0, requestHeader.getFlags());
} catch (Exception e) {
status = false;
}
Assert.assertTrue(status);
}
use of io.servicecomb.transport.highway.message.RequestHeader in project java-chassis by ServiceComb.
the class TestHighwayServerConnection method testRequestError.
@Test
public void testRequestError() throws Exception {
header.setMsgType(MsgType.REQUEST);
Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
Buffer bodyBuffer = Buffer.buffer();
Holder<Boolean> holder = new Holder<>(false);
new MockUp<HighwayServerInvoke>() {
@Mock
public boolean init(NetSocket netSocket, long msgId, RequestHeader header, Buffer bodyBuffer) {
return false;
}
};
connection.handle(0, headerBuffer, bodyBuffer);
Assert.assertEquals(null, connection.getProtocol());
Assert.assertEquals(null, connection.getZipName());
Assert.assertEquals(false, holder.value);
}
use of io.servicecomb.transport.highway.message.RequestHeader in project java-chassis by ServiceComb.
the class TestHighwayServerInvoke method test.
@Test
public void test() {
MockUtil.getInstance().mockHighwayCodec();
SchemaMeta schemaMeta = unitTestMeta.getOrCreateSchemaMeta(Impl.class);
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
operationMeta.setExecutor(new ReactiveExecutor());
HighwayServerInvoke highwayServerInvoke = new HighwayServerInvoke();
highwayServerInvoke.setMicroserviceMetaManager(unitTestMeta.getMicroserviceMetaManager());
RequestHeader requestHeader = MockUtil.getInstance().requestHeader;
// 初始化失败
requestHeader.setDestMicroservice(null);
Assert.assertFalse(highwayServerInvoke.init(netSocket, 0, null, null));
// 初始化成功
requestHeader.setDestMicroservice(schemaMeta.getMicroserviceName());
requestHeader.setSchemaId(schemaMeta.getSchemaId());
requestHeader.setOperationName(operationMeta.getOperationId());
Assert.assertTrue(highwayServerInvoke.init(netSocket, 0, requestHeader, null));
// exe成功
netSocketBuffer = null;
highwayServerInvoke.execute();
Assert.assertEquals(null, netSocketBuffer);
// exe失败
MockUtil.getInstance().decodeRequestSucc = false;
highwayServerInvoke.execute();
Assert.assertEquals(true, netSocketBuffer.toString().startsWith("CSE.TCP"));
}
use of io.servicecomb.transport.highway.message.RequestHeader in project java-chassis by ServiceComb.
the class HighwayClient method createLogin.
@Override
public TcpOutputStream createLogin() {
try {
LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
ProtobufOutput output = new ProtobufOutput(linkedBuffer);
RequestHeader header = new RequestHeader();
header.setMsgType(MsgType.LOGIN);
header.writeObject(output);
LoginRequest login = new LoginRequest();
login.setProtocol(HighwayTransport.NAME);
login.writeObject(output);
HighwayOutputStream os = new HighwayOutputStream();
os.write(header, LoginRequest.getLoginRequestSchema(), login);
return os;
} catch (Throwable e) {
throw new Error("impossible.", e);
}
}
Aggregations