Search in sources :

Example 1 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class WebsocketUtils method open.

public static void open(IpPort ipPort, String url, Handler<Void> onOpen, Handler<Void> onClose, Handler<Buffer> onMessage, Handler<Throwable> onException, Handler<Throwable> onConnectFailed) {
    HttpClientWithContext vertxHttpClient = WebsocketClientPool.INSTANCE.getClient();
    vertxHttpClient.runOnContext(client -> {
        client.websocket(ipPort.getPort(), ipPort.getHostOrIp(), url, RestUtils.getDefaultHeaders().addAll(RestUtils.getSignAuthHeaders(RestUtils.createSignRequest(null, ipPort, new RequestParam(), url, new HashMap<>()))), ws -> {
            onOpen.handle(null);
            ws.exceptionHandler(v -> {
                onException.handle(v);
                try {
                    ws.close();
                } catch (Exception err) {
                    LOGGER.error("ws close error.", err);
                }
            });
            ws.closeHandler(v -> {
                onClose.handle(v);
                try {
                    ws.close();
                } catch (Exception err) {
                    LOGGER.error("ws close error.", err);
                }
            });
            ws.handler(onMessage);
        }, onConnectFailed);
    });
}
Also used : HashMap(java.util.HashMap) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)

Example 2 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientPoolManager method findByContext_reactive.

@Test
public void findByContext_reactive() {
    HttpClientWithContext notMatchPool = new HttpClientWithContext(null, null);
    pools.add(notMatchPool);
    new Expectations(VertxImpl.class) {

        {
            factory.createClientPool();
            result = new HttpClientWithContext(null, null);
            VertxImpl.context();
            result = context;
        }
    };
    HttpClientWithContext result = poolMgr.findByContext();
    Assert.assertNotSame(notMatchPool, result);
    // find again, get the same result
    Assert.assertSame(result, poolMgr.findByContext());
}
Also used : Expectations(mockit.Expectations) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Test(org.junit.Test)

Example 3 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientPoolManager method findByContext_otherVertx.

@Test
public void findByContext_otherVertx(@Mocked Vertx otherVertx, @Mocked Context otherContext) {
    HttpClientWithContext pool = new HttpClientWithContext(null, null);
    pools.add(pool);
    new Expectations(VertxImpl.class) {

        {
            VertxImpl.context();
            result = otherContext;
            otherContext.owner();
            result = otherVertx;
        }
    };
    Assert.assertSame(pool, poolMgr.findByContext());
}
Also used : Expectations(mockit.Expectations) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Test(org.junit.Test)

Example 4 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestClientVerticle method start.

@Test
public void start(@Mocked Context context) throws Exception {
    AtomicInteger count = new AtomicInteger();
    ClientPoolManager<HttpClientWithContext> clientMgr = new MockUp<ClientPoolManager<HttpClientWithContext>>() {

        @Mock
        HttpClientWithContext createClientPool() {
            count.incrementAndGet();
            return null;
        }
    }.getMockInstance();
    clientVerticle.init(null, context);
    JsonObject config = new SimpleJsonObject();
    config.put(ClientVerticle.CLIENT_MGR, clientMgr);
    new Expectations() {

        {
            context.config();
            result = config;
        }
    };
    clientVerticle.start();
    Assert.assertEquals(1, count.get());
}
Also used : Expectations(mockit.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) SimpleJsonObject(org.apache.servicecomb.foundation.vertx.SimpleJsonObject) JsonObject(io.vertx.core.json.JsonObject) SimpleJsonObject(org.apache.servicecomb.foundation.vertx.SimpleJsonObject) Mock(mockit.Mock) Test(org.junit.Test)

Example 5 with HttpClientWithContext

use of org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext in project incubator-servicecomb-java-chassis by apache.

the class TestVertxHttpMethod method testDoMethodNullPointerException.

@Test
public void testDoMethodNullPointerException(@Mocked HttpClient httpClient) throws Exception {
    Context context = new MockUp<Context>() {

        @Mock
        public void runOnContext(Handler<Void> action) {
            action.handle(null);
        }
    }.getMockInstance();
    HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
    Invocation invocation = mock(Invocation.class);
    AsyncResponse asyncResp = mock(AsyncResponse.class);
    try {
        this.doMethod(httpClientWithContext, invocation, asyncResp);
        fail("Expect to throw NullPointerException, but got none");
    } catch (NullPointerException e) {
    }
}
Also used : Context(io.vertx.core.Context) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Invocation(org.apache.servicecomb.core.Invocation) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Mock(mockit.Mock) Test(org.junit.Test)

Aggregations

HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)10 Test (org.junit.Test)8 Expectations (mockit.Expectations)6 HashMap (java.util.HashMap)3 Mock (mockit.Mock)3 Context (io.vertx.core.Context)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Invocation (org.apache.servicecomb.core.Invocation)2 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)2 MultiMap (io.vertx.core.MultiMap)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpMethod (io.vertx.core.http.HttpMethod)1 JsonObject (io.vertx.core.json.JsonObject)1 Map (java.util.Map)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1 URLPathBuilder (org.apache.servicecomb.common.rest.definition.path.URLPathBuilder)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 SignRequest (org.apache.servicecomb.foundation.auth.SignRequest)1 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)1