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

Change main method under dubbo-remoting-api to standard test #5026

Merged
merged 6 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -50,7 +50,7 @@ public void testReconnect() throws RemotingException, InterruptedException {
Assertions.assertFalse(client.isConnected());
Server server = startServer(port);
for (int i = 0; i < 100 && !client.isConnected(); i++) {
Thread.sleep(10);
Thread.sleep(20);
}
Assertions.assertTrue(client.isConnected());
client.close(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.remoting.transport.netty4;

import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
import org.apache.dubbo.remoting.exchange.ExchangeServer;
Expand Down Expand Up @@ -47,7 +48,7 @@ public abstract class ClientToServerTest {

@BeforeEach
protected void setUp() throws Exception {
int port = (int) (1000 * Math.random() + 10000);
int port = NetUtils.getAvailablePort();
server = newServer(port, handler);
client = newClient(port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

/**
* <code>TestService</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

/**
* <code>TestServiceImpl</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,93 +14,120 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
import org.apache.dubbo.remoting.exchange.ExchangeServer;
import org.apache.dubbo.remoting.exchange.Exchangers;
import org.apache.dubbo.remoting.exchange.support.Replier;
import org.apache.dubbo.remoting.exchange.support.ReplierDispatcher;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.*;

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


/**
* Main
* ReplierDispatcherTest
*/

public class Main {
public static void main(String[] args) throws Exception {
startServer(9010);
mutliThreadTest(10, 9010);
dataPackageTest(9010);
}
public class ReplierDispatcherTest {

private ExchangeServer exchangeServer;

private ConcurrentHashMap<String, ExchangeChannel> clients = new ConcurrentHashMap<>();

private static void startServer(int port) throws Exception {
private int port;

@BeforeEach
public void startServer() throws RemotingException {
port = NetUtils.getAvailablePort();
ReplierDispatcher dispatcher = new ReplierDispatcher();
dispatcher.addReplier(RpcMessage.class, new RpcMessageHandler());
dispatcher.addReplier(Object.class, new Replier<Object>() {
public Object reply(ExchangeChannel channel, Object msg) {
for (int i = 0; i < 10000; i++)
System.currentTimeMillis();
System.out.println("handle:" + msg + ";thread:" + Thread.currentThread().getName());
return new StringMessage("hello world");
dispatcher.addReplier(Data.class, (channel, msg) -> {
for (int i = 0; i < 10000; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is existing code. but I'm thinking what's purpose for this code? looks like this is not necessary for this unit test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this loop seems to be unnecessary, just to keep the existing code. I have changed it.

System.currentTimeMillis();
}
return new StringMessage("hello world");
});
Exchangers.bind(URL.valueOf("dubbo://localhost:" + port), dispatcher);
exchangeServer = Exchangers.bind(URL.valueOf("dubbo://localhost:" + port), dispatcher);
}

static void dataPackageTest(int port) throws Exception {

@Test
public void testDataPackage() throws Exception {
ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
Random random = new Random();
for (int i = 5; i < 100; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < i * 100; j++)
sb.append("(" + random.nextLong() + ")");
Main.Data d = new Main.Data();
sb.append("(").append(random.nextLong()).append(")");
Data d = new Data();
d.setData(sb.toString());
client.request(d).get();
Assertions.assertEquals(client.request(d).get().toString(), "hello world");
}
System.out.println("send finished.");
clients.put(Thread.currentThread().getName(), client);
}

static void mutliThreadTest(int tc, final int port) throws Exception {
Executor exec = Executors.newFixedThreadPool(tc);

@Test
public void testMultiThread() throws Exception {
int tc = 10;
ExecutorService exec = Executors.newFixedThreadPool(tc);
for (int i = 0; i < tc; i++)
exec.execute(new Runnable() {
public void run() {
try {
test(port);
} catch (Exception e) {
e.printStackTrace();
}
exec.execute(() -> {
try {
clientExchangeInfo(port);
} catch (Exception e) {
fail();
}
});
exec.shutdown();
exec.awaitTermination(10, TimeUnit.SECONDS);
}

private static void test(int port) throws Exception {
ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
void clientExchangeInfo(int port) throws Exception {
ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port + "?" + CommonConstants.TIMEOUT_KEY + "=5000"));
clients.put(Thread.currentThread().getName(), client);
MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class<?>[]{int.class, int.class}, new Object[]{55, 25})).get();
System.out.println("55+25=" + result.getResult());

for (int i = 0; i < 100; i++)
Assertions.assertEquals(result.getResult(), 80);
for (int i = 0; i < 100; i++) {
client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class<?>[]{String.class}, new Object[]{"qianlei" + i}));
}
for (int i = 0; i < 100; i++) {
client.request(new Data());
}
for (int i = 0; i < 100; i++) {
CompletableFuture<Object> future = client.request(new Data());
Assertions.assertEquals(future.get().toString(), "hello world");
}
}

for (int i = 0; i < 100; i++)
client.request(new Main.Data());

System.out.println("=====test invoke=====");
for (int i = 0; i < 100; i++) {
CompletableFuture<Object> future = client.request(new Main.Data());
System.out.println("invoke and get");
System.out.println("invoke result:" + future.get());
@AfterEach
public void tearDown() {
try {
if (exchangeServer != null)
exchangeServer.close();
} finally {
if (clients.size() != 0)
clients.forEach((key, value) -> {
value.close();
clients.remove(key);
});
}
System.out.println("=====the end=====");
}


static class Data implements Serializable {
private static final long serialVersionUID = -4666580993978548778L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting;
package org.apache.dubbo.remoting.transport.netty4;

import org.apache.dubbo.common.bytecode.NoSuchMethodException;
import org.apache.dubbo.common.bytecode.Wrapper;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.exchange.ExchangeChannel;
import org.apache.dubbo.remoting.exchange.support.Replier;

Expand Down