I have a method that looks like this:
protected void OnBarcodeScan(BarcodeScannerEventArgs e)
{
ThreadStart starter = () => SendScanMessage(e, _scanDelegates);
Thread scanThread = new Thread(starter);
scanThread.Start();
}
Then the thread shuts down and executes some logic (and ends the delegate call in my test).
My problem is that my unit test ends before the thread executes. Therefore, my test fails.
I can just add in System.Threading.Thread.Sleep(1000);and hope that the logic does not take more than a second (this should not). But this seems like a hack.
The problem is that I do not want to reveal this stream to the outside world or even to the rest of the classes.
Is there any cool way to find this thread again and wait for it in unit test?
Something like that:
[TestMethod]
[HostType("Moles")]
public void AddDelegateToScanner_ScanHappens_ScanDelegateIsCalled()
{
bool scanCalled = false;
MCoreDLL.GetTopWindow = () => (new IntPtr(FauxHandle));
_scanner.AddDelegateToScanner(_formIdentity, ((evnt) => { scanCalled = true; }));
_scanner.SendScan(new BarcodeScannerEventArgs("12345678910"));
System.Threading.Thread.CoolMethodToFindMyThread().Join();
Assert.IsTrue(scanCalled);
}
I obviously compiled the CoolMethodToFindMyThread method . But are there some reasons for this?