dejafu: A library for unit-testing concurrent programs.

[ concurrency, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

[Déjà Fu is] A martial art in which the user's limbs move in time as well as space, […] It is best described as "the feeling that you have been kicked in the head this way before" -- Terry Pratchett, Thief of Time

This package builds on the concurrency package by enabling you to deterministically test your concurrent programs.

See the website or README for more.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.3.1.0, 0.3.1.1, 0.3.2.0, 0.3.2.1, 0.4.0.0, 0.5.0.0, 0.5.0.2, 0.5.1.0, 0.5.1.1, 0.5.1.2, 0.5.1.3, 0.6.0.0, 0.7.0.0, 0.7.0.1, 0.7.0.2, 0.7.1.0, 0.7.1.1, 0.7.1.2, 0.7.1.3, 0.7.2.0, 0.7.3.0, 0.8.0.0, 0.9.0.0, 0.9.0.1, 0.9.0.2, 0.9.0.3, 0.9.1.0, 0.9.1.1, 0.9.1.2, 1.0.0.0, 1.0.0.1, 1.0.0.2, 1.1.0.0, 1.1.0.1, 1.1.0.2, 1.2.0.0, 1.3.0.0, 1.3.0.1, 1.3.0.2, 1.3.0.3, 1.3.1.0, 1.3.2.0, 1.4.0.0, 1.5.0.0, 1.5.1.0, 1.6.0.0, 1.7.0.0, 1.8.0.0, 1.9.0.0, 1.9.1.0, 1.10.0.0, 1.10.1.0, 1.11.0.0, 1.11.0.1, 1.11.0.2, 1.11.0.3, 1.11.0.4, 1.11.0.5, 1.12.0.0, 2.0.0.0, 2.0.0.1, 2.1.0.0, 2.1.0.1, 2.1.0.2, 2.1.0.3, 2.2.0.0, 2.3.0.0, 2.3.0.1, 2.4.0.0, 2.4.0.1, 2.4.0.2, 2.4.0.3, 2.4.0.4, 2.4.0.5 (info)
Dependencies base (>=4.9 && <5), concurrency (>=1.11 && <1.12), containers (>=0.5 && <0.7), contravariant (>=1.2 && <1.6), deepseq (>=1.1 && <2), exceptions (>=0.7 && <0.11), leancheck (>=0.6 && <2), profunctors (>=4.0 && <6), random (>=1.0 && <1.3), transformers (>=0.5 && <0.7) [details]
License MIT
Copyright (c) 2015--2020 Michael Walker
Author Michael Walker
Maintainer mike@barrucadu.co.uk
Category Concurrency
Home page https://github.com/barrucadu/dejafu
Source repo head: git clone https://github.com/barrucadu/dejafu.git
this: git clone https://github.com/barrucadu/dejafu.git(tag dejafu-2.4.0.5)
Uploaded by barrucadu at 2023-06-17T22:28:17Z
Distributions LTSHaskell:2.4.0.5, NixOS:2.4.0.5, Stackage:2.4.0.5
Reverse Dependencies 7 direct, 26 indirect [details]
Downloads 45829 total (261 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-06-17 [all 1 reports]

Readme for dejafu-2.4.0.5

[back to package description]

dejafu

[Déjà Fu is] A martial art in which the user's limbs move in time as well as space, […] It is best described as "the feeling that you have been kicked in the head this way before"

-- Terry Pratchett, Thief of Time

Déjà Fu is a unit-testing library for concurrent Haskell programs. Tests are deterministic and expressive, making it easy and convenient to test your threaded code. Available on GitHub, Hackage, and Stackage.

Installation

Install from Hackage globally:

$ cabal install dejafu

Or add it to your cabal file:

build-depends: ...
             , dejafu

Or to your package.yaml:

dependencies:
  ...
  - dejafu

Quick start guide

Déjà Fu supports unit testing, and comes with a helper function called autocheck to look for some common issues. Let's see it in action:

import Control.Concurrent.Classy

myFunction :: MonadConc m => m String
myFunction = do
  var <- newEmptyMVar
  fork (putMVar var "hello")
  fork (putMVar var "world")
  readMVar var

That MonadConc is a typeclass abstraction over concurrency, but we'll get onto that shortly. First, the result of testing:

> autocheck myFunction
[pass] Never Deadlocks
[pass] No Exceptions
[fail] Consistent Result
        "hello" S0----S1--S0--

        "world" S0----S2--S0--
False

There are no deadlocks or uncaught exceptions, which is good; but the program is (as you probably spotted) nondeterministic!

Along with each result, Déjà Fu gives us a representative execution trace in an abbreviated form. Sn means that thread n started executing, and Pn means that thread n pre-empted the previously running thread.

Why Déjà Fu?

Testing concurrent programs is difficult, because in general they are nondeterministic. This leads to people using work-arounds like running their testsuite many thousands of times; or running their testsuite while putting their machine under heavy load.

These approaches are inadequate for a few reasons:

  • How many runs is enough? When you are just hopping to spot a bug by coincidence, how do you know to stop?
  • How do you know if you've fixed a bug you saw previously? Because the scheduler is a black box, you don't know if the previously buggy schedule has been re-run.
  • You won't get that much scheduling variety! Operating systems and language runtimes like to run threads for long periods of time, which reduces the variety you get (and so drives up the number of runs you need).

Déjà Fu addresses these points by offering complete testing. You can run a test case and be guaranteed to find all results with some bounds. These bounds can be configured, or even disabled! The underlying approach used is smarter than merely trying all possible executions, and will in general explore the state-space quickly.

If your test case is just too big for complete testing, there is also a random scheduling mode, which is necessarily incomplete. However, Déjà Fu will tend to produce much more schedule variety than just running your test case in IO the same number of times, and so bugs will tend to crop up sooner. Furthermore, as you get execution traces out, you can be certain that a bug has been fixed by simply following the trace by eye.

Contributing

Bug reports, pull requests, and comments are very welcome!

Feel free to contact me on GitHub, through IRC (#haskell on libera.chat), or email (mike@barrucadu.co.uk).