Skip to content

Commit

Permalink
tryReadTBQueueDefault: fix bug when returning Nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
amesgen committed Dec 7, 2024
1 parent 785dc08 commit e349d6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions io-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
the value to other waiting threads.
- Faster handling of timeouts and timers by using a more efficient
internal representation.
- Fixed `tryReadTBQueue` when returning `Nothing`.

## 1.6.0.0

Expand Down
4 changes: 1 addition & 3 deletions io-sim/src/Control/Monad/IOSim/STM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ tryReadTBQueueDefault (TBQueue queue _size) = do
return (Just x)
[] ->
case reverse ys of
[] -> do
writeTVar queue $! (xs, r', ys, w)
return Nothing
[] -> return Nothing

-- NB. lazy: we want the transaction to be
-- short, otherwise it will conflict
Expand Down
22 changes: 22 additions & 0 deletions io-sim/test/Test/Control/Monad/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ tests =
, testProperty "maintains FIFO order IO" prop_flushTBQueueOrder_IO
, testProperty "maintains FIFO order IOSim" prop_flushTBQueueOrder_IOSim
]
, testGroup "tryReadTBQueue"
[ testProperty "works correctly when the queue is empty IO" prop_tryReadEmptyTBQueue_IO
, testProperty "works correctly when the queue is empty IOSim" prop_tryReadEmptyTBQueue_IOSim
]
]
]

Expand Down Expand Up @@ -1464,6 +1468,24 @@ writeAndFlushTBQueue entries =
forM_ entries $ writeTBQueue q
flushTBQueue q

prop_tryReadEmptyTBQueue_IO :: Bool -> Property
prop_tryReadEmptyTBQueue_IO sndRead =
ioProperty $ tryReadEmptyTBQueue sndRead

prop_tryReadEmptyTBQueue_IOSim :: Bool -> Property
prop_tryReadEmptyTBQueue_IOSim sndRead =
runSimOrThrow $ tryReadEmptyTBQueue sndRead

tryReadEmptyTBQueue :: MonadSTM m => Bool -> m Property
tryReadEmptyTBQueue sndRead = atomically $ do
q <- newTBQueue 10
_ <- tryReadTBQueue q
writeTBQueue q ()
when sndRead $ void $ tryReadTBQueue q
l <- lengthTBQueue q

pure $ l === if sndRead then 0 else 1

--
-- Utils
--
Expand Down

0 comments on commit e349d6d

Please sign in to comment.