简体   繁体   中英

JUnit test thread's operations order

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:

  1. A MatchMaker thread that runs a matching algorithm between taxies and passengers.
  2. 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 the MatchMaker 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, eg
"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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM