linearizable 0.1.0.0 → 0.2.0.0
raw patch · 4 files changed
+65/−19 lines, 4 filesdep +tasty-quickcheckPVP ok
version bump matches the API change (PVP)
Dependencies added: tasty-quickcheck
API changes (from Hackage documentation)
- Control.Concurrent.Linearizable: MkEvent :: timestamp -> timestamp -> operation -> Event timestamp operation
- Control.Concurrent.Linearizable: MkModel :: state -> (operation -> state -> Maybe state) -> Model state operation
- Control.Concurrent.Linearizable: instance (GHC.Classes.Eq state, GHC.Classes.Eq node, GHC.Classes.Eq timestamp, GHC.Classes.Eq operation) => GHC.Classes.Eq (Control.Concurrent.Linearizable.Possibility node state timestamp operation)
- Control.Concurrent.Linearizable: instance (GHC.Classes.Eq timestamp, GHC.Classes.Eq operation) => GHC.Classes.Eq (Control.Concurrent.Linearizable.Event timestamp operation)
- Control.Concurrent.Linearizable: instance (GHC.Classes.Ord state, GHC.Classes.Ord node, GHC.Classes.Ord timestamp, GHC.Classes.Ord operation) => GHC.Classes.Ord (Control.Concurrent.Linearizable.Possibility node state timestamp operation)
- Control.Concurrent.Linearizable: instance (GHC.Classes.Ord timestamp, GHC.Classes.Ord operation) => GHC.Classes.Ord (Control.Concurrent.Linearizable.Event timestamp operation)
- Control.Concurrent.Linearizable: instance (GHC.Show.Show timestamp, GHC.Show.Show operation) => GHC.Show.Show (Control.Concurrent.Linearizable.Event timestamp operation)
+ Control.Concurrent.Linearizable: Event :: timestamp -> timestamp -> operation -> Event timestamp operation
+ Control.Concurrent.Linearizable: Model :: state -> (operation -> state -> Maybe state) -> Model state operation
+ Control.Concurrent.Linearizable: instance (GHC.Internal.Classes.Eq state, GHC.Internal.Classes.Eq node, GHC.Internal.Classes.Eq timestamp, GHC.Internal.Classes.Eq operation) => GHC.Internal.Classes.Eq (Control.Concurrent.Linearizable.Possibility node state timestamp operation)
+ Control.Concurrent.Linearizable: instance (GHC.Internal.Classes.Eq timestamp, GHC.Internal.Classes.Eq operation) => GHC.Internal.Classes.Eq (Control.Concurrent.Linearizable.Event timestamp operation)
+ Control.Concurrent.Linearizable: instance (GHC.Internal.Classes.Ord state, GHC.Internal.Classes.Ord node, GHC.Internal.Classes.Ord timestamp, GHC.Internal.Classes.Ord operation) => GHC.Internal.Classes.Ord (Control.Concurrent.Linearizable.Possibility node state timestamp operation)
+ Control.Concurrent.Linearizable: instance (GHC.Internal.Classes.Ord timestamp, GHC.Internal.Classes.Ord operation) => GHC.Internal.Classes.Ord (Control.Concurrent.Linearizable.Event timestamp operation)
+ Control.Concurrent.Linearizable: instance (GHC.Internal.Show.Show timestamp, GHC.Internal.Show.Show operation) => GHC.Internal.Show.Show (Control.Concurrent.Linearizable.Event timestamp operation)
Files
- CHANGELOG.md +7/−1
- linearizable.cabal +2/−1
- src/Control/Concurrent/Linearizable.hs +6/−6
- tests/Test/Control/Concurrent/Linearizable.hs +50/−11
CHANGELOG.md view
@@ -1,5 +1,11 @@ # Revision history for linearizability -## 0.1.0.0 -- YYYY-mm-dd+## 0.2.0.0++### Breaking changes++* Constructors `Model` and `Event` no longer have a `Mk` prefix.++## 0.1.0.0 * First version. Released on an unsuspecting world.
linearizable.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.4 name: linearizable-version: 0.1.0.0+version: 0.2.0.0 synopsis: Linearizability checker description: Linearizability checker for experimental event histories. This is useful for testing the properties of a distributed system given, for example.@@ -47,4 +47,5 @@ build-depends: base , tasty ^>=1.5 , tasty-hunit ^>=0.10+ , tasty-quickcheck ^>=0.11 , linearizable
src/Control/Concurrent/Linearizable.hs view
@@ -39,7 +39,7 @@ -- parameter tracks both what was the input to the state machine, -- as well as what was the result of the input. data Event timestamp operation- = MkEvent+ = Event { timestampReq :: timestamp, timestampAck :: timestamp, operation :: operation@@ -75,20 +75,20 @@ -- In particular, note how the model returns @'Nothing'@ in case the state is -- inconsistent with the experimental history. data Model state operation- = MkModel+ = Model { initialState :: state, apply :: operation -> state -> Maybe state } data Possibility node state timestamp operation- = MkPossibility+ = Possibility { state :: state, queues :: Map node (Seq (Event timestamp operation)) } deriving (Eq, Ord) data Checker node state timestamp operation- = MkChecker+ = Checker { model :: Model state operation, possibilities :: Set (Possibility node state timestamp operation) }@@ -131,9 +131,9 @@ stillActive = drop 1 (scanr (Set.insert . fst) Set.empty sortedEvents) checker ns =- MkChecker m $+ Checker m $ Set.singleton- MkPossibility+ Possibility { state = initialState m, queues = Map.fromList
tests/Test/Control/Concurrent/Linearizable.hs view
@@ -5,18 +5,25 @@ import Control.Concurrent.Linearizable (Event (..), Model (..), linearizable) import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (assertBool, testCase)+import Test.Tasty.QuickCheck tests :: TestTree tests = testGroup "Control.Concurrent.Linearizable"- [ testEmptyHistoryLinearizable,- testLinearizableExample,- testNonLinearizableExample,- testLinearizableExampleWithFail,- testIdleNode,- testIdleNodeNonLinearizable,- testEarlyFinisher+ [ testGroup+ "Unit tests"+ [ testEmptyHistoryLinearizable,+ testLinearizableExample,+ testNonLinearizableExample,+ testLinearizableExampleWithFail,+ testIdleNode,+ testIdleNodeNonLinearizable,+ testEarlyFinisher+ ],+ testGroup+ "Property tests"+ [testPropValidHistory] ] testEmptyHistoryLinearizable :: TestTree@@ -97,6 +104,12 @@ ] assertBool "History should be linearizable" (linearizable @Int model history) +testPropValidHistory :: TestTree+testPropValidHistory =+ testProperty "Generated valid histories are linearizable" $+ forAll genValidHistory $+ linearizable @Int model+ data State = Unknown | Known (Maybe Int)@@ -104,7 +117,7 @@ model :: Model State Op model =- MkModel+ Model { initialState = Known Nothing, apply = \op s -> case op of Put x -> pure (Known (Just x))@@ -118,16 +131,42 @@ type Timestamp = Int get :: Maybe Int -> Timestamp -> Timestamp -> Event Timestamp Op-get expectation req ack = MkEvent req ack (Get expectation)+get expectation req ack = Event req ack (Get expectation) put :: Int -> Timestamp -> Timestamp -> Event Timestamp Op-put value req ack = MkEvent req ack (Put value)+put value req ack = Event req ack (Put value) failed :: Timestamp -> Timestamp -> Event Timestamp Op-failed req ack = MkEvent req ack Failed+failed req ack = Event req ack Failed data Op = Put Int | Get (Maybe Int) -- expected result | Failed -- value uncertain deriving (Eq, Ord, Show)++-- * Quickcheck Generators++genValidHistory :: Gen [(Int, Event Timestamp Op)]+genValidHistory = do+ n <- chooseInt (0, 20)+ ops <- genValidOps n+ clients <- vectorOf n (chooseInt (0, 5))+ pure $ zipWith3 makeEvent [0 ..] clients ops+ where+ makeEvent i client op = (client, Event i (i + 30) op)++genValidOps :: Int -> Gen [Op]+genValidOps n = go n Nothing+ where+ go 0 _ = pure []+ go k current =+ oneof+ [ do+ x <- chooseInt (0, 10)+ rest <- go (k - 1) (Just x)+ pure (Put x : rest),+ do+ rest <- go (k - 1) current+ pure (Get current : rest)+ ]