on master help sign in
Add some sample test programs
commit2f8342633ca21a2cc68c2f84b618133849f92d48 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 7 Jun 2017, 21:07 (9 years ago) parentb90da15
4 files changed+77
test/1-event.hs+30 test/2-waitAsync.hs+5 test/3-async.hs+20 test/3-sample.hs+22
test/1-event.hsnew file+30
@@ -0,0 +1,30 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+
3+import Control.Applicative
4+import Control.Concurrent
5+import Control.Concurrent.STM
6+import Control.Monad.State
7+import Data.IORef
8+
9+import Duct.Event
10+
11+f :: StateT EventF IO String
12+f = do
13+ setData "x"
14+ Just x <- getData
15+ return x
16+
17+runEvent :: forall m a. (Alternative m, MonadIO m) => StateT EventF m a -> m a
18+runEvent t = do
19+ zombieChan <- liftIO $ atomically newTChan
20+ pendingRef <- liftIO $ newIORef []
21+ credit <- liftIO $ newIORef maxBound
22+ th <- liftIO $ myThreadId
23+
24+ (r, _) <- runStateT t $ initEventF
25+ (empty :: m a) th zombieChan pendingRef credit
26+ return r
27+
28+main = do
29+ r <- runEvent f
30+ putStrLn r
test/2-waitAsync.hsnew file+5
@@ -0,0 +1,5 @@
1+import Duct
2+import Control.Monad.IO.Class (liftIO)
3+
4+main = waitAsync $ do
5+ liftIO $ putStrLn "hello"
test/3-async.hsnew file+20
@@ -0,0 +1,20 @@
1+import Control.Concurrent (threadDelay, myThreadId)
2+import Control.Monad.IO.Class (liftIO)
3+import System.Random (randomIO)
4+import System.IO
5+
6+import Duct
7+
8+main = waitAsync $ do
9+ liftIO $ hSetBuffering stdout LineBuffering
10+ mainThread <- liftIO myThreadId
11+ liftIO $ putStrLn $ "Main thread: " ++ show mainThread
12+ x <- async (randomIO :: IO Int)
13+ liftIO $ putStrLn $ show x
14+
15+ y <- async (randomIO :: IO Int)
16+
17+ liftIO $ threadDelay 1000000
18+ evThread <- liftIO myThreadId
19+ liftIO $ putStrLn $ "Event thread: " ++ show evThread
20+ liftIO $ putStrLn $ show x
test/3-sample.hsnew file+22
@@ -0,0 +1,22 @@
1+import Control.Concurrent (threadDelay, myThreadId)
2+import Control.Monad.IO.Class (liftIO)
3+import System.Random (randomIO)
4+import System.IO
5+
6+import Duct
7+
8+main = waitAsync $ threads 3 $ do
9+ liftIO $ hSetBuffering stdout LineBuffering
10+ mainThread <- liftIO myThreadId
11+ liftIO $ putStrLn $ "Main thread: " ++ show mainThread
12+ x <- sample (randomIO :: IO Int) 1000000
13+ evThread <- liftIO myThreadId
14+ liftIO $ putStrLn $ "X Event thread: " ++ show evThread
15+ liftIO $ putStrLn $ "x = " ++ (show x)
16+
17+ y <- sample (randomIO :: IO Int) 1000000
18+
19+ -- liftIO $ threadDelay 10000000
20+ evThread <- liftIO myThreadId
21+ liftIO $ putStrLn $ "Y Event thread: " ++ show evThread
22+ liftIO $ putStrLn $ "y = " ++ (show y)