Search in sources :

Example 11 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method threadPoolReject.

@Test
public void threadPoolReject(@Mocked OperationMeta operationMeta) {
    RejectedExecutionException rejectedExecutionException = new RejectedExecutionException("reject");
    Executor executor = (task) -> {
        throw rejectedExecutionException;
    };
    new Expectations() {

        {
            restOperation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExecutor();
            result = executor;
        }
    };
    Holder<Throwable> holder = new Holder<>();
    requestEx = new AbstractHttpServletRequestForTest();
    restInvocation = new AbstractRestInvocationForTest() {

        @Override
        public void sendFailResponse(Throwable throwable) {
            holder.value = throwable;
            invocation.onFinish(Response.ok(null));
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.restOperationMeta = restOperation;
    restInvocation.scheduleInvocation();
    Assert.assertSame(rejectedExecutionException, holder.value);
}
Also used : CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Arrays(java.util.Arrays) SCBEngine(org.apache.servicecomb.core.SCBEngine) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) MultiMap(io.vertx.core.MultiMap) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) RestMetaUtils(org.apache.servicecomb.common.rest.definition.RestMetaUtils) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) After(org.junit.After) Mock(mockit.Mock) ProduceProcessorManager(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) MockUp(mockit.MockUp) Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Buffer(io.vertx.core.buffer.Buffer) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager) Mocked(mockit.Mocked) CONTENT_LENGTH(com.google.common.net.HttpHeaders.CONTENT_LENGTH) RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) InvocationStartEvent(org.apache.servicecomb.core.event.InvocationStartEvent) Expectations(mockit.Expectations) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) TRANSFER_ENCODING(com.google.common.net.HttpHeaders.TRANSFER_ENCODING) HashMap(java.util.HashMap) JsonUtils(org.apache.servicecomb.foundation.common.utils.JsonUtils) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AsyncContext(javax.servlet.AsyncContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Subscribe(com.google.common.eventbus.Subscribe) Status(javax.ws.rs.core.Response.Status) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) Executor(java.util.concurrent.Executor) Handler(org.apache.servicecomb.core.Handler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Const(org.apache.servicecomb.core.Const) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) Rule(org.junit.Rule) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Assert(org.junit.Assert) TestPathSchema(org.apache.servicecomb.common.rest.locator.TestPathSchema) Assert.assertEquals(org.junit.Assert.assertEquals) Expectations(mockit.Expectations) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) Executor(java.util.concurrent.Executor) Holder(org.apache.servicecomb.foundation.common.Holder) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 12 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method scheduleInvocation_invocationContextDeserializeError.

@Test
public void scheduleInvocation_invocationContextDeserializeError(@Mocked AsyncContext asyncContext) {
    requestEx = new AbstractHttpServletRequest() {

        @Override
        public String getHeader(String name) {
            return "{\"x-cse-src-microservice\":'source\"}";
        }

        @Override
        public AsyncContext getAsyncContext() {
            return asyncContext;
        }
    };
    Holder<Integer> status = new Holder<>();
    Holder<String> reasonPhrase = new Holder<>();
    Holder<Integer> endCount = new Holder<>(0);
    responseEx = new AbstractHttpServletResponse() {

        @SuppressWarnings("deprecation")
        @Override
        public void setStatus(int sc, String sm) {
            status.value = sc;
            reasonPhrase.value = sm;
        }

        @Override
        public void flushBuffer() {
            endCount.value = endCount.value + 1;
        }

        @Override
        public void setContentType(String type) {
            assertEquals("application/json; charset=utf-8", type);
        }
    };
    restInvocation.requestEx = requestEx;
    restInvocation.responseEx = responseEx;
    restInvocation.scheduleInvocation();
    assertEquals(Integer.valueOf(590), status.value);
    assertEquals("Unexpected producer error, please check logs for details", reasonPhrase.value);
    assertEquals(Integer.valueOf(1), endCount.value);
}
Also used : AbstractHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest) Holder(org.apache.servicecomb.foundation.common.Holder) AsyncContext(javax.servlet.AsyncContext) Endpoint(org.apache.servicecomb.core.Endpoint) AbstractHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletResponse) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 13 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class VertxUtils method blockDeploy.

// deploy Verticle and wait for its success. do not call this method in event-loop thread
public static <VERTICLE extends Verticle> boolean blockDeploy(Vertx vertx, Class<VERTICLE> cls, DeploymentOptions options) throws InterruptedException {
    Holder<Boolean> result = new Holder<>();
    CountDownLatch latch = new CountDownLatch(1);
    vertx.deployVerticle(cls.getName(), options, ar -> {
        result.value = ar.succeeded();
        if (ar.failed()) {
            LOGGER.error("deploy vertx failed, cause ", ar.cause());
        }
        latch.countDown();
    });
    latch.await();
    return result.value;
}
Also used : Holder(org.apache.servicecomb.foundation.common.Holder) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class AbortFaultTest method injectFaultNoError.

@Test
public void injectFaultNoError() {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus", "421");
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", "0");
    assertEquals("421", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus").getString());
    assertEquals("0", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent").getString());
    AbortFault abortFault = new AbortFault();
    FaultParam faultParam = new FaultParam(1);
    Vertx vertx = VertxUtils.getOrCreateVertxByName("faultinjectionTest", null);
    faultParam.setVertx(vertx);
    Holder<String> resultHolder = new Holder<>();
    abortFault.injectFault(invocation, faultParam, response -> resultHolder.value = response.getResult());
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName12");
    assertEquals(1, count.get());
    assertEquals("success", resultHolder.value);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Holder(org.apache.servicecomb.foundation.common.Holder) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Example 15 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class AbortFaultTest method injectFaultError.

@Test
public void injectFaultError() {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus", "421");
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", "100");
    assertEquals("421", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus").getString());
    assertEquals("100", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent").getString());
    AbortFault abortFault = new AbortFault();
    FaultParam faultParam = new FaultParam(1);
    Vertx vertx = VertxUtils.getOrCreateVertxByName("faultinjectionTest", null);
    faultParam.setVertx(vertx);
    Holder<InvocationException> resultHolder = new Holder<>();
    abortFault.injectFault(invocation, faultParam, response -> resultHolder.value = response.getResult());
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName12");
    assertEquals(1, count.get());
    assertEquals(421, resultHolder.value.getStatusCode());
    assertEquals("aborted by fault inject", resultHolder.value.getReasonPhrase());
    assertEquals("CommonExceptionData [message=aborted by fault inject]", resultHolder.value.getErrorData().toString());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Holder(org.apache.servicecomb.foundation.common.Holder) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)108 Test (org.junit.Test)88 MockUp (mockit.MockUp)36 AtomicLong (java.util.concurrent.atomic.AtomicLong)28 Invocation (org.apache.servicecomb.core.Invocation)24 Expectations (mockit.Expectations)22 Vertx (io.vertx.core.Vertx)20 List (java.util.List)18 Response (org.apache.servicecomb.swagger.invocation.Response)18 Mock (mockit.Mock)16 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 SCBEngine (org.apache.servicecomb.core.SCBEngine)14 ExpectedException (org.junit.rules.ExpectedException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 Status (javax.ws.rs.core.Response.Status)12 Deencapsulation (mockit.Deencapsulation)12 Mocked (mockit.Mocked)12 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)12