Add new `cabal`-powered testsuite
[packages/stm.git] / testsuite / src / Issue9.hs
1 {-# LANGUAGE CPP #-}
2
3 -- see https://github.com/haskell/stm/pull/9
4 --
5 -- Test-case contributed by Mitchell Rosen <mitchellwrosen@gmail.com>
6 --
7 -- This bug is observable in version `stm-2.4.5.0`
8
9 module Issue9 (main) where
10
11 import Control.Concurrent.STM
12 import Data.Foldable
13
14 main :: IO ()
15 #if MIN_VERSION_stm(2,4,5)
16 main = do
17 -- New queue with capacity 5
18 queue <- newTBQueueIO (5 :: Int)
19
20 -- Fill it up with [1..5]
21 for_ [1..5] $ \i ->
22 atomically (writeTBQueue queue (i :: Int))
23
24 -- Read 1
25 1 <- atomically (readTBQueue queue)
26
27 -- Flush [2..5]
28 [2,3,4,5] <- atomically (flushTBQueue queue)
29
30 -- The bug: now the queue capacity is 4, not 5.
31 -- To trigger it, first fill up [1..4]...
32 for_ [1..4] $ \i ->
33 atomically (writeTBQueue queue i)
34
35 -- ... then observe that writing a 5th element will fail
36 -- with "thread blocked indefinitely in an STM transaction"
37 atomically (writeTBQueue queue 5)
38 #else
39 -- test-case not applicable; `flushTBQueue` was only added in 2.4.5.0
40 main = return ()
41 #endif