Skip to content
Permalink
Browse files
[Dubbo-1693] Enhance the test coverage part-14 (#1859)
* add testcase

* remove useless code
  • Loading branch information
Jasper Zhong authored and lovepoem committed May 31, 2018
1 parent 61727e1 commit 900ab0b28ccce6e2fed4c2f73679a936282a349f
Showing 8 changed files with 393 additions and 0 deletions.
@@ -44,5 +44,11 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.utils.DubboAppender;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.Client;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.Server;
import com.alibaba.dubbo.remoting.exchange.Exchangers;
import com.alibaba.dubbo.remoting.exchange.support.ExchangeHandlerAdapter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* Client reconnect test
*/
public class ClientReconnectTest {
public static void main(String[] args) {
System.out.println(3 % 1);
}

@Before
public void clear() {
DubboAppender.clear();
}

@Test
public void testReconnect() throws RemotingException, InterruptedException {
{
int port = NetUtils.getAvailablePort();
Client client = startClient(port, 200);
Assert.assertEquals(false, client.isConnected());
Server server = startServer(port);
for (int i = 0; i < 100 && !client.isConnected(); i++) {
Thread.sleep(10);
}
Assert.assertEquals(true, client.isConnected());
client.close(2000);
server.close(2000);
}
{
int port = NetUtils.getAvailablePort();
Client client = startClient(port, 20000);
Assert.assertEquals(false, client.isConnected());
Server server = startServer(port);
for (int i = 0; i < 5; i++) {
Thread.sleep(200);
}
Assert.assertEquals(false, client.isConnected());
client.close(2000);
server.close(2000);
}
}


public Client startClient(int port, int reconnectPeriod) throws RemotingException {
final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?client=netty4&check=false&" + Constants.RECONNECT_KEY + "=" + reconnectPeriod;
return Exchangers.connect(url);
}

public Server startServer(int port) throws RemotingException {
final String url = "exchange://127.0.0.1:" + port + "/client.reconnect.test?server=netty4";
return Exchangers.bind(url, new HandlerAdapter());
}

static class HandlerAdapter extends ExchangeHandlerAdapter {
@Override
public void connected(Channel channel) throws RemotingException {
}

@Override
public void disconnected(Channel channel) throws RemotingException {
}

@Override
public void caught(Channel channel, Throwable exception) throws RemotingException {
}
}
}
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
import com.alibaba.dubbo.remoting.exchange.ResponseFuture;
import com.alibaba.dubbo.remoting.exchange.support.Replier;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

/**
* ClientToServer
*/
public abstract class ClientToServerTest extends TestCase {

protected static final String LOCALHOST = "127.0.0.1";

protected ExchangeServer server;

protected ExchangeChannel client;

protected WorldHandler handler = new WorldHandler();

protected abstract ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException;

protected abstract ExchangeChannel newClient(int port) throws RemotingException;

@Override
protected void setUp() throws Exception {
super.setUp();
int port = (int) (1000 * Math.random() + 10000);
server = newServer(port, handler);
client = newClient(port);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
try {
if (server != null)
server.close();
} finally {
if (client != null)
client.close();
}
}

@Test
public void testFuture() throws Exception {
ResponseFuture future = client.request(new World("world"));
Hello result = (Hello) future.get();
Assert.assertEquals("hello,world", result.getName());
}
}
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.remoting.Transporter;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.containsString;

public class ClientsTest {
@Test
public void testGetTransportEmpty() {
try {
ExtensionLoader.getExtensionLoader(Transporter.class).getExtension("");
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected.getMessage(), containsString("Extension name == null"));
}
}

@Test(expected = IllegalArgumentException.class)
public void testGetTransportNull() {
String name = null;
ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name);
}

@Test
public void testGetTransport3() {
String name = "netty4";
assertEquals(NettyTransporter.class, ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
}

@Test(expected = IllegalStateException.class)
public void testGetTransportWrong() {
String name = "nety";
assertNull(ExtensionLoader.getExtensionLoader(Transporter.class).getExtension(name).getClass());
}
}
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import java.io.Serializable;

/**
* Result
*/
public class Hello implements Serializable {

private static final long serialVersionUID = 753429849957096150L;

private String name;

public Hello() {
}

public Hello(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.exchange.ExchangeChannel;
import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
import com.alibaba.dubbo.remoting.exchange.Exchangers;
import com.alibaba.dubbo.remoting.exchange.support.Replier;

/**
* Netty4ClientToServerTest
*/
public class NettyClientToServerTest extends ClientToServerTest {

protected ExchangeServer newServer(int port, Replier<?> receiver) throws RemotingException {
return Exchangers.bind(URL.valueOf("exchange://localhost:" + port + "?server=netty4"), receiver);
}

protected ExchangeChannel newClient(int port) throws RemotingException {
return Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?client=netty4"));
}

}
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.remoting.transport.netty4;

import java.io.Serializable;

/**
* Data
*/
public class World implements Serializable {

private static final long serialVersionUID = 8563900571013747774L;

private String name;

public World() {
}

public World(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

0 comments on commit 900ab0b

Please sign in to comment.