How to unit testing Asynchronous Threads in Multithreaded Enviornment

Unit testing thread using one of the unit test frame like JUnit, NUnit etc. is really good exercise and quite simple to do. Some people have posted about it in the following places

here, and here

and many other places.

But I think following is much simple and easy to understand
To test single threaded or synchronous or blocking request
1) You will call some function of some class from your main function. In the calling function, set the value of it’s member variable as current thread’s thread id. Compare this value with current thread’s thread id in the main method. They should be same.

Main Method

TestClass’s execute request method

TestClass’s getRequestId

Similarly for asynchronous or not blocking request in which we spawn another thread to server our async request, it will return different id and it should not match with main thread’s threadId.

Isn’t it simple to test asynchronous (non-blocking) and synchronous (blocking) threads?

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)