liubao68 commented on a change in pull request #615: [SCB-324] Fault-Injection handler implementation URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/615#discussion_r176077192
########## File path: handlers/handler-fault-injection/src/test/java/org/apache/servicecomb/faultinjection/TestFaultInjectHandler.java ########## @@ -0,0 +1,705 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.faultinjection; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.Transport; +import org.apache.servicecomb.core.definition.OperationMeta; +import org.apache.servicecomb.foundation.vertx.VertxUtils; +import org.apache.servicecomb.swagger.invocation.AsyncResponse; +import org.apache.servicecomb.swagger.invocation.Response; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import io.vertx.core.Vertx; + +/** + * Tests the fault injection handler functionality. + */ +public class TestFaultInjectHandler { + FaultInjectionHandler handler; + + Invocation invocation; + + AsyncResponse asyncResp; + + OperationMeta operationMeta; + + private Transport transport; + + Response response; + + + @InjectMocks + FaultInjectionHandler faultHandler; + + @InjectMocks + AbortFault abortFault; + + @InjectMocks + DelayFault delayFault; + + AsyncResponse ar = new AsyncResponse() { + @Override + public void handle(Response resp) { + response = resp; + } + }; + + + @Before + public void setUp() throws Exception { + handler = new FaultInjectionHandler(); + + invocation = Mockito.mock(Invocation.class); + + asyncResp = Mockito.mock(AsyncResponse.class); + + operationMeta = Mockito.mock(OperationMeta.class); + + transport = Mockito.mock(Transport.class); + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + handler = null; + + invocation = null; + + asyncResp = null; + + operationMeta = null; + + transport = null; + } + + /** + * Tests the fault injection handler functionality with default values for + * highway transport. + * + * @throws Exception + */ + @Test + public void testFaultInjectHandlerHighwayWithDefaultCfg() throws Exception { + + Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName1"); + Mockito.when(invocation.getTransport()).thenReturn(transport); + Mockito.when(transport.getName()).thenReturn("highway"); + Mockito.when(invocation.getOperationName()).thenReturn("sayHello"); + Mockito.when(invocation.getSchemaId()).thenReturn("sayHelloSchema"); + Mockito.when(invocation.getMicroserviceName()).thenReturn("hello"); + + + List<Fault> faultInjectionFeatureList = Arrays.asList(abortFault, delayFault); + handler.setFaultFeature(faultInjectionFeatureList); + + handler.handle(invocation, asyncResp); + + AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("highwayMicroserviceQualifiedName1"); + assertEquals(2, count.get()); + } + + /** + * Tests the fault injection handler functionality with default values for rest + * transport. + * + * @throws Exception + */ + @Test + public void testFaultInjectHandlerRestWithDefaultCfg() throws Exception { + + Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName2"); + Mockito.when(invocation.getTransport()).thenReturn(transport); + Mockito.when(transport.getName()).thenReturn("rest"); + Mockito.when(invocation.getOperationName()).thenReturn("sayHello"); + Mockito.when(invocation.getSchemaId()).thenReturn("sayHelloSchema"); + Mockito.when(invocation.getMicroserviceName()).thenReturn("hello"); + + List<Fault> faultInjectionFeatureList = Arrays.asList(delayFault, abortFault); + handler.setFaultFeature(faultInjectionFeatureList); + + handler.handle(invocation, asyncResp); + + AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName2"); + assertEquals(2, count.get()); + } + + /** + * Tests the fault injection handler functionality with global configuration + * with delay/abort condition. + * + * @throws Exception + */ + @Test + public void testFaultInjectHandlerConfigChangeGlobal() throws Exception { + + System.setProperty("cse.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay", "5"); + System.setProperty("cse.governance.Consumer._global.policy.fault.protocols.rest.delay.percent", "10"); + System.setProperty("cse.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", "10"); + System.setProperty("cse.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus", "421"); + + Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName3"); + Mockito.when(invocation.getTransport()).thenReturn(transport); + Mockito.when(transport.getName()).thenReturn("rest"); + Mockito.when(invocation.getOperationName()).thenReturn("sayHello"); + Mockito.when(invocation.getSchemaId()).thenReturn("sayHelloSchema"); + Mockito.when(invocation.getMicroserviceName()).thenReturn("hello"); + + List<Fault> faultInjectionFeatureList = Arrays.asList(abortFault, delayFault); + handler.setFaultFeature(faultInjectionFeatureList); + + boolean validAssert; + try { + validAssert = true; + handler.handle(invocation, asyncResp); + } catch (Exception e) { + validAssert = false; Review comment: This test case do not delay. Can you add more invocations and assert delay or abort is actually happend? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
