Test timeout doesn't work for hanged random number generator

80 Views Asked by At

I wrote a test to check if RNG is hanging

public class StringUtilsTest {
    @Test
    @Timeout(value = 10)
    public void repeated_generateRandomStringOfDigits() {

        for (int i=0; i<5; ++i) {
            StringUtils.generateRandomStringOfDigits(10);

            try {
                Thread.sleep(1);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
            }
        }

    }
}

but it doesn't work as expected: if RNG hangs the test hangs too, ignoring the timeout.

How to accomplish?

1

There are 1 best solutions below

1
Shadi Jumaa On

Use this instead of the @Timeout :

@Rule
public Timeout timout = Timeout.millis(10000); // in milliseconds