Debian Codegraph @

test/Data/Acid/KeyValueStateMachine.hs

raw
1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE StandaloneDeriving #-}
3 {-# LANGUAGE TemplateHaskell #-}
4 {-# LANGUAGE TypeFamilies #-}
5
6 -- | This module instantiates the general framework in
7 -- 'Data.Acid.StateMachineTest' with an acid-state component that
8 -- implements a simple key-value store.
9 module Data.Acid.KeyValueStateMachine (tests) where
10
11 import Control.DeepSeq
12 import Control.Exception
13 import Control.Monad.IO.Class ( MonadIO(..) )
14 import Control.Monad.Reader ( ask )
15 import Control.Monad.State ( get, put )
16 import Data.Acid
17 import Data.Acid.StateMachineTest
18 import Data.SafeCopy
19 import qualified Data.Map as Map
20 import GHC.Generics
21 import Hedgehog
22 import qualified Hedgehog.Gen as Gen
23 import qualified Hedgehog.Range as Range
24
25
26 type Key = Int
27 type Value = String
28
29 data KeyValue = KeyValue !(Map.Map Key Value)
30 deriving (Eq, Show)
31
32 $(deriveSafeCopy 0 'base ''KeyValue)
33
34 -- | Insert a key into the key-value store.
35 insertKey :: Key -> Value -> Update KeyValue ()
36 insertKey key value
37 = do KeyValue m <- get
38 put (KeyValue (Map.insert key value m))
39
40 -- | A slightly more complicated update transaction: reverse the value
41 -- at the given key, and return the resulting state. Crucially, this
42 -- is non-idempotent, unlike 'insertKey'.
43 reverseKey :: Key -> Update KeyValue KeyValue
44 reverseKey key
45 = do KeyValue m <- get
46 let r = KeyValue (Map.adjust reverse key m)
47 put r
48 return r
49
50 -- | An update that may fail: reverse the value at the given key, or
51 -- fail if it is missing.
52 reverseKeyOrFail :: Key -> Bomb -> Update KeyValue ()
53 reverseKeyOrFail key _
54 = do KeyValue m <- get
55 case Map.lookup key m of
56 Nothing -> failUpdate "key not in map"
57 Just val -> put (KeyValue (Map.insert key (reverse val) m))
58
59 -- | An update that attempts to put an undefined state. This transaction should
60 -- simply fail and not modify the state.
61 breakState :: Update KeyValue ()
62 breakState = put (throw (TransactionError "broken state"))
63
64 -- | An update that puts a partially-defined state. Unfortunately
65 -- acid-state does not handle this case gracefully, and will fail with
66 -- 'BlockedIndefinitelyOnMVar' (see #38). Thus this update is not
67 -- included in 'keyValueCommands' below.
68 breakState2 :: Update KeyValue ()
69 breakState2 = put (KeyValue (Map.singleton 1 (throw (TransactionError "broken state"))))
70
71 -- | Look up a key from the store.
72 lookupKey :: Key -> Query KeyValue (Maybe Value)
73 lookupKey key
74 = do KeyValue m <- ask
75 return (Map.lookup key m)
76
77 -- | Look up a key from the store, or fail if it is missing.
78 lookupKeyOrFail :: Key -> Bomb -> Query KeyValue Value
79 lookupKeyOrFail key _
80 = do KeyValue m <- ask
81 maybe (failQuery "key not in map") return (Map.lookup key m)
82
83 -- | Query the current value of the state. This is not used in the
84 -- generated commands, but is used for checking the state we get back
85 -- in 'prop_restore_old_state_1' etc.
86 askState :: Query KeyValue KeyValue
87 askState = ask
88
89 $(makeAcidic ''KeyValue ['insertKey, 'reverseKey, 'reverseKeyOrFail, 'breakState, 'breakState2, 'lookupKey, 'lookupKeyOrFail, 'askState])
90
91 deriving instance Generic InsertKey
92 deriving instance Generic ReverseKey
93 deriving instance Generic ReverseKeyOrFail
94 deriving instance Generic BreakState
95 deriving instance Generic LookupKey
96 deriving instance Generic LookupKeyOrFail
97
98 deriving instance Show InsertKey
99 deriving instance Show ReverseKey
100 deriving instance Show ReverseKeyOrFail
101 deriving instance Show BreakState
102 deriving instance Show LookupKey
103 deriving instance Show LookupKeyOrFail
104
105 instance NFData InsertKey
106 instance NFData ReverseKey
107 instance NFData ReverseKeyOrFail
108 instance NFData BreakState
109 instance NFData LookupKey
110 instance NFData LookupKeyOrFail
111
112 genKey :: Gen Key
113 genKey = Gen.int (Range.constant 1 10)
114
115 genValue :: Gen Value
116 genValue = Gen.string (Range.constant 0 10) Gen.alphaNum
117
118 keyValueCommands :: MonadIO m => [Command Gen m (Model KeyValue)]
119 keyValueCommands = [ acidUpdate (InsertKey <$> genKey <*> genValue)
120 , acidUpdate (ReverseKey <$> genKey)
121 , acidUpdateMayFail (ReverseKeyOrFail <$> genKey <*> genBomb)
122 , acidUpdateMayFail (pure BreakState)
123 , acidQuery (LookupKey <$> genKey)
124 , acidQueryMayFail (LookupKeyOrFail <$> genKey <*> genBomb)
125 ]
126
127 -- | Possible initial states; because of #20 we can currently only use
128 -- one of these when testing the properties.
129 initialStates :: [KeyValue]
130 initialStates = [ KeyValue Map.empty
131 , KeyValue (Map.singleton 1 "foo")
132 ]
133
134 prop_sequential :: Property
135 prop_sequential = acidStateSequentialProperty (acidStateInterface fp) (pure (head initialStates)) (Range.linear 1 10) keyValueCommands
136 where
137 fp = "state/KeyValueSequentialTest"
138
139 prop_parallel :: Property
140 prop_parallel = acidStateParallelProperty (acidStateInterface fp) (pure (head initialStates)) (Range.linear 1 10) (Range.linear 1 10) keyValueCommands
141 where
142 fp = "state/KeyValueParallelTest"
143
144 prop_restore_old_state_1 :: Property
145 prop_restore_old_state_1 = restoreOldStateProperty (acidStateInterface fp) (KeyValue Map.empty) AskState r
146 where
147 fp = "test-state/OldStateTest1"
148 r = KeyValue (Map.fromList [(1,""),(2,""),(3,"y5Pl"),(4,""),(5,"Zc"),(6,"8aENKK")
149 ,(7,"FDzyGCz"),(8,""),(9,"xq"),(10,"1Ra1obuINa")])
150
151 prop_restore_old_state_2 :: Property
152 prop_restore_old_state_2 = restoreOldStateProperty (acidStateInterface fp) (KeyValue Map.empty) AskState r
153 where
154 fp = "test-state/OldStateTest2"
155 r = KeyValue (Map.fromList [(1,"PLwR1S6F"),(2,"0yrcVQM0c"),(3,"zAA"),(4,"prAocOc")
156 ,(5,"HM"),(6,"ENdfLrrW"),(7,"sESXGsI"),(8,"AFa69uu5")
157 ,(9,"XBvIQHX"),(10,"A2CzkvW")])
158
159 prop_restore_old_state_3 :: Property
160 prop_restore_old_state_3 = restoreOldStateProperty (acidStateInterface fp) (KeyValue Map.empty) AskState r
161 where
162 fp = "test-state/OldStateTest3"
163 r = KeyValue (Map.fromList [(1,"4"),(2,"RQ1xEc5"),(3,"aT0Hqk"),(4,"Duf")
164 ,(5,"tssCng7e0d"),(6,"uQW0hCVze"),(7,"FSCZPMGL")
165 ,(8,"q1WI9He"),(9,"IYHbWmO"),(10,"lCErPJC3")])
166
167
168 tests :: IO Bool
169 tests = checkParallel $$(discover)