2

How to do unit tests on a multithreaded server?

Assuming I have a server that extends a thread class and every connected client will be a subthread. This server accepts a new client using socket api.

What is a good way to do unit tests on this server? What should the unit tests cover? I wonder how to simulate clients.

Update: I decided to share my code on github

Jun
  • 2,942
  • 5
  • 28
  • 50

5 Answers5

2

Answer is JMeter. Please read here Java sockets - How to simulate multiple clients.

Note: You try to create functional tests but before that you need to make unit tests for your classes. It's really important step before functional testing.

Community
  • 1
  • 1
Vladislav Kysliy
  • 3,488
  • 3
  • 32
  • 44
1

If you are talking about pure unit tests, not integration tests, you can use TestNG to preform multithreaded tests.

@Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000)
public void multithreadedTestMethdo(){
    ....
}
Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69
0

Jmeter to simulate multiple and concurrent clients and a profiler linked to the server in order to know what is going on that application server.

There are alternatives to jmeter, but i did not test them. The profiler can be jconsole which is inside the jdk..

aironman
  • 837
  • 5
  • 26
  • 55
0

Good old junit is enough for such a test suite. Declare an AtomicInteger to count errors. From an initializing method (annotated with @Before) start a thread with the server. From each test method (annotated with @Test) start multiple threads representing clients and then perform join operation to each thread. Each client and server, encountering an error, prints detailed diagnostics, increases the error counter, and exits.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
0

Also look into mocks to see how well your class "dances". I recommend mockito.

louisgab
  • 2,414
  • 1
  • 15
  • 9