Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tryReadTBQueueDefault: fix bug when returning Nothing #196

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions io-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
is more general. These functions now accepts trace with any result, rather
than one that finishes with `SimResult`.
- More polymorphic `ppTrace_` type signature.
- 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
Loading