Search in sources :

Example 96 with Holder

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

the class TestInvocation method onFinish.

@Test
public void onFinish() {
    mockNonaTime();
    Holder<InvocationFinishEvent> result = new Holder<>();
    Object subscriber = new Object() {

        @Subscribe
        public void onStart(InvocationFinishEvent event) {
            result.value = event;
        }
    };
    EventManager.register(subscriber);
    Invocation invocation = new Invocation(endpoint, operationMeta, arguments);
    Assert.assertFalse(invocation.isFinished());
    Response response = Response.succResp(null);
    invocation.onFinish(response);
    Assert.assertEquals(nanoTime, result.value.getNanoCurrent());
    Assert.assertSame(invocation, result.value.getInvocation());
    Assert.assertSame(response, result.value.getResponse());
    Assert.assertTrue(invocation.isFinished());
    // should not post event again
    InvocationFinishEvent oldEvent = result.value;
    invocation.onFinish(null);
    Assert.assertSame(oldEvent, result.value);
    EventManager.unregister(subscriber);
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Example 97 with Holder

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

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 98 with Holder

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

the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent4.

/**
 * Tests the fault injection handler functionality with configuration change event for service level config.
 */
@Test
public void testFaultInjectHandlerConfigChangeEvent4() throws Exception {
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.fixedDelay", "1");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.percent", "100");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.percent", "100");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.httpStatus", "421");
    Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName10");
    Mockito.when(invocation.getTransport()).thenReturn(transport);
    Mockito.when(transport.getName()).thenReturn("rest");
    Mockito.when(invocation.getOperationName()).thenReturn("sayBye4");
    Mockito.when(invocation.getSchemaId()).thenReturn("testSchema4");
    Mockito.when(invocation.getMicroserviceName()).thenReturn("carts4");
    boolean validAssert;
    long timeOld = System.currentTimeMillis();
    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.carts4.policy.fault.protocols.rest.delay.fixedDelay", 500);
    Holder<Boolean> isAsserted = new Holder<>(false);
    handler.handle(invocation, ar -> {
        // check whether error code return,
        isAsserted.value = true;
        assertEquals(421, response.getStatusCode());
        assertTrue(response.isFailed());
        long timeNow = System.currentTimeMillis();
        // if really time delay is added it should be greater than 5s.
        Assert.assertTrue((timeNow - timeOld) >= 500);
    });
    Assert.assertTrue(isAsserted.value);
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.fixedDelay");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.percent");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.percent");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.httpStatus");
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName10");
    assertEquals(3, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Example 99 with Holder

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

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 100 with Holder

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

the class AbortFaultTest method injectFaultNoPercentageConfig.

@Test
public void injectFaultNoPercentageConfig() {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", null);
    assertNull(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)

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