Test multiple instances using junit

I have a Java server on the server. Clients connect to this server socket and exchange streams of I / O objects. Now I need to check the application for scalability. This means that I need to run the same test requests and check if the server is capable of handling requests from random clients.

Is a junit based test case the right way to check for random connections / requests. I get the feeling that the code below is testing clients sequentially.

Some links that I read and did not work for me

Creating a JUnit testuite with multiple test instances with parameters

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(); } } 
+4
source share
2 answers

You create many tests, each of which has one client.

What you need to do is one simple test that creates many clients that run simultaneously.


In my unit tests

  • start the server as a separate thread (so I can also disable it)
  • use for ExecutionService as a thread pool for clients
  • It has a cycle for creating all tasks, each of which creates a client and implements them. Upon completion of the return from the task.
  • shutting down ExecutorService.
  • complete all the tasks and check if they passed. If they are thrown and an error occurs, the error will be selected in the current (test) stream.
+2
source

It just turned out that I used threadExecutor.isTerminated and threadExecutor.showdown, instead of using threadExecutor.isShutDown and threadExecutor.shutdownNow. I also tried threadExecutor.awaitTermination. Have worked. Basically, both tests are performed 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()) { // } } 
0
source

Source: https://habr.com/ru/post/1386707/


All Articles