Search in sources :

Example 36 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestCseClientHttpRequest method testNormal.

@Test
public void testNormal() throws IOException {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    UnitTestMeta meta = new UnitTestMeta();
    CseContext.getInstance().getSchemaListenerManager().setSchemaListenerList(Arrays.asList(new RestEngineSchemaListener()));
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(SpringmvcImpl.class);
    CseContext.getInstance().getSchemaListenerManager().notifySchemaListener(schemaMeta);
    Holder<Invocation> holder = new Holder<>();
    CseClientHttpRequest client = new CseClientHttpRequest(URI.create("cse://app:test/" + SpringmvcImpl.class.getSimpleName() + "/bytes"), HttpMethod.POST) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Response doInvoke(Invocation invocation) {
            holder.value = invocation;
            return Response.ok("result");
        }
    };
    byte[] body = "abc".getBytes();
    client.setRequestBody(body);
    client.execute();
    Assert.assertArrayEquals(body, holder.value.getSwaggerArgument(0));
}
Also used : RestEngineSchemaListener(org.apache.servicecomb.common.rest.RestEngineSchemaListener) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) Invocation(org.apache.servicecomb.core.Invocation) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) Holder(javax.xml.ws.Holder) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Test(org.junit.Test)

Example 37 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class ProducerHttpRequestArgMapper method createContextArg.

@Override
public Object createContextArg(SwaggerInvocation swaggerInvocation) {
    Invocation invocation = (Invocation) swaggerInvocation;
    // 从rest transport来
    HttpServletRequest request = (HttpServletRequest) invocation.getHandlerContext().get(RestConst.REST_REQUEST);
    if (request != null) {
        return request;
    }
    // 通过args模拟request
    return new InvocationToHttpServletRequest(invocation);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation)

Example 38 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.

the class TestInvoker method completableFutureInvoke_failed.

@Test
public void completableFutureInvoke_failed(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Throwable error = new Error("failed");
    Response response = Response.createConsumerFail(error);
    new MockUp<InvokerUtils>() {

        @Mock
        void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
            asyncResp.handle(response);
            ;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
    future.whenComplete((result, ex) -> {
        Assert.assertEquals(null, result);
        Assert.assertSame(error, ex);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 39 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-skywalking by apache.

the class ProducerOperationHandlerInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Invocation invocation = (Invocation) allArguments[0];
    AbstractSpan span = ContextManager.activeSpan();
    int statusCode = invocation.getStatus().getStatusCode();
    if (statusCode >= 400) {
        span.errorOccurred();
        Tags.STATUS_CODE.set(span, Integer.toString(statusCode));
    }
    ContextManager.stopSpan();
    return ret;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 40 with Invocation

use of org.apache.servicecomb.core.Invocation in project incubator-skywalking by apache.

the class ProducerOperationHandlerInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Invocation invocation = (Invocation) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(invocation.getContext().get(next.getHeadKey()));
    }
    String operationName = invocation.getMicroserviceQualifiedName();
    AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier);
    String url = invocation.getOperationMeta().getOperationPath();
    Tags.URL.set(span, url);
    span.setComponent(ComponentsDefine.SERVICECOMB);
    SpanLayer.asRPCFramework(span);
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Invocation(org.apache.servicecomb.core.Invocation) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

Invocation (org.apache.servicecomb.core.Invocation)66 Test (org.junit.Test)50 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)23 Response (org.apache.servicecomb.swagger.invocation.Response)19 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)16 MockUp (mockit.MockUp)11 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)9 ArrayList (java.util.ArrayList)9 Server (com.netflix.loadbalancer.Server)8 HashMap (java.util.HashMap)8 Holder (javax.xml.ws.Holder)8 Expectations (mockit.Expectations)7 Mock (mockit.Mock)7 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)5 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)5 Map (java.util.Map)4 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4 Endpoint (org.apache.servicecomb.core.Endpoint)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4