简体   繁体   English

使用junit进行多实例测试

[英]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; 在我的单元测试中;

  • start the server as a separate thread (so I can also shut it down) 作为单独的线程启动服务器(因此我也可以将其关闭)
  • use an ExecutorService as a Thread pool for the clients 使用ExecutorService作为客户端的线程池
  • have a loop to create all the tasks which each create a client and exercise them. 有一个循环来创建所有任务,每个任务都创建一个客户端并进行锻炼。 When finished return from the task. 完成后从任务中返回。
  • shutdown the ExecutorService. 关闭ExecutorService。
  • go through all the tasks and check they passed. 完成所有任务并检查它们是否通过。 If they threw and error, the error will be thrown in the current (testing) thread. 如果它们抛出错误,错误将在当前(测试)线程中抛出。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM