typelevel / cats-effect

The pure asynchronous runtime for Scala
https://typelevel.org/cats-effect/
Apache License 2.0
2.03k stars 520 forks source link

Idea: a hostile thread scheduler to look for bugs #1386

Closed johnynek closed 1 year ago

johnynek commented 3 years ago

a friend described: https://microsoft.github.io/coyote/

as "property based testing meets jepsen". It occurred to me that it would be cool to have an ContextShift, or ExecutionContext, or whatever, that uses randomization to try to see if properties are ever violated.

So, you would write some law about some IO code, then run it many times with this scheduler that isn't going for throughput, but is instead trying to trigger all the different orderings your code could have.

That could be really interesting for helping people find bugs in their async code.

SystemFw commented 3 years ago

I often dream of something along these lines, but specialised to the Java Memory Model (in between a model checker and a simulator)

djspiewak commented 3 years ago

I've thought about stuff like this. :-) There are several different ways it can be done. Coop provides one answer (you can actually layer ThreadT on top of IO and get pure concurrency with controllable yielding). Another answer is TestContext.

There are two problems to solve here:

I really really like the idea, but there's some research to be done to get to a working solution. :-)

johnynek commented 3 years ago

It would be interesting to have a TestContext that is deterministic based on some seed. Then make a Gen[TestContext] and finally just have normal properties you are checking.

When you find a failing case, you use scalacheck's ability to print the seed to rerun the test.

djspiewak commented 3 years ago

Working in terms of seeds seems logical, but again how do you communicate what went wrong? Being able to trace through the exact combination of things that exhibited the race is really important. Minimization is an important and related element here.