Skip to content

Commit

Permalink
Add a test that flushTQueue maintains order
Browse files Browse the repository at this point in the history
This is currently broken.
  • Loading branch information
ch1bo committed Jan 30, 2024
1 parent 614974d commit 590512c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions io-sim/test/Test/Control/Monad/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ tests =
prop_registerDelayCancellable_IO
]
, testGroup "MonadSTM"
[ testProperty "flushTQueue empties the queue" prop_flushTQueue ]
[ testGroup "flushTQueue"
[ testProperty "empties the queue" prop_flushTQueueEmpties
, testProperty "maintains FIFO order" prop_flushTQueueOrder
]
]
]

--
Expand Down Expand Up @@ -1350,8 +1354,9 @@ prop_registerDelayCancellable_IO =
cancelTimeout
awaitTimeout

prop_flushTQueue :: Property
prop_flushTQueue =
-- | Test that 'flushTQueue' empties the queue.
prop_flushTQueueEmpties :: Property
prop_flushTQueueEmpties =
ioProperty emptyQueueAfterFlush
.&&. runSimOrThrow emptyQueueAfterFlush

Expand All @@ -1363,6 +1368,19 @@ emptyQueueAfterFlush = do
_ <- flushTQueue q
isEmptyTQueue q

-- | Test that 'flushTQueue' returns values in FIFO order.
prop_flushTQueueOrder :: [Int] -> Property
prop_flushTQueueOrder entries =
ioProperty (writeAndFlushQueue entries >>= \actual -> pure $ actual === entries)
.&&. runSimOrThrow (writeAndFlushQueue entries) === entries

writeAndFlushQueue :: MonadSTM m => [Int] -> m [Int]
writeAndFlushQueue entries =
atomically $ do
q <- newTQueue
forM_ entries $ writeTQueue q
flushTQueue q

--
-- Utils
--
Expand Down

0 comments on commit 590512c

Please sign in to comment.