Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-1693] Enhance the test coverage part-14 #1859

Merged
merged 2 commits into from
May 31, 2018
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
6 changes: 6 additions & 0 deletions dubbo-remoting/dubbo-remoting-netty4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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 {
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading