Skip to content

Commit 29daad4

Browse files
committed
io-classes: added TBQueue test
stm-2.5.2.x comes with a bug. I added a test to cover it (#137), and a constraint to avoid it.
1 parent 413d7f3 commit 29daad4

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

io-classes/io-classes.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ library
9999
bytestring,
100100
mtl >=2.2 && <2.4,
101101
primitive >= 0.7 && <0.11,
102-
stm >=2.5 && <2.6,
102+
stm >=2.5 && <2.5.2 || >=2.5.3 && <2.6,
103103
time >=1.9.1 && <1.13
104104
if impl(ghc >= 9.10)
105105
build-depends: ghc-internal

io-sim/test/Test/Control/Monad/IOSim.hs

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,16 @@ tests =
181181
]
182182
, testGroup "MonadSTM"
183183
[ testGroup "flushTQueue"
184-
[ testProperty "empties the queue" prop_flushTQueueEmpties
185-
, testProperty "maintains FIFO order" prop_flushTQueueOrder
184+
[ testProperty "empties the queue IO" prop_flushTQueueEmpties_IO
185+
, testProperty "empties the queue IOSim" prop_flushTQueueEmpties_IOSim
186+
, testProperty "maintains FIFO order IO" prop_flushTQueueOrder_IO
187+
, testProperty "maintains FIFO order IOSim" prop_flushTQueueOrder_IOSim
188+
]
189+
, testGroup "flushTBQueue"
190+
[ testProperty "empties the queue IO" prop_flushTBQueueEmpties_IO
191+
, testProperty "empties the queue IOSim" prop_flushTBQueueEmpties_IOSim
192+
, testProperty "maintains FIFO order IO" prop_flushTBQueueOrder_IO
193+
, testProperty "maintains FIFO order IOSim" prop_flushTBQueueOrder_IOSim
186194
]
187195
]
188196
]
@@ -1385,33 +1393,77 @@ prop_registerDelayCancellable_IO =
13851393
cancelTimeout
13861394
awaitTimeout
13871395

1388-
-- | Test that 'flushTQueue' empties the queue.
1389-
prop_flushTQueueEmpties :: Property
1390-
prop_flushTQueueEmpties =
1391-
ioProperty emptyQueueAfterFlush
1392-
.&&. runSimOrThrow emptyQueueAfterFlush
1396+
-- | Test that 'flushTQueue' empties the queue in `IO`.
1397+
prop_flushTQueueEmpties_IO :: Property
1398+
prop_flushTQueueEmpties_IO =
1399+
ioProperty emptyTQueueAfterFlush
1400+
1401+
-- | Test that 'flushTQueue' empties the queue in `IOSim`.
1402+
prop_flushTQueueEmpties_IOSim :: Bool
1403+
prop_flushTQueueEmpties_IOSim =
1404+
runSimOrThrow emptyTQueueAfterFlush
13931405

1394-
emptyQueueAfterFlush :: MonadSTM m => m Bool
1395-
emptyQueueAfterFlush = do
1406+
emptyTQueueAfterFlush :: MonadSTM m => m Bool
1407+
emptyTQueueAfterFlush = do
13961408
q <- newTQueueIO
13971409
atomically $ do
13981410
writeTQueue q (1 :: Int)
13991411
_ <- flushTQueue q
14001412
isEmptyTQueue q
14011413

1402-
-- | Test that 'flushTQueue' returns values in FIFO order.
1403-
prop_flushTQueueOrder :: [Int] -> Property
1404-
prop_flushTQueueOrder entries =
1405-
ioProperty (writeAndFlushQueue entries >>= \actual -> pure $ actual === entries)
1406-
.&&. runSimOrThrow (writeAndFlushQueue entries) === entries
1414+
-- | Test that 'flushTQueue' returns values in FIFO order in `IO`.
1415+
prop_flushTQueueOrder_IO :: [Int] -> Property
1416+
prop_flushTQueueOrder_IO entries =
1417+
ioProperty (writeAndFlushTQueue entries >>= \actual -> pure $ actual === entries)
14071418

1408-
writeAndFlushQueue :: MonadSTM m => [Int] -> m [Int]
1409-
writeAndFlushQueue entries =
1419+
-- | Test that 'flushTQueue' returns values in FIFO order in `IOSim`.
1420+
prop_flushTQueueOrder_IOSim :: [Int] -> Property
1421+
prop_flushTQueueOrder_IOSim entries =
1422+
runSimOrThrow (writeAndFlushTQueue entries) === entries
1423+
1424+
writeAndFlushTQueue :: MonadSTM m => [Int] -> m [Int]
1425+
writeAndFlushTQueue entries =
14101426
atomically $ do
14111427
q <- newTQueue
14121428
forM_ entries $ writeTQueue q
14131429
flushTQueue q
14141430

1431+
-- | Test that 'flushTBQueue' empties the queue in `IO`.
1432+
prop_flushTBQueueEmpties_IO :: Property
1433+
prop_flushTBQueueEmpties_IO =
1434+
ioProperty emptyTBQueueAfterFlush
1435+
1436+
-- | Test that 'flushTBQueue' empties the queue in `IOSim`.
1437+
prop_flushTBQueueEmpties_IOSim :: Bool
1438+
prop_flushTBQueueEmpties_IOSim =
1439+
runSimOrThrow emptyTBQueueAfterFlush
1440+
1441+
emptyTBQueueAfterFlush :: MonadSTM m => m Bool
1442+
emptyTBQueueAfterFlush = do
1443+
q <- newTBQueueIO 10
1444+
atomically $ do
1445+
writeTBQueue q (1 :: Int)
1446+
writeTBQueue q 2
1447+
_ <- flushTBQueue q
1448+
isEmptyTBQueue q
1449+
1450+
-- | Test that 'flushTBQueue' returns values in FIFO order in `IO`.
1451+
prop_flushTBQueueOrder_IO :: [Int] -> Property
1452+
prop_flushTBQueueOrder_IO entries =
1453+
ioProperty (writeAndFlushTBQueue entries >>= \actual -> pure $ actual === entries)
1454+
1455+
-- | Test that 'flushTBQueue' returns values in FIFO order in `IOSim`.
1456+
prop_flushTBQueueOrder_IOSim :: [Int] -> Property
1457+
prop_flushTBQueueOrder_IOSim entries =
1458+
runSimOrThrow (writeAndFlushTBQueue entries) === entries
1459+
1460+
writeAndFlushTBQueue :: MonadSTM m => [Int] -> m [Int]
1461+
writeAndFlushTBQueue entries =
1462+
atomically $ do
1463+
q <- newTBQueue (1 + fromIntegral (length entries))
1464+
forM_ entries $ writeTBQueue q
1465+
flushTBQueue q
1466+
14151467
--
14161468
-- Utils
14171469
--

0 commit comments

Comments
 (0)