Skip to content

Commit

Permalink
fix unit test for windows platform (apache#5913)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ authored Mar 26, 2020
1 parent 7dbe632 commit cefe7c3
Show file tree
Hide file tree
Showing 38 changed files with 264 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -767,15 +767,15 @@ private void loadDirectory(Map<String, Class<?>> extensionClasses, String dir, S
try {
Enumeration<java.net.URL> urls = null;
ClassLoader classLoader = findClassLoader();

// try to load from ExtensionLoader's ClassLoader first
if (extensionLoaderClassLoaderFirst) {
ClassLoader extensionLoaderClassLoader = ExtensionLoader.class.getClassLoader();
if (ClassLoader.getSystemClassLoader() != extensionLoaderClassLoader) {
urls = extensionLoaderClassLoader.getResources(fileName);
}
}

if(urls == null || !urls.hasMoreElements()) {
if (classLoader != null) {
urls = classLoader.getResources(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -41,7 +42,9 @@ public void testGenerate() throws IOException {
String value = generator.generate();
URL url = getClass().getResource("/org/apache/dubbo/common/extension/adaptive/HasAdaptiveExt$Adaptive");
try (InputStream inputStream = url.openStream()) {
String content = IOUtils.read(new InputStreamReader(inputStream, "UTF-8"));
String content = IOUtils.read(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
// in Windows platform content get from resource contains \r delimiter
content = content.replaceAll("\r","");
assertTrue(content.contains(value));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testGetRandomPort() throws Exception {
public void testGetAvailablePort() throws Exception {
assertThat(NetUtils.getAvailablePort(), greaterThan(0));
assertThat(NetUtils.getAvailablePort(12345), greaterThanOrEqualTo(12345));
assertThat(NetUtils.getAvailablePort(-1), greaterThanOrEqualTo(30000));
assertThat(NetUtils.getAvailablePort(-1), greaterThanOrEqualTo(0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public void testOnEvent() {
eventDispatcher.dispatch(new EchoEvent("Hello,World"));

assertEquals("Hello,World", listener.getSource());

// fix EventDispatcherTest.testDefaultMethods may contain OnlyHelloWorldEventListener
// ( ConditionalEventListenerTest and EventDispatcherTest are running together in one suite case )
eventDispatcher.removeAllEventListeners();
}

static class OnlyHelloWorldEventListener implements ConditionalEventListener<EchoEvent> {
Expand Down
5 changes: 5 additions & 0 deletions dubbo-metadata/dubbo-metadata-report-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@
<artifactId>embedded-redis</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.metadata.store.redis;

import org.apache.commons.lang3.SystemUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
Expand All @@ -34,6 +35,7 @@
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.embedded.RedisServer;
import redis.embedded.RedisServerBuilder;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -58,10 +60,20 @@ public void constructor(TestInfo testInfo) throws IOException {
String methodName = testInfo.getTestMethod().get().getName();
if ("testAuthRedisMetadata".equals(methodName) || ("testWrongAuthRedisMetadata".equals(methodName))) {
String password = "チェリー";
redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build();
RedisServerBuilder builder = RedisServer.builder().port(redisPort).setting("requirepass " + password);
if (SystemUtils.IS_OS_WINDOWS) {
// set maxheap to fix Windows error 0x70 while starting redis
builder.setting("maxheap 128mb");
}
redisServer = builder.build();
registryUrl = URL.valueOf("redis://username:" + password + "@localhost:" + redisPort);
} else {
redisServer = RedisServer.builder().port(redisPort).build();
RedisServerBuilder builder = RedisServer.builder().port(redisPort);
if (SystemUtils.IS_OS_WINDOWS) {
// set maxheap to fix Windows error 0x70 while starting redis
builder.setting("maxheap 128mb");
}
redisServer = builder.build();
registryUrl = URL.valueOf("redis://localhost:" + redisPort);
}

Expand All @@ -76,7 +88,6 @@ public void tearDown() throws Exception {
this.redisServer.stop();
}


@Test
public void testAsyncStoreProvider() throws ClassNotFoundException {
testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.qos.legacy;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.qos.legacy.service.DemoService;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.exchange.ExchangeClient;
Expand All @@ -41,13 +42,14 @@ public class PortTelnetHandlerTest {

private static TelnetHandler port = new PortTelnetHandler();
private static Invoker<DemoService> mockInvoker;
private static int availablePort = NetUtils.getAvailablePort();

@SuppressWarnings("unchecked")
@BeforeAll
public static void before() {
mockInvoker = mock(Invoker.class);
given(mockInvoker.getInterface()).willReturn(DemoService.class);
given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20887/demo"));
given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:" + availablePort + "/demo"));

DubboProtocol.getDubboProtocol().export(mockInvoker);
}
Expand All @@ -63,10 +65,10 @@ public static void after() {
*/
@Test
public void testListClient() throws Exception {
ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:" + availablePort + "/demo");
ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:" + availablePort + "/demo");
Thread.sleep(5000);
String result = port.telnet(null, "-l 20887");
String result = port.telnet(null, "-l " + availablePort + "");
String client1Addr = client1.getLocalAddress().toString();
String client2Addr = client2.getLocalAddress().toString();
System.out.printf("Result: %s %n", result);
Expand All @@ -79,13 +81,13 @@ public void testListClient() throws Exception {
@Test
public void testListDetail() throws RemotingException {
String result = port.telnet(null, "-l");
assertEquals("dubbo://127.0.0.1:20887", result);
assertEquals("dubbo://127.0.0.1:" + availablePort + "", result);
}

@Test
public void testListAllPort() throws RemotingException {
String result = port.telnet(null, "");
assertEquals("20887", result);
assertEquals("" + availablePort + "", result);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.pszymczyk.consul.ConsulStarterBuilder;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.status.Status;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.status.RegistryStatusChecker;
Expand All @@ -43,7 +44,7 @@ public class ConsulRegistryTest {
private static ConsulProcess consul;
private ConsulRegistry consulRegistry;
private String service = "org.apache.dubbo.test.injvmServie";
private URL serviceUrl = URL.valueOf("consul://127.0.0.1:8012/" + service + "?notify=false&methods=test1,test2");
private URL serviceUrl = URL.valueOf("consul://127.0.0.1:" + NetUtils.getAvailablePort() + "/" + service + "?notify=false&methods=test1,test2");
private URL registryUrl;
private ConsulRegistryFactory consulRegistryFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.pszymczyk.consul.ConsulProcess;
import com.pszymczyk.consul.ConsulStarterBuilder;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.client.DefaultServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void close() {

@Test
public void testRegistration() throws InterruptedException{
DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, 8012);
DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());
consulServiceDiscovery.register(serviceInstance);
Thread.sleep(5000);

Expand All @@ -86,13 +87,15 @@ private DefaultServiceInstance createServiceInstance(String serviceName, String
public void testGetInstances() throws Exception {
String serviceName = "ConsulTest77Service";
assertTrue(consulServiceDiscovery.getInstances(serviceName).isEmpty());
consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 8080));
consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 9809));
int portA = NetUtils.getAvailablePort();
int portB = NetUtils.getAvailablePort();
consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", portA));
consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", portB));
Thread.sleep(5000);
Assertions.assertFalse(consulServiceDiscovery.getInstances(serviceName).isEmpty());
List<String> r = convertToIpPort(consulServiceDiscovery.getInstances(serviceName));
assertTrue(r.contains("127.0.0.1:8080"));
assertTrue(r.contains("127.0.0.1:9809"));
assertTrue(r.contains("127.0.0.1:"+portA));
assertTrue(r.contains("127.0.0.1:"+portB));
}

private List<String> convertToIpPort(List<ServiceInstance> serviceInstances) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.registry.eureka;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.client.DefaultServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstance;

Expand All @@ -37,13 +38,13 @@ public class EurekaServiceDiscoveryTest {

private ServiceInstance serviceInstance;

private URL registryURL = URL.valueOf("eureka://127.0.0.1:8761/eureka");
private URL registryURL = URL.valueOf("eureka://127.0.0.1:" + NetUtils.getAvailablePort() + "/eureka");

@BeforeEach
public void init() throws Exception {
serviceDiscovery = new EurekaServiceDiscovery();
serviceDiscovery.initialize(registryURL);
serviceInstance = new DefaultServiceInstance("test", "127.0.0.1", 8080);
serviceInstance = new DefaultServiceInstance("test", "127.0.0.1", NetUtils.getAvailablePort());
serviceDiscovery.register(serviceInstance);
}

Expand Down
5 changes: 5 additions & 0 deletions dubbo-registry/dubbo-registry-multiple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@
<artifactId>embedded-redis</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.registry.multiple;

import org.apache.commons.lang3.SystemUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.NotifyListener;
Expand All @@ -31,6 +32,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import redis.embedded.RedisServer;
import redis.embedded.RedisServerBuilder;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -66,7 +68,12 @@ public static void setUp() throws Exception {
zookeeperRegistryURLStr = "zookeeper://127.0.0.1:" + zkServerPort;

redisServerPort = NetUtils.getAvailablePort();
redisServer = new RedisServer(redisServerPort);
RedisServerBuilder builder = RedisServer.builder().port(redisServerPort);
if (SystemUtils.IS_OS_WINDOWS) {
// set maxheap to fix Windows error 0x70 while starting redis
builder.setting("maxheap 128mb");
}
redisServer = builder.build();
redisServer.start();
redisRegistryURLStr = "redis://127.0.0.1:" + redisServerPort;

Expand Down
5 changes: 5 additions & 0 deletions dubbo-registry/dubbo-registry-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
<artifactId>embedded-redis</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.registry.redis;

import org.apache.commons.lang3.SystemUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.NotifyListener;
Expand All @@ -26,6 +27,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.embedded.RedisServer;
import redis.embedded.RedisServerBuilder;

import java.util.List;
import java.util.Map;
Expand All @@ -46,10 +48,14 @@ public class RedisRegistryTest {
@BeforeEach
public void setUp() throws Exception {
int redisPort = NetUtils.getAvailablePort();
this.redisServer = new RedisServer(redisPort);
RedisServerBuilder builder = RedisServer.builder().port(redisPort);
if (SystemUtils.IS_OS_WINDOWS) {
// set maxheap to fix Windows error 0x70 while starting redis
builder.setting("maxheap 128mb");
}
this.redisServer = builder.build();
this.redisServer.start();
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);

redisRegistry = (RedisRegistry) new RedisRegistryFactory().createRegistry(registryUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.registry.zookeeper;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.registry.client.DefaultServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstance;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void close() throws Exception {
@Test
public void testRegistration() {

DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, 8080);
DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());

discovery.register(serviceInstance);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.remoting.transport.netty;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.RemotingServer;
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
Expand All @@ -35,11 +36,11 @@
*/
public class NettyClientTest {
static RemotingServer server;

static int port = NetUtils.getAvailablePort();

@BeforeAll
public static void setUp() throws Exception {
server = Exchangers.bind(URL.valueOf("exchange://localhost:10001?server=netty3"), new TelnetServerHandler());
server = Exchangers.bind(URL.valueOf("exchange://localhost:" + port + "?server=netty3"), new TelnetServerHandler());
}

@AfterAll
Expand All @@ -60,7 +61,7 @@ public static void main(String[] args) throws RemotingException, InterruptedExce
public void testClientClose() throws Exception {
List<ExchangeChannel> clients = new ArrayList<ExchangeChannel>(100);
for (int i = 0; i < 100; i++) {
ExchangeChannel client = Exchangers.connect(URL.valueOf("exchange://localhost:10001?client=netty3"));
ExchangeChannel client = Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty3"));
Thread.sleep(5);
clients.add(client);
}
Expand All @@ -73,7 +74,7 @@ public void testClientClose() throws Exception {
@Test
public void testServerClose() throws Exception {
for (int i = 0; i < 100; i++) {
RemotingServer aServer = Exchangers.bind(URL.valueOf("exchange://localhost:" + (6000 + i) + "?server=netty3"), new TelnetServerHandler());
RemotingServer aServer = Exchangers.bind(URL.valueOf("exchange://localhost:" + NetUtils.getAvailablePort(6000) + "?server=netty3"), new TelnetServerHandler());
aServer.close();
}
}
Expand Down
Loading

0 comments on commit cefe7c3

Please sign in to comment.