Skip to content

Commit 1c84274

Browse files
committed
Run strict-mvar tests with IOSim
1 parent 6392278 commit 1c84274

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

strict-mvar/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ are distinct types.
4545
Although all functions that modify a checked `StrictMVar` will check the
4646
invariant, we do *not* guarantee that the value inside the `StrictMVar` always
4747
satisfies the invariant. Instead, we *do* guarantee that if the `StrictMVar` is
48-
updated with a value that does not satisfy the invariant, an exception is
49-
thrown. The reason for this weaker guarantee is that leaving an `MVar` empty can
50-
lead to very hard to debug "blocked indefinitely" problems.
48+
updated with a value that does not satisfy the invariant, an exception is thrown
49+
*after* the new value is written to the `StrictMVar`. The reason for this weaker
50+
guarantee is that leaving an `MVar` empty can lead to very hard to debug
51+
"blocked indefinitely" problems.

strict-mvar/strict-mvar.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test-suite test
4848
other-modules: Test.Control.Concurrent.Class.MonadMVar.Strict.Checked
4949
default-language: Haskell2010
5050
build-depends: base >=4.9 && <4.19,
51+
io-sim,
5152
QuickCheck,
5253
tasty,
5354
tasty-quickcheck,
Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
13
module Test.Control.Concurrent.Class.MonadMVar.Strict.Checked where
24

35
import Control.Concurrent.Class.MonadMVar.Strict.Checked
6+
import Control.Monad.IOSim
7+
import Test.QuickCheck.Gen.Unsafe (Capture (..), capture)
48
import Test.QuickCheck.Monadic
59
import Test.Tasty
610
import Test.Tasty.QuickCheck
711

812
tests :: TestTree
913
tests = testGroup "Test.Control.Concurrent.Class.MonadMVar.Strict" [
1014
testGroup "Checked" [
11-
testProperty "prop_invariantShouldFail" prop_invariantShouldFail
12-
, testProperty "prop_invariantShouldNotFail" prop_invariantShouldNotFail
15+
testGroup "IO" [
16+
testProperty "prop_invariantShouldFail" $
17+
once $ expectFailure $ monadicIO prop_invariantShouldFail
18+
, testProperty "prop_invariantShouldNotFail" $
19+
once $ monadicIO prop_invariantShouldNotFail
20+
]
21+
, testGroup "IOSim" [
22+
testProperty "prop_invariantShouldFail" $
23+
once $ expectFailure $ monadicSim prop_invariantShouldFail
24+
, testProperty "prop_invariantShouldNotFail" $
25+
once $ monadicSim prop_invariantShouldNotFail
26+
]
1327
]
1428
]
1529

@@ -19,12 +33,24 @@ invPositiveInt x
1933
| x >= 0 = Nothing
2034
| otherwise = Just $ "x<0 for x=" <> show x
2135

22-
prop_invariantShouldFail :: Property
23-
prop_invariantShouldFail = once $ expectFailure $ monadicIO $ run $ do
36+
prop_invariantShouldNotFail :: MonadMVar m => PropertyM m ()
37+
prop_invariantShouldNotFail = run $ do
2438
v <- newMVarWithInvariant invPositiveInt 0
25-
modifyMVar_ v (\x -> pure $ x - 1)
39+
modifyMVar_ v (\x -> pure $ x + 1)
2640

27-
prop_invariantShouldNotFail :: Property
28-
prop_invariantShouldNotFail = monadicIO $ run $ do
41+
prop_invariantShouldFail :: MonadMVar m => PropertyM m ()
42+
prop_invariantShouldFail = run $ do
2943
v <- newMVarWithInvariant invPositiveInt 0
30-
modifyMVar_ v (\x -> pure $ x + 1)
44+
modifyMVar_ v (\x -> pure $ x - 1)
45+
46+
{-------------------------------------------------------------------------------
47+
Property runners (copied from "Ouroboros.Network.Testing.QuickCheck")
48+
-------------------------------------------------------------------------------}
49+
50+
runSimGen :: (forall s. Gen (IOSim s a)) -> Gen a
51+
runSimGen f = do
52+
Capture eval <- capture
53+
return $ runSimOrThrow (eval f)
54+
55+
monadicSim :: Testable a => (forall s. PropertyM (IOSim s) a) -> Property
56+
monadicSim m = property (runSimGen (monadic' m))

0 commit comments

Comments
 (0)