[英]Multiple Instances test using junit
I have a java server on a server socket. 我在服务器套接字上有一个Java服务器。 Clients connect to this server socket, and exchange over input/output object streams.
客户端连接到该服务器套接字,并通过输入/输出对象流进行交换。 Now I need to test the app for scalability.
现在,我需要测试该应用程序的可伸缩性。 Which means I need to run the same test making requests, and test if the server is able to handle requests from random clients.
这意味着我需要运行相同的测试请求,并测试服务器是否能够处理来自随机客户端的请求。
Is the below junit based test case the right way to test random connections/requests. 下面的基于junit的测试用例是否是测试随机连接/请求的正确方法? I get a feeling that the below code is testing clients sequencially.
我感到以下代码正在依次测试客户端。
Some links I read, and did not work for me 我阅读了一些链接,但对我不起作用
Creating a JUnit testsuite with multiple instances of a Parameterized test 使用参数化测试的多个实例创建JUnit测试套件
public class ScalePostiveTestCases {
SendQueue sendQueue;
Socket clientSocket = null;
public static void main(String[] args) throws Throwable {
testSearching() ;
}
@SuppressWarnings("unchecked")
private static void testSearching() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
TestSuite tests = new TestSuite();
for (int i = 0; i < 99; i++) {
Class<SingleSearchTest> singleSearchTest = (Class<SingleSearchTest>) ClassLoader
.getSystemClassLoader().loadClass(
"SingleSearchTest");
SingleSearchTest singleSearch = singleSearchTest.newInstance();
tests.addTest(singleSearch);
}
TestRunner.run(tests);
}
public class SingleSearchTest extends TestCase {
static SingleSearchTest singleSearch = null;
private String device, connection, user;
private ClientSession clientSession;
private SendQueue sendQueue;
public static SingleSearchTest main(String args[]) {
singleSearch = new SingleSearchTest();
return singleSearch;
}
public SingleSearchTest() {
super("testSingleSearch");
Random rand = new Random();
}
}
You are creating many test which have one client each. 您正在创建许多测试,每个测试都有一个客户端。
What you need to do is have one plain test which creates many clients which are executed concurrently. 您需要做的是做一个简单的测试,该测试创建许多并发执行的客户端。
In my unit tests; 在我的单元测试中;
Just figured out that I was using threadExecutor.isTerminated and threadExecutor.showdown, instead of using threadExecutor.isShutDown and threadExecutor.shutdownNow. 只是发现我在使用threadExecutor.isTerminated和threadExecutor.showdown,而不是使用threadExecutor.isShutDown和threadExecutor.shutdownNow。 I also tried threadExecutor.awaitTermination.
我也尝试过threadExecutor.awaitTermination。 Worked.
工作了。 Basically both tests run in parallel.
基本上,两个测试都是并行运行的。
public static void testLoading() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleClientTest clientTest = new SingleClientTest();
randomStringList.add(clientTest.getRandomString());
threadExecutor.execute(clientTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
public static void testSearching() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleSearchTest searchTest = new SingleSearchTest(randomStringList);
threadExecutor.execute(searchTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.