Search in sources :

Example 6 with RequestHeader

use of org.apache.servicecomb.transport.highway.message.RequestHeader in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayServerConnection method testRequestNormal.

@Test
public void testRequestNormal(@Mocked MicroserviceMeta microserviceMeta, @Mocked OperationMeta operationMeta, @Mocked SchemaMeta schemaMeta) throws Exception {
    header.setMsgType(MsgType.REQUEST);
    Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
    Buffer bodyBuffer = Buffer.buffer();
    new Expectations(CseContext.getInstance()) {

        {
            CseContext.getInstance().getMicroserviceMetaManager();
            result = microserviceMetaManager;
            microserviceMetaManager.ensureFindValue(null);
            result = microserviceMeta;
            microserviceMeta.ensureFindSchemaMeta(header.getSchemaId());
            result = schemaMeta;
        }
    };
    new Expectations(ProtobufManager.class) {

        {
            ProtobufManager.getOrCreateOperation(operationMeta);
            result = null;
        }
    };
    Holder<Boolean> holder = new Holder<>();
    new MockUp<HighwayServerInvoke>() {

        @Mock
        public boolean init(NetSocket netSocket, long msgId, RequestHeader header, Buffer bodyBuffer) {
            return true;
        }

        @Mock
        public void execute() {
            holder.value = true;
        }
    };
    connection.handle(0, headerBuffer, bodyBuffer);
    Assert.assertEquals(null, connection.getProtocol());
    Assert.assertEquals(null, connection.getZipName());
    Assert.assertEquals(true, holder.value);
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) Expectations(mockit.Expectations) NetSocket(io.vertx.core.net.NetSocket) Holder(javax.xml.ws.Holder) RequestHeader(org.apache.servicecomb.transport.highway.message.RequestHeader) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 7 with RequestHeader

use of org.apache.servicecomb.transport.highway.message.RequestHeader in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayServerInvoke method test.

@Test
public void test() {
    Holder<InvocationStartEvent> startHolder = new Holder<>();
    Holder<InvocationFinishEvent> finishHolder = new Holder<>();
    Object subscriber = new Object() {

        @Subscribe
        public void onStart(InvocationStartEvent event) {
            startHolder.value = event;
        }

        @Subscribe
        public void onFinish(InvocationFinishEvent event) {
            finishHolder.value = event;
        }
    };
    EventManager.register(subscriber);
    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(connection, 0, null, null));
    // 初始化成功
    requestHeader.setDestMicroservice(schemaMeta.getMicroserviceName());
    requestHeader.setSchemaId(schemaMeta.getSchemaId());
    requestHeader.setOperationName(operationMeta.getOperationId());
    Assert.assertTrue(highwayServerInvoke.init(connection, 0, requestHeader, null));
    // exe失败
    MockUtil.getInstance().decodeRequestSucc = false;
    highwayServerInvoke.execute();
    EventManager.unregister(subscriber);
    Assert.assertEquals(true, Buffer.buffer(netSocketBuffer).toString().startsWith("CSE.TCP"));
    Assert.assertSame(highwayServerInvoke.invocation, startHolder.value.getInvocation());
    Assert.assertSame(highwayServerInvoke.invocation, finishHolder.value.getInvocation());
    Assert.assertTrue(highwayServerInvoke.invocation.getStartExecutionTime() != 0);
}
Also used : InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Holder(javax.xml.ws.Holder) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) RequestHeader(org.apache.servicecomb.transport.highway.message.RequestHeader) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Test(org.junit.Test)

Example 8 with RequestHeader

use of org.apache.servicecomb.transport.highway.message.RequestHeader in project incubator-servicecomb-java-chassis by apache.

the class HighwayCodec method encodeRequest.

public static TcpOutputStream encodeRequest(long msgId, Invocation invocation, OperationProtobuf operationProtobuf, ProtobufFeature protobufFeature) throws Exception {
    // 写header
    RequestHeader header = new RequestHeader();
    header.setMsgType(MsgType.REQUEST);
    header.setFlags(0);
    header.setDestMicroservice(invocation.getMicroserviceName());
    header.setSchemaId(invocation.getSchemaId());
    header.setOperationName(invocation.getOperationName());
    header.setContext(invocation.getContext());
    HighwayOutputStream os = new HighwayOutputStream(msgId, protobufFeature);
    os.write(header, operationProtobuf.getRequestSchema(), invocation.getArgs());
    return os;
}
Also used : RequestHeader(org.apache.servicecomb.transport.highway.message.RequestHeader)

Aggregations

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