More complicated logging tests

This commit is contained in:
Harendra Kumar 2017-06-29 05:21:50 +05:30
parent 760a298598
commit afbd289a89
2 changed files with 28 additions and 6 deletions

13
test/3-waitEvents.hs Normal file
View File

@ -0,0 +1,13 @@
import Control.Concurrent (threadDelay, myThreadId)
import Control.Monad.IO.Class (liftIO)
import System.Random (randomIO)
import System.IO
import Strands
--- Check space leaks
main = wait_ $ do
x <- waitEvents (randomIO :: IO Int)
y <- waitEvents (randomIO :: IO Int)
return (x, y)

View File

@ -1,26 +1,35 @@
import Strands
{-# LANGUAGE FlexibleContexts #-}
import Control.Applicative ((<$>), (<*>), (<|>))
import Control.Monad.IO.Class (liftIO)
import System.IO
import Strands
main = do
logs <- waitLogged comp
hSetBuffering stdout LineBuffering
logs <- waitLogged_ comp
putStrLn $ "\nResuming with logs:"
logs1 <- waitLogged $ eachWithLog comp logs
logs1 <- waitLogged_ $ eachWithLog comp logs
putStrLn $ "\nResuming with logs:"
logs2 <- waitLogged $ eachWithLog comp logs1
logs2 <- waitLogged_ $ eachWithLog comp logs1
putStrLn $ "\nLogs at the end must be empty:"
putStrLn $ show logs2
putStrLn $ "\nRunning the last log again using wait:"
wait $ eachWithLog comp logs1
wait_ $ eachWithLog comp logs1
where
comp = logged $ threads 5 $ do
r <- logged $ each [1..3]
logged $ liftIO $ print ("A",r)
suspend
logged $ liftIO $ print ("B",r)
x <- logged $ (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
suspend
liftIO $ print ("C",r)
liftIO $ print ("C", r, x)
event n = async (do putStrLn ("event" ++ show n); return n :: IO Int)