linearizable-0.2.0.0: tests/Test/Control/Concurrent/Linearizable.hs
{-# LANGUAGE TypeApplications #-}
module Test.Control.Concurrent.Linearizable (tests) where
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"
[ testGroup
"Unit tests"
[ testEmptyHistoryLinearizable,
testLinearizableExample,
testNonLinearizableExample,
testLinearizableExampleWithFail,
testIdleNode,
testIdleNodeNonLinearizable,
testEarlyFinisher
],
testGroup
"Property tests"
[testPropValidHistory]
]
testEmptyHistoryLinearizable :: TestTree
testEmptyHistoryLinearizable =
testCase "Empty example" $
let emptyHistory = [] :: [(Int, Event Timestamp Op)]
in assertBool
"Trivial history should be linearizable"
(linearizable model emptyHistory)
testLinearizableExample :: TestTree
testLinearizableExample = testCase "Linearizable example" $ do
let history =
[ (2, get Nothing 99 101),
(0, put 8 100 105),
(1, put 7 104 106),
(2, get (Just 7) 102 108),
(1, get (Just 8) 110 112),
(2, get (Just 9) 109 115),
(0, get (Just 8) 111 117),
(1, put 9 114 118)
]
assertBool "History should be linearizable" (linearizable @Int model history)
testNonLinearizableExample :: TestTree
testNonLinearizableExample = testCase "Non-linearizable example" $ do
let history =
[ (0, put 8 100 105),
(1, put 7 104 106),
(2, put 9 107 110),
(0, get (Just 7) 111 113)
]
assertBool "History should not be linearizable" (not $ linearizable @Int model history)
testLinearizableExampleWithFail :: TestTree
testLinearizableExampleWithFail = testCase "Linearizable example with failures" $ do
let history =
[ (0, put 8 100 105),
(1, failed 104 107),
(1, get (Just 8) 109 110),
(0, get (Just 7) 111 112),
(1, get (Just 9) 113 114),
(1, put 10 115 117),
(0, get (Just 10) 118 119)
]
assertBool "History should be linearizable" (linearizable @Int model history)
testIdleNode :: TestTree
testIdleNode = testCase "Idle node in the node list" $ do
-- node 2 isn't involved in the history
let history =
[ (0, put 1 0 2),
(1, get (Just 1) 3 4),
(0, put 2 5 6),
(1, get (Just 2) 7 8)
]
assertBool "History should be linearizable" (linearizable @Int model history)
testIdleNodeNonLinearizable :: TestTree
testIdleNodeNonLinearizable = testCase "Idle node, non-linearizable history" $ do
-- node 2 isn't involved in the history, but also the history
-- isn't linearizable
let history =
[ (0, put 1 0 2),
(1, get (Just 2) 3 4)
]
assertBool "History should not be linearizable" (not $ linearizable @Int model history)
testEarlyFinisher :: TestTree
testEarlyFinisher = testCase "Client finishing early" $ do
-- node 0's history ends early
let history =
[ (0, put 0 0 1),
(1, get (Just 0) 2 3),
(2, put 5 4 5),
(1, get (Just 5) 6 7),
(2, get (Just 5) 8 9)
]
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)
deriving (Eq, Ord)
model :: Model State Op
model =
Model
{ initialState = Known Nothing,
apply = \op s -> case op of
Put x -> pure (Known (Just x))
Get expectation
| s == Unknown -> pure s
| s == Known expectation -> pure s
| otherwise -> Nothing
Failed -> pure Unknown
}
type Timestamp = Int
get :: Maybe Int -> Timestamp -> Timestamp -> Event Timestamp Op
get expectation req ack = Event req ack (Get expectation)
put :: Int -> Timestamp -> Timestamp -> Event Timestamp Op
put value req ack = Event req ack (Put value)
failed :: Timestamp -> Timestamp -> Event Timestamp Op
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)
]