Skip to content

Commit 614974d

Browse files
committed
io-sim: Fix flushTQueue implementation
Fixes #133
1 parent f6919ea commit 614974d

File tree

6 files changed

+23
-4
lines changed

6 files changed

+23
-4
lines changed

io-classes/src/Control/Concurrent/Class/MonadSTM/TQueue.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE ExplicitNamespaces #-}
22

3-
-- | This module corresponds to `Control.Concurrnet.STM.TVar` in "stm" package
3+
-- | This module corresponds to `Control.Concurrent.STM.TQueue` in "stm" package
44
--
55
module Control.Concurrent.Class.MonadSTM.TQueue
66
( -- * MonadSTM

io-classes/src/Control/Monad/Class/MonadSTM/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module Control.Monad.Class.MonadSTM.Internal
4848
, isEmptyTMVarDefault
4949
, labelTMVarDefault
5050
, traceTMVarDefault
51-
-- ** Default 'TBQueue' implementation
51+
-- ** Default 'TQueue' implementation
5252
, TQueueDefault (..)
5353
, newTQueueDefault
5454
, writeTQueueDefault

io-sim/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Non-breaking changes
1010

1111
* `Alternative` & `MonadPlus` instances for `IOSim`.
12+
* Fixed `flushTQueue` implemetation.
1213

1314
## 1.3.1.0
1415

io-sim/io-sim.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
name: io-sim
3-
version: 1.3.1.0
3+
version: 1.3.1.1
44
synopsis: A pure simulator for monadic concurrency with STM.
55
description:
66
A pure simulator monad with support of concurency (base, async), stm,

io-sim/src/Control/Monad/IOSim/STM.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ tryPeekTQueueDefault (TQueue queue) = do
9595
[] -> Nothing
9696

9797
flushTQueueDefault :: MonadSTM m => TQueueDefault m a -> STM m [a]
98-
flushTQueueDefault (TQueue queue) = uncurry (++) <$> readTVar queue
98+
flushTQueueDefault (TQueue queue) = do
99+
xs <- uncurry (++) <$> readTVar queue
100+
writeTVar queue ([], [])
101+
pure xs
99102

100103
unGetTQueueDefault :: MonadSTM m => TQueueDefault m a -> a -> STM m ()
101104
unGetTQueueDefault (TQueue queue) a = do

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ tests =
176176
, testProperty "registerDelayCancellable (IO impl)"
177177
prop_registerDelayCancellable_IO
178178
]
179+
, testGroup "MonadSTM"
180+
[ testProperty "flushTQueue empties the queue" prop_flushTQueue ]
179181
]
180182

181183
--
@@ -1348,6 +1350,19 @@ prop_registerDelayCancellable_IO =
13481350
cancelTimeout
13491351
awaitTimeout
13501352

1353+
prop_flushTQueue :: Property
1354+
prop_flushTQueue =
1355+
ioProperty emptyQueueAfterFlush
1356+
.&&. runSimOrThrow emptyQueueAfterFlush
1357+
1358+
emptyQueueAfterFlush :: MonadSTM m => m Bool
1359+
emptyQueueAfterFlush = do
1360+
q <- newTQueueIO
1361+
atomically $ do
1362+
writeTQueue q (1 :: Int)
1363+
_ <- flushTQueue q
1364+
isEmptyTQueue q
1365+
13511366
--
13521367
-- Utils
13531368
--

0 commit comments

Comments
 (0)