Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

import io.servicecomb.foundation.vertx.client.AbstractClientVerticle;
import io.servicecomb.foundation.vertx.client.ClientPoolManager;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Context;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.impl.FileResolver;
import io.vertx.core.impl.VertxImplEx;
import io.vertx.core.logging.SLF4JLogDelegateFactory;

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public static Vertx getOrCreateVertxByName(String name, VertxOptions vertxOption
synchronized (VertxUtils.class) {
vertx = getVertxByName(name);
if (vertx == null) {
vertx = init(vertxOptions);
vertx = init(name, vertxOptions);
vertxMap.put(name, vertx);
}
}
Expand All @@ -117,6 +117,10 @@ public static Vertx getOrCreateVertxByName(String name, VertxOptions vertxOption
}

public static Vertx init(VertxOptions vertxOptions) {
return init(null, vertxOptions);
}

public static Vertx init(String name, VertxOptions vertxOptions) {
if (vertxOptions == null) {
vertxOptions = new VertxOptions();
}
Expand All @@ -128,7 +132,7 @@ public static Vertx init(VertxOptions vertxOptions) {
}

configureVertxFileCaching();
return Vertx.vertx(vertxOptions);
return new VertxImplEx(name, vertxOptions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.vertx.core.impl;

import java.lang.reflect.Field;

import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import io.vertx.core.VertxOptions;

public class VertxImplEx extends VertxImpl {
public VertxImplEx(String name, VertxOptions vertxOptions) {
super(vertxOptions);

if (StringUtils.isEmpty(name)) {
return;
}

Field field = ReflectionUtils.findField(VertxImpl.class, "eventLoopThreadFactory");
field.setAccessible(true);
VertxThreadFactory eventLoopThreadFactory = (VertxThreadFactory) ReflectionUtils.getField(field, this);

field = ReflectionUtils.findField(eventLoopThreadFactory.getClass(), "prefix");
field.setAccessible(true);

String prefix = (String) ReflectionUtils.getField(field, eventLoopThreadFactory);
ReflectionUtils.setField(field, eventLoopThreadFactory, name + "-" + prefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@

import io.servicecomb.foundation.vertx.client.http.HttpClientVerticle;
import io.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
import io.vertx.core.Vertx;

public class TestClient {

@Test
public void testHttpClientVerticle() throws Exception {
Vertx vertx = VertxUtils.init(null);

HttpClientVerticle oVerticle = new HttpClientVerticle();
oVerticle.init(VertxUtils.init(null), null);
oVerticle.init(vertx, null);
Assert.assertEquals("clientMgr", HttpClientVerticle.CLIENT_MGR);
Assert.assertEquals("poolCount", HttpClientVerticle.POOL_COUNT);
Assert.assertEquals("clientOptions", HttpClientVerticle.CLIENT_OPTIONS);
HttpClientWithContext oContextClient = new HttpClientWithContext(null, null);
Assert.assertEquals(null, oContextClient.getHttpClient());

vertx.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ protected void startCheckTimeout(TcpClientConfig clientConfig, Context context)
Deencapsulation.invoke(oTcpClient, "onReply", l, hBuffer, bBuffer);
oTcpClient.checkTimeout();
Assert.assertNotNull(oTcpClient);

vertx.close();
}

@Test
Expand All @@ -133,11 +135,15 @@ public void testTcpOutputStream() {

@Test
public void testTcpServerStarter() {
Vertx vertx = VertxUtils.init(null);

URIEndpointObject endpiont = new URIEndpointObject("highway://127.0.0.1:9900");
TcpServer oStarter = new TcpServer(endpiont);
oStarter.init(VertxUtils.init(null), "", null);
oStarter.init(vertx, "", null);
Assert.assertNotNull(oStarter);
//TODO Need to find a way to Assert TcpServerStarter as this obbject does not return any values.

vertx.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,49 @@

package io.servicecomb.foundation.vertx;

import org.junit.Assert;
import java.util.concurrent.CountDownLatch;

import javax.xml.ws.Holder;

import org.junit.Assert;
import org.junit.Test;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;

public class TestVertex {
@Test
public void testGetOrCreateVertx() throws InterruptedException {
Vertx vertx = VertxUtils.getOrCreateVertxByName("ut", null);

Holder<String> name = new Holder<>();
CountDownLatch latch = new CountDownLatch(1);
vertx.runOnContext(v -> {
name.value = Thread.currentThread().getName();
latch.countDown();
});
latch.await();

Assert.assertEquals(name.value, "ut-vert.x-eventloop-thread-0");
vertx.close();
}

@Test
public void testVertxUtils() {
VertxUtils.init(null);
public void testVertxUtilsInitNullOptions() {
Vertx vertx = VertxUtils.init(null);
Assert.assertNotEquals(null, vertx);
vertx.close();
}

@Test
public void testVertxUtilsInitWithOptions() {
VertxOptions oOptions = new VertxOptions();
oOptions.setClustered(false);
Assert.assertNotEquals(null,VertxUtils.init(oOptions));

Vertx vertx = VertxUtils.init(oOptions);
Assert.assertNotEquals(null, vertx);
vertx.close();
}

@Test
Expand Down