mirror of
https://github.com/composewell/streamly.git
synced 2024-09-11 11:37:22 +03:00
Add applicative and alternative tests
This commit is contained in:
parent
b9c1c92dc0
commit
594185f18d
30
test/alternative.hs
Normal file
30
test/alternative.hs
Normal file
@ -0,0 +1,30 @@
|
||||
import Control.Concurrent (threadDelay, myThreadId)
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import System.Random (randomIO)
|
||||
import System.IO
|
||||
import Control.Applicative
|
||||
|
||||
import Strands
|
||||
|
||||
main = wait $ threads 3 $ do
|
||||
liftIO $ hSetBuffering stdout LineBuffering
|
||||
mainThread <- liftIO myThreadId
|
||||
liftIO $ putStrLn $ "Main thread: " ++ show mainThread
|
||||
|
||||
eventA <|> eventB
|
||||
|
||||
eventA = do
|
||||
x <- sample (randomIO :: IO Int) 1000000
|
||||
evThread <- liftIO myThreadId
|
||||
liftIO $ putStrLn $ "X Event thread: " ++ show evThread
|
||||
liftIO $ putStrLn $ "x = " ++ (show x)
|
||||
return x
|
||||
|
||||
eventB = do
|
||||
y <- sample (randomIO :: IO Int) 1000000
|
||||
|
||||
-- liftIO $ threadDelay 10000000
|
||||
evThread <- liftIO myThreadId
|
||||
liftIO $ putStrLn $ "Y Event thread: " ++ show evThread
|
||||
liftIO $ putStrLn $ "y = " ++ (show y)
|
||||
return y
|
17
test/applicative.hs
Normal file
17
test/applicative.hs
Normal file
@ -0,0 +1,17 @@
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
|
||||
import Control.Applicative ((<$>), (<*>), (<|>))
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import System.IO
|
||||
import Strands
|
||||
|
||||
main = do
|
||||
hSetBuffering stdout LineBuffering
|
||||
xs <- gather $ threads 5 $ do
|
||||
x <- (,) <$> (event 1 <|> event 2) <*> (event 3 <|> event 4)
|
||||
--x <- (,) <$> choose [1,2] <*> choose [3,4]
|
||||
liftIO $ putStrLn $ show x
|
||||
return x
|
||||
putStrLn $ show xs
|
||||
where
|
||||
event n = async (do putStrLn ("event" ++ show n); return n :: IO Int)
|
Loading…
Reference in New Issue
Block a user