question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model Based Testing - Restore State on Shrinking

See original GitHub issue

💬 Question and Help

First time with a question here 😄 I would like to thank you for the library, it is a great tool that have help a lot. I have search though the docs and other issues, but I couldn’t find exactly what I’m looking for. So here is my question.

Is there a way to restore state before every shrink?

Or said in a different way, Is there a way to execute a command/function before each shrink?

When doing Model Based Testing for me it’s fundamental to restore the state of the system after every test. This is mandatory when there is an state mutation and shared resources. There is an easy way to do it:

fc.asyncProperty(
  fc.commands(commands),
  async (cmds) => {
    await restoreState(); // For example, cleaning a db
    const s = () => ({ model: { ... }, real: new DbClient() });
    fc.asyncModelRun(s, cmds);
  }
);

But when an error is found, I didn’t found a way to restore the state between every shrink. I have to enable endOnFailure, which produces non minimal counterexamples.

I think that this library is powerful enough to find very useful counterexamples in these cases. Is it possible to restore the state before each shrink?

Please, if you want me to elaborate more on the question or provide more examples, I’ll include them.

It there is no solution or alternative, I’ll happy trying to implement this if considered.

Others workaraunds that I tried

I tried to modify the command generator to always include an sepecific first command in the sequence, but couldn’t apply .map(...) to the generator in order to append that command.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vvpcommented, Jul 16, 2021

Fwiw, the initial state provider is called on each shrinking run and it can be async too, so we used something like this to restore a snapshot state:

asyncProperty(commands(...), async (cmds) => {
  const initialState = async () => {
    let stateSnapshot = await restoreState(...)
    console.log("Restored state snapshot")
    return {
      model: await initializeModel(stateSnapshot),
      real: stateSnapshot,
    }
  }
  await asyncModelRun(initialState, cmds)
})
1reaction
ignaciobllcommented, Aug 21, 2020

I think it’s probably because when looking for the methods of the AsyncProperty I ended up on the signature page or looking at IAsyncProperty. I didn’t notice IAsyncPropertyWithHooks.

I think it could be really useful a little section on the tips. Maybe a name like Restore Initial State with Model Based Testing could be enough. A little snippet like this could be helpful:

fc.asyncProperty(
  fc.commands(commands),
  async (cmds) => {
    fc.asyncModelRun(() => ({ model: { ... }, real: new Client() }), cmds);
  }
).beforeEach(async () => await restoreInitialState());

Oh! And one of my main doubts. I couldn’t found explicitly said that this hooks are called also when shrinking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[PDF] Model-Based Shrinking for State-Based Testing | Semantic ...
This paper shows how the model can be used to systematically search for shorter traces producing an issue based on some trace revealing...
Read more >
Model-Based Shrinking for State Based Testing
Abstract. Issues found in model-based testing of state-based systems are traces produced by the system under test, sut, that are not allowed by....
Read more >
Model-Based Shrinking for State-Based Testing - Volume 8322
Issues found in model-based testing of state-based systems are traces produced by the system under test that are not allowed by the model ......
Read more >
Shrinking Choices, Shrinking Values - Property-based Testing ...
Explaining how property-based testing libraries like hypothesis shrink random values to make them easier to understand and debug.
Read more >
Reliable Systems Series: Model-Based Testing | by Tyler Neely
QuickCheck is only used here as a library for generating a sequence of random inputs and then shrinking the failing sequence to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found