How to Unit Test a Thread which has an infinite loop?
When you want to test things that happened asynchronously, one solution is to give them some time to happen as you did. A bit more practical approach is to check if the assert condition is met in a loop and sleep a bit between the checks. if the condition is met no need to continue waiting. eventually, it needs to stop after a few tries or you have an infinite loop in your test.

Usually, you need to use this approach in E2E / Integration tests and not unit tests.