[ 
https://issues.apache.org/jira/browse/SCB-324?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16352108#comment-16352108
 ] 

ASF GitHub Bot commented on SCB-324:
------------------------------------

liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection 
handler implementation
URL: 
https://github.com/apache/incubator-servicecomb-java-chassis/pull/543#discussion_r165899465
 
 

 ##########
 File path: 
handlers/handler-fault-injection/src/test/java/org/apache/servicecomb/faultinjection/TestFaultInjectHandler.java
 ##########
 @@ -0,0 +1,421 @@
+/*
+ * 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.concurrent.atomic.AtomicLong;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.Transport;
+import org.apache.servicecomb.core.definition.OperationMeta;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+
+/**
+ * Tests the fault injection handler functionality.
+ */
+public class TestFaultInjectHandler {
+  FaultInjectionHandler handler;
+
+  Invocation invocation;
+
+  AsyncResponse asyncResp;
+
+  OperationMeta operationMeta;
+
+  private Transport transport;
+
+  @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);
+  }
+
+  @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");
+
+    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");
+
+    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");
+
+    boolean validAssert;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    
System.getProperties().remove("cse.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay");
+    
System.getProperties().remove("cse.governance.Consumer._global.policy.fault.protocols.rest.delay.percent");
+    
System.getProperties().remove("cse.governance.Consumer._global.policy.fault.protocols.rest.abort.percent");
+    
System.getProperties().remove("cse.governance.Consumer._global.policy.fault.protocols.rest.abort.httpStatus");
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName3");
+    assertEquals(2, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with service level 
configuration
+   * with delay/abort condition.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerServiceCfgSuccess() throws Exception {
+
+    
System.setProperty("cse.governance.Consumer.carts.policy.fault.protocols.rest.delay.fixedDelay",
 "1");
+    
System.setProperty("cse.governance.Consumer.carts.policy.fault.protocols.rest.delay.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.carts.policy.fault.protocols.rest.abort.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.carts.policy.fault.protocols.rest.abort.httpStatus",
 "421");
+
+    
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName4");
+    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("carts");
+
+    boolean validAssert;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    
System.getProperties().remove("cse.governance.Consumer.carts.policy.fault.protocols.rest.delay.fixedDelay");
+    
System.getProperties().remove("cse.governance.Consumer.carts.policy.fault.protocols.rest.delay.percent");
+    
System.getProperties().remove("cse.governance.Consumer.carts.policy.fault.protocols.rest.abort.percent");
+    
System.getProperties().remove("cse.governance.Consumer.carts.policy.fault.protocols.rest.abort.httpStatus");
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName4");
+    assertEquals(2, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with schema level 
configuration
+   * with delay/abort condition.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerSchemaCfgSuccess() throws Exception {
+
+    
System.setProperty("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.delay.fixedDelay",
 "1");
+    
System.setProperty("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.delay.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.abort.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.abort.httpStatus",
+        "421");
+
+    
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName5");
+    Mockito.when(invocation.getTransport()).thenReturn(transport);
+    Mockito.when(transport.getName()).thenReturn("rest");
+    Mockito.when(invocation.getOperationName()).thenReturn("sayHello");
+    Mockito.when(invocation.getSchemaId()).thenReturn("testSchema");
+    Mockito.when(invocation.getMicroserviceName()).thenReturn("carts");
+
+    boolean validAssert;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    System.getProperties()
+        
.remove("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.delay.fixedDelay");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.delay.percent");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.abort.percent");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.schemas.testSchema.policy.fault.protocols.rest.abort.httpStatus");
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName5");
+    assertEquals(2, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with operation level 
configuration
+   * with delay/abort condition.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerOperationCfgSuccess() throws Exception {
+
+    
System.setProperty("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.delay.fixedDelay",
 "1");
+    
System.setProperty("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.delay.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.abort.percent",
 "10");
+    
System.setProperty("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.abort.httpStatus",
+        "421");
+
+    
Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName6");
+    Mockito.when(invocation.getTransport()).thenReturn(transport);
+    Mockito.when(transport.getName()).thenReturn("rest");
+    Mockito.when(invocation.getOperationName()).thenReturn("sayHi");
+    Mockito.when(invocation.getSchemaId()).thenReturn("testSchema");
+    Mockito.when(invocation.getMicroserviceName()).thenReturn("carts");
+
+    boolean validAssert;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    System.getProperties()
+        
.remove("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.delay.fixedDelay");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.delay.percent");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.abort.percent");
+    System.getProperties()
+        
.remove("cse.governance.Consumer.operations.sayHi.policy.fault.protocols.rest.abort.httpStatus");
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName6");
+    assertEquals(2, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with configuration change 
event for global level config.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerConfigChangeEvent1() throws Exception {
+
+    
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;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    TestFaultInjectUtil
+        
.updateProperty("cse.governance.Consumer._global.policy.fault.protocols.rest.delay.fixedDelay",
 500);
+
+    handler.handle(invocation, asyncResp);
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName7");
+    assertEquals(3, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with configuration change 
event for operation level config.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerConfigChangeEvent2() throws Exception {
+    
System.setProperty("cse.governance.Consumer.operations.sayBye2.policy.fault.protocols.rest.delay.fixedDelay",
 "1");
+
+    
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;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    TestFaultInjectUtil
+        
.updateProperty("cse.governance.Consumer.operations.sayBye2.policy.fault.protocols.rest.delay.fixedDelay",
 500);
+
+    handler.handle(invocation, asyncResp);
+
+    System.getProperties()
+        
.remove("cse.governance.Consumer.operations.sayBye2.policy.fault.protocols.rest.delay.fixedDelay");
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName8");
+    assertEquals(3, count.get());
+  }
+
+  /**
+   * Tests the fault injection handler functionality with configuration change 
event for schema level config.
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testFaultInjectHandlerConfigChangeEvent3() throws Exception {
+    
System.setProperty("cse.governance.Consumer.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay",
 "1");
+
+    
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;
+    try {
+      validAssert = true;
+      handler.handle(invocation, asyncResp);
+    } catch (Exception e) {
+      validAssert = false;
+    }
+
+    TestFaultInjectUtil
+        
.updateProperty("cse.governance.Consumer.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay",
+            500);
+
+    handler.handle(invocation, asyncResp);
+
+    System.getProperties()
+        
.remove("cse.governance.Consumer.schemas.testSchema3.policy.fault.protocols.rest.delay.fixedDelay");
+
+    Assert.assertTrue(validAssert);
+    AtomicLong count = 
FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName9");
 
 Review comment:
   Can you add some assertions to make sure fault injection is taking effect. 
e.g. error code is right or time delay is actually happended

----------------------------------------------------------------
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:
us...@infra.apache.org


> Chassis must support network failure simulation, so that I can developers can 
> enhance the robustness of the app
> ---------------------------------------------------------------------------------------------------------------
>
>                 Key: SCB-324
>                 URL: https://issues.apache.org/jira/browse/SCB-324
>             Project: Apache ServiceComb
>          Issue Type: Task
>          Components: Java-Chassis
>            Reporter: sukesh
>            Assignee: sukesh
>            Priority: Minor
>             Fix For: java-chassis-1.0.0-m1
>
>
> Add new handler for fault injection.
> 1) If handler 'fault-injection' is added in application property 
> file(microservice.yaml) then fault-injection handler should include in 
> handler chain.
> 2) When handler is called read the configuration and listen the configuration 
> event related to fault inject. The sample configuration file is in next slide.
> 3) The configuration mainly consist
>     -delay
>     -abort
>  *delay:*can set the delay for requests based on percentage configured. The 
> unit is ms. The delay percentage default value is 100%
>  *abort*: Abort the requests based on percentage configured. For rest 
> transport protocol error code can be configurable(500, 421) and highway 
> protocol does not support for error return. The abort percentage default 
> value is 100%
> 4) This features currently supports at consumer side.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to