-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dubbo-1693] Enhance the test coverage part-14 (#1859)
* add testcase * remove useless code
- Loading branch information
Showing
8 changed files
with
393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientReconnectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientToServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...emoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/ClientsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/Hello.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...y4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/NettyClientToServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...ubbo-remoting-netty4/src/test/java/com/alibaba/dubbo/remoting/transport/netty4/World.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.