use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent3.
/**
* Tests the fault injection handler functionality with configuration change event for schema level config.
*/
@Test
public void testFaultInjectHandlerConfigChangeEvent3() throws Exception {
System.setProperty("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay", "1");
System.setProperty("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.delay.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.abort.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.abort.httpStatus", "421");
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName9");
Mockito.when(invocation.getTransport()).thenReturn(transport);
Mockito.when(transport.getName()).thenReturn("rest");
Mockito.when(invocation.getOperationName()).thenReturn("sayBye3");
Mockito.when(invocation.getSchemaId()).thenReturn("testSchema3");
Mockito.when(invocation.getMicroserviceName()).thenReturn("carts3");
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.carts3.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay", 500);
Holder<Boolean> isAsserted = new Holder<>(false);
handler.handle(invocation, ar -> {
// check whether error code return, defaut is 421.
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.carts3.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay");
System.getProperties().remove("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.delay.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.abort.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts3.schemas.testSchema3.policy.fault.protocols.rest.abort.httpStatus");
AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName9");
assertEquals(3, count.get());
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent2.
/**
* Tests the fault injection handler functionality with configuration change event for operation level config.
*/
@Test
public void testFaultInjectHandlerConfigChangeEvent2() throws Exception {
System.setProperty("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.delay.fixedDelay", "1");
System.setProperty("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.delay.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.abort.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.abort.httpStatus", "420");
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName8");
Mockito.when(invocation.getTransport()).thenReturn(transport);
Mockito.when(transport.getName()).thenReturn("rest");
Mockito.when(invocation.getOperationName()).thenReturn("sayBye2");
Mockito.when(invocation.getSchemaId()).thenReturn("testSchema2");
Mockito.when(invocation.getMicroserviceName()).thenReturn("carts2");
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.carts2.schemas.testSchema2.operations.sayBye2.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(420, 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.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.delay.fixedDelay");
System.getProperties().remove("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.delay.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.abort.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts2.schemas.testSchema2.operations.sayBye2.policy.fault.protocols.rest.abort.httpStatus");
AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName8");
assertEquals(3, count.get());
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent5.
/**
* Tests the fault injection handler functionality with configuration change event for service level config.
*/
@Test
public void testFaultInjectHandlerConfigChangeEvent5() throws Exception {
System.setProperty("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.delay.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.delay.fixedDelay", "10");
System.setProperty("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.abort.percent", "100");
System.setProperty("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.abort.httpStatus", "500");
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName11");
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("carts5");
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.carts5.policy.fault.protocols.rest.abort.httpStatus", "420");
Holder<Boolean> isAsserted = new Holder<>(false);
handler.handle(invocation, ar -> {
isAsserted.value = true;
assertTrue(response.isFailed());
assertEquals(500, response.getStatusCode());
assertEquals(420, ar.getStatusCode());
});
Assert.assertTrue(isAsserted.value);
System.getProperties().remove("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.delay.fixedDelay");
System.getProperties().remove("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.delay.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.abort.percent");
System.getProperties().remove("servicecomb.governance.Consumer.carts5.policy.fault.protocols.rest.abort.httpStatus");
AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName11");
assertEquals(3, count.get());
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
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());
}
use of org.apache.servicecomb.foundation.common.Holder in project incubator-servicecomb-java-chassis by apache.
the class TestInspectorBootListener method enabled.
@Test
public void enabled() {
Holder<Object> holder = new Holder<>();
SCBEngine scbEngine = SCBBootstrap.createSCBEngineForTest();
scbEngine.setProducerMicroserviceMeta(new MicroserviceMeta(scbEngine, "ms", false));
scbEngine.setProducerProviderManager(new ProducerProviderManager(scbEngine) {
@Override
public SchemaMeta registerSchema(String schemaId, Object instance) {
if ("inspector".equals(schemaId)) {
holder.value = instance;
}
return null;
}
});
InspectorConfig inspectorConfig = new InspectorConfig().setEnabled(true);
new InspectorBootListener(inspectorConfig, null).onAfterTransport(new BootEvent(scbEngine, EventType.AFTER_TRANSPORT));
Assert.assertNotNull(holder.value);
}
Aggregations