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

2
  • What kind of server do you mean HTTP/plain sockets? please expand a bit your question Commented Dec 16, 2016 at 8:07
  • right, plain sockets. Commented Dec 16, 2016 at 8:10

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes unit tests will give you faster feedback, and should be much easier to debug too.
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(){
    ....
}

Comments

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..

Comments

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.