Issue
I Have a Multi-threaded, shared-taxi simulator that I implemented.
In addition to the threads that produce real-time activities and display the road-map, the projects contains the following threads:
- A
MatchMaker
thread that runs a matching algorithm between taxies and passengers. - Each
Taxi
runs on its own thread, which calculates at any given moment the current position of the taxi and the shortest route to pick up and drop off all the passengers that theMatchMaker
has matched to it.
I want to write a JUnit
test to check the order of operations (pickup, dropoff and route-changing) that each taxi is doing.
My naive solution is to make each thread write to a log file after each operation, e.g.
"Taxi <taxi-id> picked up <passenger-id>."
then reconstruct the order of operations from the log file. And finally, check if it matches my expectations.
However, I'm pretty sure that this approach is inefficient.
So, what is the correct/traditional way to test a thread's operations order?
Solution
Why don't you test based on the outcome? So do the scheduled pick-ups eventually get to their location? Testing on outcome makes your test a lot less sensitive to future change since it doesn't rely that much on the implementation.
Answered By - pveentjer
Answer Checked By - Pedro (JavaFixing Volunteer)