Search in sources :

Example 26 with Holder

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

the class DelayFaultTest method injectFaultSystemDelay.

@Test
public void injectFaultSystemDelay() throws InterruptedException {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay", "10");
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent", "100");
    assertEquals("10", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay").getString());
    assertEquals("100", DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent").getString());
    DelayFault delayFault = new DelayFault();
    FaultParam faultParam = new FaultParam(1);
    Holder<String> resultHolder = new Holder<>();
    CountDownLatch latch = new CountDownLatch(1);
    delayFault.injectFault(invocation, faultParam, response -> {
        resultHolder.value = response.getResult();
        latch.countDown();
    });
    latch.await(10, TimeUnit.SECONDS);
    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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 27 with Holder

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

the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent1.

/**
 * Tests the fault injection handler functionality with configuration change event for global level config.
 */
@Test
public void testFaultInjectHandlerConfigChangeEvent1() throws Exception {
    System.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay", "1");
    System.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent", "100");
    System.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", "100");
    System.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus", "420");
    Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName7");
    Mockito.when(invocation.getTransport()).thenReturn(transport);
    Mockito.when(transport.getName()).thenReturn("rest");
    Mockito.when(invocation.getOperationName()).thenReturn("sayBye1");
    Mockito.when(invocation.getSchemaId()).thenReturn("testSchema1");
    Mockito.when(invocation.getMicroserviceName()).thenReturn("carts1");
    boolean validAssert;
    List<Fault> faultInjectionFeatureList = Arrays.asList(delayFault, abortFault);
    handler.setFaultFeature(faultInjectionFeatureList);
    try {
        validAssert = true;
        handler.handle(invocation, ar);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay", 500);
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus", 421);
    Holder<Boolean> isAsserted = new Holder<>(false);
    handler.handle(invocation, ar -> {
        isAsserted.value = true;
        assertTrue(response.isFailed());
    });
    Assert.assertTrue(isAsserted.value);
    System.getProperties().remove("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay");
    System.getProperties().remove("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent");
    System.getProperties().remove("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent");
    System.getProperties().remove("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus");
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName7");
    assertEquals(3, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Example 28 with Holder

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

the class TestCseClientHttpRequest method testNormal.

@Test
public void testNormal() {
    Holder<Invocation> holder = new Holder<>();
    CseClientHttpRequest client = new CseClientHttpRequest(URI.create("cse://defaultMicroservice/" + SpringmvcImpl.class.getSimpleName() + "/bytes"), HttpMethod.POST) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Response doInvoke(Invocation invocation) {
            holder.value = invocation;
            return Response.ok("result");
        }
    };
    byte[] body = "abc".getBytes();
    HttpHeaders headers = new HttpHeaders();
    headers.add("token", "123");
    client.setRequestBody(body);
    client.setHttpHeaders(headers);
    client.execute();
    Assert.assertArrayEquals(body, (byte[]) holder.value.getInvocationArguments().get("input"));
    Assert.assertEquals("123", holder.value.getInvocationArguments().get("token"));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Invocation(org.apache.servicecomb.core.Invocation) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Example 29 with Holder

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

the class TestInstanceCacheCheckerMock method createFindServiceInstancesResult.

private Holder<MicroserviceInstances> createFindServiceInstancesResult() {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    microserviceInstances.setNeedRefresh(true);
    microserviceInstances.setRevision("first");
    FindInstancesResponse findInstancesResponse = new FindInstancesResponse();
    findInstancesResponse.setInstances(new ArrayList<>());
    microserviceInstances.setInstancesResponse(findInstancesResponse);
    Holder<MicroserviceInstances> findHolder = new Holder<>();
    findHolder.value = microserviceInstances;
    return findHolder;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) Holder(org.apache.servicecomb.foundation.common.Holder) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse)

Example 30 with Holder

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

the class HighwayServerInvoke method checkQpsFlowControl.

private Holder<Boolean> checkQpsFlowControl(OperationMeta operationMeta) {
    Holder<Boolean> qpsFlowControlReject = new Holder<>(false);
    @SuppressWarnings("deprecation") Handler providerQpsFlowControlHandler = operationMeta.getProviderQpsFlowControlHandler();
    if (null != providerQpsFlowControlHandler) {
        try {
            providerQpsFlowControlHandler.handle(invocation, response -> {
                qpsFlowControlReject.value = true;
                sendResponse(header.getContext(), response);
            });
        } catch (Exception e) {
            LOGGER.error("failed to execute ProviderQpsFlowControlHandler", e);
            qpsFlowControlReject.value = true;
            sendResponse(header.getContext(), Response.providerFailResp(e));
        }
    }
    return qpsFlowControlReject;
}
Also used : Holder(org.apache.servicecomb.foundation.common.Holder) Handler(org.apache.servicecomb.core.Handler) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

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