Search in sources :

Example 1 with ReactiveExecutor

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

the class TestExecutors method testFixedThreadExecutor.

@Test
public void testFixedThreadExecutor() {
    FixedThreadExecutor oFixedThreadExecutor = new FixedThreadExecutor();
    oFixedThreadExecutor.execute(new Runnable() {

        @Override
        public void run() {
        }
    });
    Map<Long, Executor> threadExecutorMap = Deencapsulation.getField(oFixedThreadExecutor, "threadExecutorMap");
    Assert.assertEquals(true, (threadExecutorMap.size() > 0));
    List<Executor> executorList = Deencapsulation.getField(oFixedThreadExecutor, "executorList");
    Assert.assertEquals(true, (executorList.size() > 1));
    ReactiveExecutor oReactiveExecutor = new ReactiveExecutor();
    oReactiveExecutor.execute(new Runnable() {

        @Override
        public void run() {
            strThreadTest = "thread Ran";
        }
    });
    Assert.assertEquals("thread Ran", strThreadTest);
}
Also used : FixedThreadExecutor(org.apache.servicecomb.core.executor.FixedThreadExecutor) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) FixedThreadExecutor(org.apache.servicecomb.core.executor.FixedThreadExecutor) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Test(org.junit.Test)

Example 2 with ReactiveExecutor

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

the class TestAbstractRestInvocation method scheduleInvocationException.

@Test
public void scheduleInvocationException(@Mocked OperationMeta operationMeta) {
    Executor executor = new ReactiveExecutor();
    requestEx = new AbstractHttpServletRequest() {
    };
    requestEx.setAttribute(RestConst.REST_REQUEST, requestEx);
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
        }
    };
    Holder<Throwable> result = new Holder<>();
    Error error = new Error("run on executor");
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        protected void runOnExecutor() {
            throw error;
        }

        @Override
        public void sendFailResponse(Throwable throwable) {
            result.value = throwable;
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    Assert.assertSame(error, result.value);
}
Also used : Expectations(mockit.Expectations) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Test(org.junit.Test)

Example 3 with ReactiveExecutor

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

the class TestHighwayClient method doTestSend.

private Object doTestSend(Vertx vertx, HighwayClientConnectionPool pool, HighwayClientConnection tcpClient, Object decodedResponse) throws Exception {
    new MockUp<VertxUtils>() {

        @Mock
        <VERTICLE extends AbstractVerticle> boolean blockDeploy(Vertx vertx, Class<VERTICLE> cls, DeploymentOptions options) throws InterruptedException {
            return true;
        }
    };
    new MockUp<ClientPoolManager<HighwayClientConnectionPool>>() {

        @Mock
        public HighwayClientConnectionPool findClientPool(boolean sync) {
            return pool;
        }
    };
    new MockUp<ProtobufManager>() {

        @Mock
        public OperationProtobuf getOrCreateOperation(OperationMeta operationMeta) throws Exception {
            return operationProtobuf;
        }
    };
    new MockUp<HighwayClientConnectionPool>() {

        @Mock
        HighwayClientConnection findOrCreateClient(String endpoint) {
            return tcpClient;
        }
    };
    new MockUp<HighwayCodec>() {

        @Mock
        public Buffer encodeRequest(Invocation invocation, OperationProtobuf operationProtobuf, long msgId) throws Exception {
            return null;
        }

        @Mock
        Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData, ProtobufFeature protobufFeature) throws Throwable {
            if (Response.class.isInstance(decodedResponse)) {
                return (Response) decodedResponse;
            }
            throw (Throwable) decodedResponse;
        }
    };
    client.init(vertx);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
    Mockito.when(invocation.getEndpoint().getEndpoint()).thenReturn("endpoint");
    Mockito.when(invocation.getResponseExecutor()).thenReturn(new ReactiveExecutor());
    Holder<Object> result = new Holder<>();
    client.send(invocation, ar -> {
        result.value = ar.getResult();
    });
    return result.value;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) MockUp(mockit.MockUp) Vertx(io.vertx.core.Vertx) AbstractVerticle(io.vertx.core.AbstractVerticle) TcpData(org.apache.servicecomb.foundation.vertx.client.tcp.TcpData) ProtobufFeature(io.protostuff.runtime.ProtobufFeature) Response(org.apache.servicecomb.swagger.invocation.Response) DeploymentOptions(io.vertx.core.DeploymentOptions) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) BeforeClass(org.junit.BeforeClass) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Example 4 with ReactiveExecutor

use of org.apache.servicecomb.core.executor.ReactiveExecutor 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 5 with ReactiveExecutor

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

the class TestProducerSchemaFactory method init.

@BeforeClass
public static void init() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    ConverterMgr converterMgr = new ConverterMgr();
    ProducerArgumentsMapperFactory producerArgsMapperFactory = new ProducerArgumentsMapperFactory();
    producerArgsMapperFactory.setConverterMgr(converterMgr);
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    SchemaLoader schemaLoader = new SchemaLoader() {

        @Override
        public void putSelfBasePathIfAbsent(String microserviceName, String basePath) {
        }
    };
    CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
    producerSchemaFactory.setSwaggerEnv(swaggerEnv);
    ReflectUtils.setField(producerSchemaFactory, "microserviceMetaManager", microserviceMetaManager);
    ReflectUtils.setField(producerSchemaFactory, "schemaLoader", schemaLoader);
    ReflectUtils.setField(producerSchemaFactory, "compositeSwaggerGeneratorContext", compositeSwaggerGeneratorContext);
    Executor reactiveExecutor = new ReactiveExecutor();
    Executor normalExecutor = (cmd) -> {
    };
    new MockUp<BeanUtils>() {

        @SuppressWarnings("unchecked")
        @Mock
        <T> T getBean(String name) {
            if (ExecutorManager.EXECUTOR_REACTIVE.equals(name)) {
                return (T) reactiveExecutor;
            }
            return (T) normalExecutor;
        }
    };
    // ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
    // BeanUtils.setContext(applicationContext);
    // Mockito.when(applicationContext.getBean(ExecutorManager.EXECUTOR_REACTIVE)).thenReturn(new ReactiveExecutor());
    UnitTestMeta.init();
    schemaMeta = producerSchemaFactory.getOrCreateProducerSchema("app:ms", "schema", TestProducerSchemaFactoryImpl.class, new TestProducerSchemaFactoryImpl());
}
Also used : BeanUtils(org.apache.servicecomb.foundation.common.utils.BeanUtils) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) ExecutorManager(org.apache.servicecomb.core.executor.ExecutorManager) BeforeClass(org.junit.BeforeClass) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) BootstrapNormal(org.apache.servicecomb.swagger.engine.bootstrap.BootstrapNormal) SchemaLoader(org.apache.servicecomb.core.definition.loader.SchemaLoader) CompletableFuture(java.util.concurrent.CompletableFuture) ProducerArgumentsMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperFactory) Mock(mockit.Mock) Response(org.apache.servicecomb.swagger.invocation.Response) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) AfterClass(org.junit.AfterClass) MockUp(mockit.MockUp) Executor(java.util.concurrent.Executor) Const(org.apache.servicecomb.core.Const) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) ReflectUtils(org.apache.servicecomb.foundation.common.utils.ReflectUtils) MicroserviceMetaManager(org.apache.servicecomb.core.definition.MicroserviceMetaManager) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) Invocation(org.apache.servicecomb.core.Invocation) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation) ConverterMgr(org.apache.servicecomb.swagger.invocation.converter.ConverterMgr) ServiceRegistryFactory(org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) Holder(javax.xml.ws.Holder) Endpoint(org.apache.servicecomb.core.Endpoint) UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) Assert(org.junit.Assert) MicroserviceMetaManager(org.apache.servicecomb.core.definition.MicroserviceMetaManager) SchemaLoader(org.apache.servicecomb.core.definition.loader.SchemaLoader) ProducerArgumentsMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperFactory) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) MockUp(mockit.MockUp) ConverterMgr(org.apache.servicecomb.swagger.invocation.converter.ConverterMgr) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) BeforeClass(org.junit.BeforeClass)

Aggregations

ReactiveExecutor (org.apache.servicecomb.core.executor.ReactiveExecutor)6 Holder (javax.xml.ws.Holder)5 Test (org.junit.Test)5 Executor (java.util.concurrent.Executor)4 MockUp (mockit.MockUp)3 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)3 Expectations (mockit.Expectations)2 Invocation (org.apache.servicecomb.core.Invocation)2 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)2 InvocationStartEvent (org.apache.servicecomb.core.event.InvocationStartEvent)2 AbstractHttpServletRequest (org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 BeforeClass (org.junit.BeforeClass)2 ProtobufFeature (io.protostuff.runtime.ProtobufFeature)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Vertx (io.vertx.core.Vertx)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Mock (mockit.Mock)1 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)1