I have some code that produces an infinite loop. Now I need to write a test that will fail after about 200ms. 200ms will indicate that the code is in the infinite loop.
For example:
public void CodeUnderTest() { while(true) { } }
You can press Ctrl + C .
In multi-threaded programs some threads can be executing inside infinite loops without causing the entire program to be stuck in an infinite loop. If the main thread exits all threads of the process are forcefully stopped thus all execution ends and the process/program terminates.
An infinite loop error describes a technical glitch that forces your computer to repeat the same actions over and over again. For example, you just restarted your device after you installed the latest OS version. But instead of booting up, your computer keeps on restarting.
What is an Infinite Loop? An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0.
When using MSTest you can use an attribute
[TestMethod] [Timeout(200)]
See How to force tests to stop running
You can set time limits with which the execution of a test or a test run will comply. You might need to do this, for example, if you work in a test lab and need a test run to complete by a certain time of day.
Another scenario for the use of time limits is that of non-responsive code.
what about:
Task.Create(CodeUnderTest).Wait(TimeSpan.FromSeconds(1));
or:
Task.Factory.StartNew(CodeUndertest).Wait(TimeSpan.FromSeconds(1));
or:
Task.Factory.StartNew(() => CodeUndertest(arg1,arg2,arg3...)).Wait(TimeSpan.FromSeconds(1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With