Skip to content

Best-practice for guarding against infinite-looping arbitraries? #4659

Answered by dubzzz
amccarthy1 asked this question in Q&A
Discussion options

You must be logged in to vote

At the moment, the only built-in thing that can protect you against filtering leading to an infinite loop is fc.pre.

For now, I think the only trick that could make it for filter would be to do something like:

function buildStoppeable(filter) {
  let consecutiveErrors = 0;
  return function stoppeableFilter(...args) {
    const out = filter(...args);
    if (out) {
      consecutiveErrors = 0;
      return out;
    }
    if (++consecutiveErrors > 100) throw new Error('To many errors!');
    return out;
  }
}

it('Should interrupt the test run after a short time', async () => {
  fc.assert(
    fc.property(
      fc.nat().filter(buildStoppeable(() => false)),
      (_value) => {}
    ),
    {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dubzzz
Comment options

Answer selected by amccarthy1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants