Search in sources :

Example 1 with RequestHeader

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());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest) ByteBuf(io.netty.buffer.ByteBuf) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Endpoint(io.servicecomb.core.Endpoint) Test(org.junit.Test)

Example 2 with RequestHeader

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);
}
Also used : Input(io.protostuff.Input) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) MockUp(mockit.MockUp) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with RequestHeader

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);
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) NetSocket(io.vertx.core.net.NetSocket) Holder(javax.xml.ws.Holder) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 4 with RequestHeader

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"));
}
Also used : SchemaMeta(io.servicecomb.core.definition.SchemaMeta) ReactiveExecutor(io.servicecomb.core.executor.ReactiveExecutor) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 5 with RequestHeader

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);
    }
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ProtobufOutput(io.protostuff.ProtobufOutput) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest)

Aggregations

RequestHeader (io.servicecomb.transport.highway.message.RequestHeader)8 Test (org.junit.Test)5 LinkedBuffer (io.protostuff.LinkedBuffer)3 Buffer (io.vertx.core.buffer.Buffer)3 MockUp (mockit.MockUp)3 LoginRequest (io.servicecomb.transport.highway.message.LoginRequest)2 NetSocket (io.vertx.core.net.NetSocket)2 Holder (javax.xml.ws.Holder)2 ByteBuf (io.netty.buffer.ByteBuf)1 Input (io.protostuff.Input)1 ProtobufOutput (io.protostuff.ProtobufOutput)1 Endpoint (io.servicecomb.core.Endpoint)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)1 ReactiveExecutor (io.servicecomb.core.executor.ReactiveExecutor)1 TcpOutputStream (io.servicecomb.foundation.vertx.tcp.TcpOutputStream)1 SocketAddressImpl (io.vertx.core.net.impl.SocketAddressImpl)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Expectations (mockit.Expectations)1