Skip to content

Commit f7c943c

Browse files
committed
tryReadTBQueueDefault: fix bug when returning Nothing
1 parent 573aa8d commit f7c943c

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

io-sim/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- `selectTraceEventsSayWithTime'`
2323
is more general. These functions now accepts trace with any result, rather
2424
than one that finishes with `SimResult`.
25+
- Fixed `tryReadTBQueue` when returning `Nothing`.
2526

2627
## 1.6.0.0
2728

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
@@ -192,6 +192,10 @@ tests =
192192
, testProperty "maintains FIFO order IO" prop_flushTBQueueOrder_IO
193193
, testProperty "maintains FIFO order IOSim" prop_flushTBQueueOrder_IOSim
194194
]
195+
, testGroup "tryReadTBQueue"
196+
[ testProperty "works correctly when the queue is empty IO" prop_tryReadEmptyTBQueue_IO
197+
, testProperty "works correctly when the queue is empty IOSim" prop_tryReadEmptyTBQueue_IOSim
198+
]
195199
]
196200
]
197201

@@ -1464,6 +1468,24 @@ writeAndFlushTBQueue entries =
14641468
forM_ entries $ writeTBQueue q
14651469
flushTBQueue q
14661470

1471+
prop_tryReadEmptyTBQueue_IO :: Bool -> Property
1472+
prop_tryReadEmptyTBQueue_IO sndRead =
1473+
ioProperty $ tryReadEmptyTBQueue sndRead
1474+
1475+
prop_tryReadEmptyTBQueue_IOSim :: Bool -> Property
1476+
prop_tryReadEmptyTBQueue_IOSim sndRead =
1477+
runSimOrThrow $ tryReadEmptyTBQueue sndRead
1478+
1479+
tryReadEmptyTBQueue :: MonadSTM m => Bool -> m Property
1480+
tryReadEmptyTBQueue sndRead = atomically $ do
1481+
q <- newTBQueue 10
1482+
_ <- tryReadTBQueue q
1483+
writeTBQueue q ()
1484+
when sndRead $ void $ tryReadTBQueue q
1485+
l <- lengthTBQueue q
1486+
1487+
pure $ l === if sndRead then 0 else 1
1488+
14671489
--
14681490
-- Utils
14691491
--

0 commit comments

Comments
 (0)