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/01/26 01:05:51 UTC
[incubator-servicecomb-java-chassis] 01/06: SCB-190 inherited
invocation context for reactive
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 334256e83c69cc61a2e22eb85388a492b10a7fe9
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Jan 23 09:12:34 2018 +0800
SCB-190 inherited invocation context for reactive
---
.../core/provider/consumer/InvokerUtils.java | 10 ++++-
.../core/provider/consumer/TestInvokerUtils.java | 38 ++++++++++++-----
.../swagger/invocation/SwaggerInvocation.java | 12 ++++--
.../swagger/invocation/TestSwaggerInvocation.java} | 48 ++++++++--------------
4 files changed, 60 insertions(+), 48 deletions(-)
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
index 2635ac3..2504ab0 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
@@ -25,6 +25,7 @@ import org.apache.servicecomb.foundation.common.utils.EventUtils;
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.context.ContextUtils;
import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.slf4j.Logger;
@@ -94,8 +95,13 @@ public final class InvokerUtils {
invocation.setResponseExecutor(respExecutor);
invocation.next(ar -> {
- invocation.triggerFinishedEvent(ar.getStatusCode(), ar.isSuccessed());
- asyncResp.handle(ar);
+ ContextUtils.setInvocationContext(invocation.getParentContext());
+ try {
+ invocation.triggerFinishedEvent(ar.getStatusCode(), ar.isSuccessed());
+ asyncResp.handle(ar);
+ } finally {
+ ContextUtils.removeInvocationContext();
+ }
});
} catch (Throwable e) {
//if throw exception,we can use 500 for status code ?
diff --git a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
index 5560519..d975e65 100644
--- a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
+++ b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
@@ -17,6 +17,8 @@
package org.apache.servicecomb.core.provider.consumer;
+import javax.xml.ws.Holder;
+
import org.apache.servicecomb.core.BootListener;
import org.apache.servicecomb.core.CseContext;
import org.apache.servicecomb.core.Invocation;
@@ -25,6 +27,8 @@ import org.apache.servicecomb.core.definition.SchemaMeta;
import org.apache.servicecomb.core.invocation.InvocationFactory;
import org.apache.servicecomb.swagger.invocation.AsyncResponse;
import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.junit.Assert;
import org.junit.Test;
@@ -34,6 +38,7 @@ import mockit.Expectations;
import mockit.Injectable;
import mockit.Mock;
import mockit.MockUp;
+import mockit.Mocked;
public class TestInvokerUtils {
@@ -61,17 +66,28 @@ public class TestInvokerUtils {
}
@Test
- public void testReactiveInvoke() {
- Invocation invocation = Mockito.mock(Invocation.class);
- AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
- boolean validAssert;
- try {
- InvokerUtils.reactiveInvoke(invocation, asyncResp);
- validAssert = true;
- } catch (Exception e) {
- validAssert = false;
- }
- Assert.assertTrue(validAssert);
+ public void testReactiveInvoke(@Mocked Invocation invocation, @Mocked InvocationContext parentContext,
+ @Mocked Response response) {
+ new MockUp<Invocation>(invocation) {
+ @Mock
+ InvocationContext getParentContext() {
+ return parentContext;
+ }
+
+ @Mock
+ void next(AsyncResponse asyncResp) {
+ asyncResp.handle(response);
+ }
+ };
+
+
+ Holder<InvocationContext> holder = new Holder<>();
+ InvokerUtils.reactiveInvoke(invocation, ar -> {
+ holder.value = ContextUtils.getInvocationContext();
+ });
+
+ Assert.assertNull(ContextUtils.getInvocationContext());
+ Assert.assertSame(parentContext, holder.value);
}
@SuppressWarnings("deprecation")
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java
index d71c4f8..cd2c094 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java
@@ -26,13 +26,19 @@ public class SwaggerInvocation extends InvocationContext {
protected Object[] swaggerArguments;
+ protected InvocationContext parentContext;
+
public SwaggerInvocation() {
- InvocationContext context = ContextUtils.getInvocationContext();
- if (context != null) {
- addContext(context.getContext());
+ parentContext = ContextUtils.getInvocationContext();
+ if (parentContext != null) {
+ addContext(parentContext.getContext());
}
}
+ public InvocationContext getParentContext() {
+ return parentContext;
+ }
+
public InvocationType getInvocationType() {
return invocationType;
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/TestSwaggerInvocation.java
similarity index 51%
copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java
copy to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/TestSwaggerInvocation.java
index d71c4f8..8517771 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/SwaggerInvocation.java
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/TestSwaggerInvocation.java
@@ -14,47 +14,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.servicecomb.swagger.invocation;
import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+import org.junit.Assert;
+import org.junit.Test;
-public class SwaggerInvocation extends InvocationContext {
- // 本实例是在consumer端,还是在provider端
- protected InvocationType invocationType;
+import mockit.Mocked;
- protected Object[] swaggerArguments;
+public class TestSwaggerInvocation {
+ @Test
+ public void construct_withContext(@Mocked InvocationContext parentContext) {
+ ContextUtils.setInvocationContext(parentContext);
- public SwaggerInvocation() {
- InvocationContext context = ContextUtils.getInvocationContext();
- if (context != null) {
- addContext(context.getContext());
+ try {
+ SwaggerInvocation invocation = new SwaggerInvocation();
+ Assert.assertSame(parentContext, invocation.getParentContext());
+ } finally {
+ ContextUtils.removeInvocationContext();
}
}
- public InvocationType getInvocationType() {
- return invocationType;
- }
-
- public Object[] getSwaggerArguments() {
- return swaggerArguments;
- }
-
- @SuppressWarnings("unchecked")
- public <T> T getSwaggerArgument(int idx) {
- return (T) swaggerArguments[idx];
- }
-
- public void setSwaggerArguments(Object[] swaggerArguments) {
- this.swaggerArguments = swaggerArguments;
- }
-
- public void setSwaggerArgument(int idx, Object swaggerArgument) {
- this.swaggerArguments[idx] = swaggerArgument;
- }
-
- public String getInvocationQualifiedName() {
- return invocationType.name();
+ @Test
+ public void construct_noContext() {
+ SwaggerInvocation invocation = new SwaggerInvocation();
+ Assert.assertNull(invocation.getParentContext());
}
}
--
To stop receiving notification emails like this one, please contact
liubao@apache.org.