You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/19 14:10:56 UTC

[incubator-servicecomb-java-chassis] branch master updated: SCB-256 fix bug: SC 0.5 is not compatible to 0.4, change sdk to support 0.5

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 462f279  SCB-256 fix bug: SC 0.5 is not compatible to 0.4, change sdk to support 0.5
462f279 is described below

commit 462f2790bd24ce6cfd84e0c8862caff5db0a784a
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Jan 18 21:08:43 2018 +0800

    SCB-256 fix bug: SC 0.5 is not compatible to 0.4, change sdk to support 0.5
---
 .../client/http/ServiceRegistryClientImpl.java     | 20 ++++++++-
 .../client/http/TestServiceRegistryClientImpl.java | 51 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index 97a40d8..64f9a93 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -57,6 +57,9 @@ import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.VisibleForTesting;
+
+import io.netty.handler.codec.http.HttpStatusClass;
 import io.vertx.core.Handler;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientResponse;
@@ -85,8 +88,9 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
     RestUtils.httpDo(requestContext, responseHandler);
   }
 
+  @VisibleForTesting
   @SuppressWarnings("unchecked")
-  private <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, Class<T> cls,
+  protected <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, Class<T> cls,
       Holder<T> holder) {
     return restResponse -> {
       RequestContext requestContext = restResponse.getRequestContext();
@@ -107,6 +111,18 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
               countDownLatch.countDown();
               return;
             }
+
+            // no need to support 304 in this place
+            if (!HttpStatusClass.SUCCESS.equals(HttpStatusClass.valueOf(response.statusCode()))) {
+              LOGGER.warn("get response for {} failed, {}:{}, {}",
+                  cls.getName(),
+                  response.statusCode(),
+                  response.statusMessage(),
+                  bodyBuffer.toString());
+              countDownLatch.countDown();
+              return;
+            }
+
             try {
               holder.value =
                   JsonUtils.readValue(bodyBuffer.getBytes(), cls);
@@ -259,7 +275,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient {
           schemaId,
           e);
     }
-    return holder.value != null;
+    return holder.value != null && schemaId.equals(holder.value.getSchemaId());
   }
 
   @Override
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
index a8716dd..a171531 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
+import javax.ws.rs.core.Response.Status;
 import javax.xml.ws.Holder;
 
 import org.apache.log4j.Appender;
@@ -30,6 +31,7 @@ import org.apache.log4j.Logger;
 import org.apache.log4j.spi.LoggingEvent;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory;
+import org.apache.servicecomb.serviceregistry.api.response.GetExistenceResponse;
 import org.apache.servicecomb.serviceregistry.client.ClientException;
 import org.apache.servicecomb.serviceregistry.client.IpPortManager;
 import org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl.ResponseWrapper;
@@ -46,6 +48,7 @@ import io.vertx.core.http.HttpClientOptions;
 import io.vertx.core.http.HttpClientResponse;
 import io.vertx.core.http.HttpVersion;
 import mockit.Deencapsulation;
+import mockit.Expectations;
 import mockit.Mock;
 import mockit.MockUp;
 import mockit.Mocked;
@@ -252,6 +255,54 @@ public class TestServiceRegistryClientImpl {
     }.run();
   }
 
+  @Test
+  public void syncHandler_failed(@Mocked RequestContext requestContext,
+      @Mocked HttpClientResponse response) {
+    CountDownLatch countDownLatch = new CountDownLatch(1);
+    Class<GetExistenceResponse> cls = GetExistenceResponse.class;
+    Holder<GetExistenceResponse> holder = new Holder<>();
+    Handler<RestResponse> handler = oClient.syncHandler(countDownLatch, cls, holder);
+
+    Holder<Handler<Buffer>> bodyHandlerHolder = new Holder<>();
+    new MockUp<HttpClientResponse>(response) {
+      @Mock
+      HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) {
+        bodyHandlerHolder.value = bodyHandler;
+        return null;
+      }
+    };
+    new Expectations() {
+      {
+        response.statusCode();
+        result = 400;
+        response.statusMessage();
+        result = Status.BAD_REQUEST.getReasonPhrase();
+      }
+    };
+
+    RestResponse event = new RestResponse(requestContext, response);
+    handler.handle(event);
+
+    Buffer bodyBuffer = Buffer.buffer("{}");
+    bodyHandlerHolder.value.handle(bodyBuffer);
+
+    Assert.assertNull(holder.value);
+  }
+
+  @Test
+  public void isSchemaExist() {
+    String microserviceId = "msId";
+    String schemaId = "schemaId";
+
+    new MockUp<RestUtils>() {
+      @Mock
+      void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
+        Holder<GetExistenceResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
+        holder.value = new GetExistenceResponse();
+      }
+    };
+    Assert.assertFalse(oClient.isSchemaExist(microserviceId, schemaId));
+  }
 
   @Test
   public void testFindServiceInstance() {

-- 
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].