Skip to content

Commit 611f004

Browse files
committed
tryReadTBQueueDefault: fix bug when returning Nothing
1 parent 202e205 commit 611f004

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

io-sim/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history of io-sim
22

3+
## Next release
4+
5+
- Fixed `tryReadTBQueue` when returning `Nothing`.
6+
37
## 1.5.1.0
48

59
- The signature of:

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ tryReadTBQueueDefault (TBQueue queue _size) = do
149149
return (Just x)
150150
[] ->
151151
case reverse ys of
152-
[] -> do
153-
writeTVar queue $! (xs, r', ys, w)
154-
return Nothing
152+
[] -> return Nothing
155153

156154
-- NB. lazy: we want the transaction to be
157155
-- short, otherwise it will conflict

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ tests =
184184
[ testProperty "empties the queue" prop_flushTQueueEmpties
185185
, testProperty "maintains FIFO order" prop_flushTQueueOrder
186186
]
187+
, testGroup "tryReadTBQueue"
188+
[ testProperty "works correctly when the queue is empty IO" prop_tryReadEmptyTBQueue_IO
189+
, testProperty "works correctly when the queue is empty IOSim" prop_tryReadEmptyTBQueue_IOSim
190+
]
187191
]
188192
]
189193

@@ -1412,6 +1416,24 @@ writeAndFlushQueue entries =
14121416
forM_ entries $ writeTQueue q
14131417
flushTQueue q
14141418

1419+
prop_tryReadEmptyTBQueue_IO :: Bool -> Property
1420+
prop_tryReadEmptyTBQueue_IO sndRead =
1421+
ioProperty $ tryReadEmptyTBQueue sndRead
1422+
1423+
prop_tryReadEmptyTBQueue_IOSim :: Bool -> Property
1424+
prop_tryReadEmptyTBQueue_IOSim sndRead =
1425+
runSimOrThrow $ tryReadEmptyTBQueue sndRead
1426+
1427+
tryReadEmptyTBQueue :: MonadSTM m => Bool -> m Property
1428+
tryReadEmptyTBQueue sndRead = atomically $ do
1429+
q <- newTBQueue 10
1430+
_ <- tryReadTBQueue q
1431+
writeTBQueue q ()
1432+
when sndRead $ void $ tryReadTBQueue q
1433+
l <- lengthTBQueue q
1434+
1435+
pure $ l === if sndRead then 0 else 1
1436+
14151437
--
14161438
-- Utils
14171439
--

0 commit comments

Comments
 (0)