|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DeriveAnyClass #-} |
| 3 | +{-# LANGUAGE DeriveGeneric #-} |
| 4 | +{-# LANGUAGE FlexibleInstances #-} |
| 5 | +{-# LANGUAGE KindSignatures #-} |
| 6 | +{-# LANGUAGE LambdaCase #-} |
| 7 | +{-# LANGUAGE PolyKinds #-} |
| 8 | +{-# LANGUAGE StandaloneDeriving #-} |
| 9 | + |
| 10 | +module NodeIPCSMSpec |
| 11 | + ( mkEnv |
| 12 | + , prop_echoOK |
| 13 | + , prop_echoParallelOK |
| 14 | + ) where |
| 15 | + |
| 16 | +import Cardano.Prelude |
| 17 | + |
| 18 | +import Control.Concurrent.STM (TVar, newTVarIO, readTVar, writeTVar) |
| 19 | + |
| 20 | +import Data.Kind (Type) |
| 21 | +import Data.TreeDiff (ToExpr (..)) |
| 22 | + |
| 23 | +import GHC.Generics (Generic, Generic1) |
| 24 | +import Prelude |
| 25 | +import Test.QuickCheck (Gen, Property, arbitrary, oneof, (===)) |
| 26 | +import Test.QuickCheck.Monadic (monadicIO) |
| 27 | + |
| 28 | +import Test.StateMachine |
| 29 | +import Test.StateMachine.Types |
| 30 | +import qualified Test.StateMachine.Types.Rank2 as Rank2 |
| 31 | + |
| 32 | +------------------------------------------------------------------------ |
| 33 | + |
| 34 | +-- | Echo API. |
| 35 | + |
| 36 | +data Env = Env |
| 37 | + { _buf :: TVar (Maybe String) } |
| 38 | + |
| 39 | +-- | Create a new environment. |
| 40 | +mkEnv :: IO Env |
| 41 | +mkEnv = Env <$> newTVarIO Nothing |
| 42 | + |
| 43 | +-- | Input a string. Returns 'True' iff the buffer was empty and the given |
| 44 | +-- string was added to it. |
| 45 | +input :: Env -> String -> IO Bool |
| 46 | +input (Env mBuf) str = atomically $ do |
| 47 | + res <- readTVar mBuf |
| 48 | + case res of |
| 49 | + Nothing -> writeTVar mBuf (Just str) >> return True |
| 50 | + Just _ -> return False |
| 51 | + |
| 52 | +-- | Output the buffer contents. |
| 53 | +output :: Env -> IO (Maybe String) |
| 54 | +output (Env mBuf) = atomically $ do |
| 55 | + res <- readTVar mBuf |
| 56 | + writeTVar mBuf Nothing |
| 57 | + return res |
| 58 | + |
| 59 | +------------------------------------------------------------------------ |
| 60 | + |
| 61 | +-- | Spec for echo. |
| 62 | + |
| 63 | +prop_echoOK :: Property |
| 64 | +prop_echoOK = forAllCommands smUnused Nothing $ \cmds -> monadicIO $ do |
| 65 | + env <- liftIO $ mkEnv |
| 66 | + let echoSM' = echoSM env |
| 67 | + (hist, _, res) <- runCommands echoSM' cmds |
| 68 | + prettyCommands echoSM' hist (res === Ok) |
| 69 | + |
| 70 | +prop_echoParallelOK :: Bool -> Property |
| 71 | +prop_echoParallelOK problem = forAllParallelCommands smUnused $ \cmds -> monadicIO $ do |
| 72 | + env <- liftIO $ mkEnv |
| 73 | + let echoSM' = echoSM env |
| 74 | + let n | problem = 2 |
| 75 | + | otherwise = 1 |
| 76 | + prettyParallelCommands cmds =<< runParallelCommandsNTimes n echoSM' cmds |
| 77 | + |
| 78 | +smUnused :: StateMachine Model Action IO Response |
| 79 | +smUnused = echoSM $ error "used env during command generation" |
| 80 | + |
| 81 | +echoSM :: Env -> StateMachine Model Action IO Response |
| 82 | +echoSM env = StateMachine |
| 83 | + { initModel = Empty |
| 84 | + -- ^ At the beginning of time nothing was received. |
| 85 | + , transition = mTransitions |
| 86 | + , precondition = mPreconditions |
| 87 | + , postcondition = mPostconditions |
| 88 | + , generator = mGenerator |
| 89 | + , invariant = Nothing |
| 90 | + , shrinker = mShrinker |
| 91 | + , semantics = mSemantics |
| 92 | + , mock = mMock |
| 93 | + , distribution = Nothing |
| 94 | + } |
| 95 | + where |
| 96 | + mTransitions :: Model r -> Action r -> Response r -> Model r |
| 97 | + mTransitions Empty (In str) _ = Buf str |
| 98 | + mTransitions (Buf _) Echo _ = Empty |
| 99 | + mTransitions Empty Echo _ = Empty |
| 100 | + -- TODO: qcsm will match the case below. However we don't expect this to happen! |
| 101 | + mTransitions (Buf str) (In _) _ = Buf str -- Dummy response |
| 102 | + -- error "This shouldn't happen: input transition with full buffer" |
| 103 | + |
| 104 | + -- | There are no preconditions for this model. |
| 105 | + mPreconditions :: Model Symbolic -> Action Symbolic -> Logic |
| 106 | + mPreconditions _ _ = Top |
| 107 | + |
| 108 | + -- | Post conditions for the system. |
| 109 | + mPostconditions :: Model Concrete -> Action Concrete -> Response Concrete -> Logic |
| 110 | + mPostconditions Empty (In _) InAck = Top |
| 111 | + mPostconditions (Buf _) (In _) ErrFull = Top |
| 112 | + mPostconditions _ (In _) _ = Bot |
| 113 | + mPostconditions Empty Echo ErrEmpty = Top |
| 114 | + mPostconditions Empty Echo _ = Bot |
| 115 | + mPostconditions (Buf str) Echo (Out out) = str .== out |
| 116 | + mPostconditions (Buf _) Echo _ = Bot |
| 117 | + |
| 118 | + -- | Generator for symbolic actions. |
| 119 | + mGenerator :: Model Symbolic -> Maybe (Gen (Action Symbolic)) |
| 120 | + mGenerator _ = Just $ oneof |
| 121 | + [ In <$> arbitrary |
| 122 | + , return Echo |
| 123 | + ] |
| 124 | + |
| 125 | + -- | Trivial shrinker. |
| 126 | + mShrinker :: Model Symbolic -> Action Symbolic -> [Action Symbolic] |
| 127 | + mShrinker _ _ = [] |
| 128 | + |
| 129 | + -- | Here we'd do the dispatch to the actual SUT. |
| 130 | + mSemantics :: Action Concrete -> IO (Response Concrete) |
| 131 | + mSemantics (In str) = do |
| 132 | + success <- input env str |
| 133 | + return $ if success |
| 134 | + then InAck |
| 135 | + else ErrFull |
| 136 | + mSemantics Echo = maybe ErrEmpty Out <$> output env |
| 137 | + |
| 138 | + -- | What is the mock for? |
| 139 | + mMock :: Model Symbolic -> Action Symbolic -> GenSym (Response Symbolic) |
| 140 | + mMock Empty (In _) = return InAck |
| 141 | + mMock (Buf _) (In _) = return ErrFull |
| 142 | + mMock Empty Echo = return ErrEmpty |
| 143 | + mMock (Buf str) Echo = return (Out str) |
| 144 | + |
| 145 | +deriving instance ToExpr (Model Concrete) |
| 146 | + |
| 147 | +-- | The model contains the last string that was communicated in an input |
| 148 | +-- action. |
| 149 | +data Model (r :: Type -> Type) |
| 150 | + = -- | The model hasn't been initialized. |
| 151 | + Empty |
| 152 | + | -- | Last input string (a buffer with size one). |
| 153 | + Buf String |
| 154 | + deriving (Eq, Show, Generic) |
| 155 | + |
| 156 | +-- | Actions supported by the system. |
| 157 | +data Action (r :: Type -> Type) |
| 158 | + = -- | Input a string, which should be echoed later. |
| 159 | + In String |
| 160 | + -- | Request a string output. |
| 161 | + | Echo |
| 162 | + deriving (Show, Generic1, Rank2.Foldable, Rank2.Traversable, Rank2.Functor, CommandNames) |
| 163 | + |
| 164 | +-- | The system gives a single type of output response, containing a string |
| 165 | +-- with the input previously received. |
| 166 | +data Response (r :: Type -> Type) |
| 167 | + = -- | Input acknowledgment. |
| 168 | + InAck |
| 169 | + -- | The previous action wasn't an input, so there is no input to echo. |
| 170 | + -- This is: the buffer is empty. |
| 171 | + | ErrEmpty |
| 172 | + -- | There is already a string in the buffer. |
| 173 | + | ErrFull |
| 174 | + -- | Output string. |
| 175 | + | Out String |
| 176 | + deriving (Show, Generic1, Rank2.Foldable, Rank2.Traversable, Rank2.Functor) |
0 commit comments