The loop waiting for the message count to increase is not reliable because the lock has to be released between the calls, so a message can sneak in between and cause a timeout on the next iteration.
This diff updates the test to use a more simple and robust approach.