quickcheck-state-machine-0.9.0: Test monadic programs using state machine based models
Copyright(C) 2017 ATS Advanced Telematic Systems GmbH
LicenseBSD-style (see the file LICENSE)
MaintainerStevan Andjelkovic <stevan.andjelkovic@strath.ac.uk>
Stabilityprovisional
Portabilitynon-portable (GHC extensions)
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.StateMachine.Parallel

Description

This module contains helpers for generating, shrinking, and checking parallel programs.

Note that:

  • the .*NParallelCommands functions and the NParallelCommands datatype are expected to be used when you want to run N concurrent threads running at the same time and repeat each test 10 (by default) times to get sufficient randomness on the RTS scheduling of concurrent actions.
  • the .*ParallelCommandsNTimes functions and the ParallelCommandsF datatype are expected to be used when you want to run the test with 2 concurrent threads and repeat each test N times to get sufficient randomness on the RTS scheduling of concurrent actions.
  • the .*NParallelCommandsNTime functions do both of the above points at the same time, in particular they take an NParallelCommands value generated to be run by some number of threads concurrently, and they take as input how many times each test should be repeated.
  • the run.*WithSetup functions receive a monadic action that must initialize the StateMachine which will be used at the beginning of each repetition of each sequence of commands. See the section in the README for more context.
Synopsis

Documentation

forAllNParallelCommands Source #

Arguments

:: Testable prop 
=> (Show (cmd Symbolic), Show (resp Symbolic), Show (model Symbolic)) 
=> (Traversable cmd, Foldable resp) 
=> StateMachine model cmd m resp 
-> Int

Number of threads

-> (NParallelCommands cmd resp -> prop)

Predicate.

-> Property 

forAllParallelCommands Source #

Arguments

:: Testable prop 
=> (Show (cmd Symbolic), Show (resp Symbolic), Show (model Symbolic)) 
=> (Traversable cmd, Foldable resp) 
=> StateMachine model cmd m resp 
-> Maybe Int 
-> (ParallelCommands cmd resp -> prop)

Predicate.

-> Property 

generateNParallelCommands :: forall model cmd m resp. Foldable resp => Show (model Symbolic) => (Show (cmd Symbolic), Show (resp Symbolic)) => StateMachine model cmd m resp -> Int -> Gen (NParallelCommands cmd resp) Source #

generateParallelCommands :: forall model cmd m resp. Foldable resp => Show (model Symbolic) => (Show (cmd Symbolic), Show (resp Symbolic)) => StateMachine model cmd m resp -> Maybe Int -> Gen (ParallelCommands cmd resp) Source #

Generate parallel commands.

Parallel commands are generated as follows. We begin by generating sequential commands and then splitting this list in two at some index. The first half will be used as the prefix.

The second half will be used to build suffixes. For example, starting from the following sequential commands:

[A, B, C, D, E, F, G, H, I]

We split it in two, giving us the prefix and the rest:

prefix: [A, B]
rest:   [C, D, E, F, G, H, I]

We advance the model with the prefix.

Make a suffix: we take commands from rest as long as these are parallel safe (see parallelSafe). This means that the pre-conditions (using the 'advanced' model) of each of those commands will hold no matter in which order they are executed.

Say this is true for [C, D, E], but not anymore for F, maybe because F depends on one of [C, D, E]. Then we divide this 'chunk' in two by splitting it in the middle, obtaining [C] and [D, E]. These two halves of the chunk (stored as a Pair) will later be executed in parallel. Together they form one suffix.

Then the model is advanced using the whole chunk [C, D, E]. Think of it as a barrier after executing the two halves of the chunk in parallel. Then this process of building a chunk/suffix repeats itself, starting from Make a suffix using the 'advanced' model.

In the end we might end up with something like this:

        ┌─ [C] ──┐  ┌ [F, G] ┐
[A, B] ─┤        ├──┤        │
        └ [D, E] ┘  └ [H, I] ┘

shrinkNParallelCommands :: forall cmd model m resp. Traversable cmd => Foldable resp => StateMachine model cmd m resp -> NParallelCommands cmd resp -> [NParallelCommands cmd resp] Source #

Shrink a parallel program in a pre-condition and scope respecting way.

shrinkParallelCommands :: forall cmd model m resp. Traversable cmd => Foldable resp => StateMachine model cmd m resp -> ParallelCommands cmd resp -> [ParallelCommands cmd resp] Source #

Shrink a parallel program in a pre-condition and scope respecting way.

shrinkAndValidateNParallel :: forall model cmd m resp. (Traversable cmd, Foldable resp) => StateMachine model cmd m resp -> ShouldShrink -> NParallelCommands cmd resp -> [NParallelCommands cmd resp] Source #

shrinkAndValidateParallel :: forall model cmd m resp. (Traversable cmd, Foldable resp) => StateMachine model cmd m resp -> ShouldShrink -> ParallelCommands cmd resp -> [ParallelCommands cmd resp] Source #

shrinkCommands' :: Commands cmd resp -> [Shrunk (Commands cmd resp)] Source #

Shrinks Commands in a way that it has strictly less number of commands.

runNParallelCommands :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => StateMachine model cmd m resp -> NParallelCommands cmd resp -> PropertyM m [(History cmd resp, model Concrete, Logic)] Source #

runNParallelCommandsWithSetup :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => m (StateMachine model cmd m resp) -> NParallelCommands cmd resp -> PropertyM m [(History cmd resp, model Concrete, Logic)] Source #

runParallelCommands :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => StateMachine model cmd m resp -> ParallelCommands cmd resp -> PropertyM m [(History cmd resp, model Concrete, Logic)] Source #

runParallelCommandsWithSetup :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => m (StateMachine model cmd m resp) -> ParallelCommands cmd resp -> PropertyM m [(History cmd resp, model Concrete, Logic)] Source #

runParallelCommands' :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => m (StateMachine model cmd m resp) -> (cmd Concrete -> resp Concrete) -> ParallelCommands cmd resp -> PropertyM m [(History cmd resp, model Concrete, Logic)] Source #

runNParallelCommandsNTimes Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> StateMachine model cmd m resp 
-> NParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

runNParallelCommandsNTimesWithSetup Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> m (StateMachine model cmd m resp) 
-> NParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

runParallelCommandsNTimes Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> StateMachine model cmd m resp 
-> ParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

runParallelCommandsNTimesWithSetup Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> m (StateMachine model cmd m resp) 
-> ParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

runNParallelCommandsNTimes' Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> m (StateMachine model cmd m resp) 
-> (cmd Concrete -> resp Concrete) 
-> NParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

runParallelCommandsNTimes' Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> (Traversable cmd, Foldable resp) 
=> (MonadMask m, MonadUnliftIO m) 
=> Int

How many times to execute the parallel program.

-> m (StateMachine model cmd m resp) 
-> (cmd Concrete -> resp Concrete) 
-> ParallelCommands cmd resp 
-> PropertyM m [(History cmd resp, model Concrete, Logic)] 

executeParallelCommands :: (Show (cmd Concrete), Show (resp Concrete)) => (Traversable cmd, Foldable resp) => (MonadMask m, MonadUnliftIO m) => StateMachine model cmd m resp -> ParallelCommands cmd resp -> TChan (Pid, HistoryEvent cmd resp) -> Bool -> m (History cmd resp, model Concrete, Reason, Reason) Source #

linearise :: forall model cmd m resp. (Show (cmd Concrete), Show (resp Concrete)) => StateMachine model cmd m resp -> History cmd resp -> Logic Source #

Try to linearise a history of a parallel program execution using a sequential model. See the *Linearizability: a correctness condition for concurrent objects* paper linked to from the README for more info.

toBoxDrawings :: forall cmd resp. Foldable cmd => (Show (cmd Concrete), Show (resp Concrete)) => ParallelCommands cmd resp -> History cmd resp -> Doc Source #

Draw an ASCII diagram of the history of a parallel program. Useful for seeing how a race condition might have occured.

prettyNParallelCommands Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> MonadIO m 
=> Foldable cmd 
=> NParallelCommands cmd resp 
-> [(History cmd resp, a, Logic)]

Output of runNParallelCommands.

-> PropertyM m () 

prettyParallelCommands Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> MonadIO m 
=> Foldable cmd 
=> ParallelCommands cmd resp 
-> [(History cmd resp, a, Logic)]

Output of runNParallelCommands.

-> PropertyM m () 

prettyParallelCommandsWithOpts Source #

Arguments

:: (MonadIO m, Foldable cmd) 
=> (Show (cmd Concrete), Show (resp Concrete)) 
=> ParallelCommands cmd resp 
-> Maybe GraphOptions 
-> [(History cmd resp, a, Logic)]

Output of runParallelCommands.

-> PropertyM m () 

Takes the output of parallel program runs and pretty prints a counterexample if any of the runs fail.

prettyNParallelCommandsWithOpts Source #

Arguments

:: (Show (cmd Concrete), Show (resp Concrete)) 
=> MonadIO m 
=> Foldable cmd 
=> NParallelCommands cmd resp 
-> Maybe GraphOptions 
-> [(History cmd resp, a, Logic)]

Output of runNParallelCommands.

-> PropertyM m () 

Takes the output of parallel program runs and pretty prints a counterexample if any of the runs fail.

advanceModel Source #

Arguments

:: StateMachine model cmd m resp 
-> model Symbolic

The model.

-> Commands cmd resp

The commands.

-> model Symbolic 

Apply the transition of some commands to a model.

checkCommandNamesParallel :: forall cmd resp t. Foldable t => CommandNames cmd => ParallelCommandsF t cmd resp -> Property -> Property Source #

Print the percentage of each command used. The prefix check is an unfortunate remaining for backwards compatibility.

coverCommandNamesParallel :: forall cmd resp t. Foldable t => CommandNames cmd => ParallelCommandsF t cmd resp -> Property -> Property Source #

Fail if some commands have not been executed.

commandNamesParallel :: forall cmd resp t. Foldable t => CommandNames cmd => ParallelCommandsF t cmd resp -> [(String, Int)] Source #