How is the Unit Test an endlessly awaiting listener?

I am using MSTest, Vs2010. I have a situation where a method with a while loop calls AutoResetEvent.WaitOne . I can trigger this event from my testing method; it will be repeated once, and then wait again when this event is fired.

I can not say anything in this situation. How can I use Unit Test to use these methods?

thanks

+4
source share
1 answer

I would suggest using Task in your unit test to initialize a loop in a separate thread.

 public void TestMyLoop() { var myLooper = new Looper(); Task t = Task.Run(() => myLooper.BeginWorking()); // BeginWorking is an infinite loop, it will never end! myLooper.AddAnItemToProcess(new Item()); Thread.Sleep(5000); // wait 5 seconds, alternatively hook into and `await` some completion event. // assert here Assert.That(myLooper.processedItems == 1); } 
+3
source

Source: https://habr.com/ru/post/1492131/


All Articles