2

("controlled" as in you have control over the order in which code executes, rather than the underlying scheduler(s))

I was asked in an interview earlier "How would you test for race conditions?" and was honestly pretty stumped.

Since then all I've been able to figure out is that load testing/fuzzing can be helpful for sussing them out, and maybe it doesn't hurt to increase your code's observability with logging so you can debug them faster once you identify them.

What I haven't been able to figure out is how I could take away control from the underlying scheduler(s) orchestrating what code runs when, and generically turn that into something I could control easily so I could try out explicit permutations of code flow on my own. The closest I've thought of is if I'm in a situation where I'm able to attach a debugger to each of the threads, processes, etc... involved and I could step line-by-line in each, I could test the permutations I want to try (not great, but doable, and I realize now this is actually what I've tried to do in the past where possible).

My googlefu is failing me on this one though, so any direction on where to find more information around this would be super helpful!

Thanks

1

Of course, the exact solution will depend on your context (tech stack, test goal, etc), but generally speaking, you would work with polymorphism:

Putting the code that controls the threads behind an interface, then you can inject a fake thread manager you know how it will behave.

This presentation by Dror Helper show some patterns in this. And this article may help as well.

This video by Dave Farley talks about controlling the system time, but it may also help for concurrency situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.