You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/15 11:12:18 UTC

[incubator-servicecomb-java-chassis] 02/05: SCB-374 [WIP] invocation add publish life event method

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 4ea8e6f34b398bd9513b28afa612dcf791c1c6fa
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Mar 9 15:25:00 2018 +0800

    SCB-374 [WIP] invocation add publish life event method
---
 .../org/apache/servicecomb/core/Invocation.java    |  49 ++++++--
 .../apache/servicecomb/core/TestInvocation.java    | 138 +++++++++++++++++++++
 2 files changed, 180 insertions(+), 7 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/Invocation.java b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
index d26c903..369f071 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
@@ -24,12 +24,16 @@ import java.util.concurrent.Executor;
 
 import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.core.definition.SchemaMeta;
-import org.apache.servicecomb.core.metrics.InvocationFinishedEvent;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
 import org.apache.servicecomb.core.metrics.InvocationStartExecutionEvent;
+import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.foundation.common.event.EventBus;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.apache.servicecomb.swagger.invocation.InvocationType;
+import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
 
 public class Invocation extends SwaggerInvocation {
@@ -53,7 +57,6 @@ public class Invocation extends SwaggerInvocation {
 
   private int handlerIndex;
 
-
   // 应答的处理器
   // 同步模式:避免应答在网络线程中处理解码等等业务级逻辑
   private Executor responseExecutor;
@@ -64,8 +67,12 @@ public class Invocation extends SwaggerInvocation {
 
   private boolean sync = true;
 
-  public void setStartTime(long startTime) {
-    this.startTime = startTime;
+  public long getStartTime() {
+    return startTime;
+  }
+
+  public long getStartExecutionTime() {
+    return startExecutionTime;
   }
 
   public Invocation(ReferenceConfig referenceConfig, OperationMeta operationMeta, Object[] swaggerArguments) {
@@ -185,7 +192,23 @@ public class Invocation extends SwaggerInvocation {
     return operationMeta.getMicroserviceQualifiedName();
   }
 
-  public void triggerStartExecutionEvent() {
+  public void onStart() {
+    this.startTime = System.nanoTime();
+    EventManager.post(new InvocationStartEvent(this));
+
+    // old logic, need to be deleted
+    EventBus.getInstance().triggerEvent(new InvocationStartedEvent(getMicroserviceQualifiedName(),
+        invocationType, startTime));
+  }
+
+  public void onStartExecute() {
+    this.startExecutionTime = System.nanoTime();
+
+    // old logic, need to be deleted
+    triggerStartExecutionEvent();
+  }
+
+  private void triggerStartExecutionEvent() {
     if (InvocationType.PRODUCER.equals(invocationType)) {
       this.startExecutionTime = System.nanoTime();
       EventBus.getInstance()
@@ -193,10 +216,18 @@ public class Invocation extends SwaggerInvocation {
     }
   }
 
-  public void triggerFinishedEvent(int statusCode) {
+  public void onFinish(Response response) {
+    EventManager.post(new InvocationFinishEvent(this, response));
+
+    // old logic, need to be deleted
+    triggerFinishedEvent(response.getStatusCode());
+  }
+
+  private void triggerFinishedEvent(int statusCode) {
     long finishedTime = System.nanoTime();
     EventBus.getInstance()
-        .triggerEvent(new InvocationFinishedEvent(operationMeta.getMicroserviceQualifiedName(), this.invocationType,
+        .triggerEvent(new org.apache.servicecomb.core.metrics.InvocationFinishedEvent(
+            operationMeta.getMicroserviceQualifiedName(), this.invocationType,
             startExecutionTime - startTime, finishedTime - startExecutionTime,
             finishedTime - startTime, statusCode));
   }
@@ -208,4 +239,8 @@ public class Invocation extends SwaggerInvocation {
   public void setSync(boolean sync) {
     this.sync = sync;
   }
+
+  public boolean isConsumer() {
+    return InvocationType.CONSUMER.equals(invocationType);
+  }
 }
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
new file mode 100644
index 0000000..49352ab
--- /dev/null
+++ b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
@@ -0,0 +1,138 @@
+/*
+ * 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.core;
+
+import javax.xml.ws.Holder;
+
+import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
+import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+
+public class TestInvocation {
+  Invocation invocation;
+
+  @Mocked
+  Endpoint endpoint;
+
+  @Mocked
+  OperationMeta operationMeta;
+
+  @Mocked
+  Object[] swaggerArguments;
+
+  static long currentNanoTime = 123;
+
+  @BeforeClass
+  public static void classSetup() {
+    EventManager.eventBus = new EventBus();
+  }
+
+  protected static void mockNonaTime() {
+    new MockUp<System>() {
+      @Mock
+      long nanoTime() {
+        return currentNanoTime;
+      }
+    };
+  }
+
+  @AfterClass
+  public static void classTeardown() {
+    EventManager.eventBus = new EventBus();
+  }
+
+  @Test
+  public void onStart() {
+    mockNonaTime();
+
+    Holder<Invocation> result = new Holder<>();
+    Object subscriber = new Object() {
+      @Subscribe
+      public void onStart(InvocationStartEvent event) {
+        result.value = event.getInvocation();
+      }
+    };
+    EventManager.register(subscriber);
+
+    Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
+    invocation.onStart();
+
+    Assert.assertEquals(currentNanoTime, result.value.getStartTime());
+    Assert.assertSame(invocation, result.value);
+
+    EventManager.unregister(subscriber);
+  }
+
+  @Test
+  public void onStartExecute() {
+    mockNonaTime();
+
+    Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
+    invocation.onStartExecute();
+
+    Assert.assertEquals(currentNanoTime, invocation.getStartExecutionTime());
+  }
+
+  @Test
+  public void onFinish(@Mocked Response response) {
+    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, swaggerArguments);
+    invocation.onFinish(response);
+
+    Assert.assertEquals(currentNanoTime, result.value.getNanoCurrent());
+    Assert.assertSame(invocation, result.value.getInvocation());
+    Assert.assertSame(response, result.value.getResponse());
+
+    EventManager.unregister(subscriber);
+  }
+
+  @Test
+  public void isConsumer_yes() {
+    Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
+    Assert.assertFalse(invocation.isConsumer());
+  }
+
+  @Test
+  public void isConsumer_no(@Mocked ReferenceConfig referenceConfig) {
+    Invocation invocation = new Invocation(referenceConfig, operationMeta, swaggerArguments);
+    Assert.assertTrue(invocation.isConsumer());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.