@@ -9,7 +9,7 @@
99
1010module Streamly.Test.Prelude (main) where
1111
12−import Control.Concurrent (threadDelay)
12+import Control.Concurrent (myThreadId, threadDelay)
1313import Control.Exception (Exception, try)
1414import Control.Monad (when)
1515import Control.Monad.Catch (throwM)
@@ -23,6 +23,8 @@ import Data.Maybe (fromJust, isJust)
2323#if !(MIN_VERSION_base(4,11,0))
2424import Data.Semigroup (Semigroup(..))
2525#endif
26+import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
27+import System.Random (randomIO)
2628import Test.Hspec as H
2729import Test.Hspec.QuickCheck (prop)
2830import Test.QuickCheck (Property, choose)
@@ -227,6 +229,26 @@ mixedOpsAheadly =
227229 return (x1 + y1 + z1)
228230 return (x + y + z)
229231
232+nestedLoops :: IO ()
233+nestedLoops = S.drain $ do
234+ S.yieldM $ hSetBuffering stdout LineBuffering
235+ x <- loop "A " 2
236+ y <- loop "B " 2
237+ S.yieldM $ myThreadId >>= putStr . show
238+ >> putStr " "
239+ >> print (x, y)
240+
241+ where
242+
243+ -- we can just use
244+ -- parallely $ mconcat $ replicate n $ yieldM (...)
245+ loop :: String -> Int -> SerialT IO String
246+ loop name n = do
247+ rnd <- S.yieldM (randomIO :: IO Int)
248+ let result = name <> show rnd
249+ repeatIt = if n > 1 then loop name (n - 1) else S.nil
250+ in return result `S.wAsync` repeatIt
251+
230252main :: IO ()
231253main = hspec $ H.parallel $ do
232254 describe "Filtering" $ do
@@ -243,6 +265,7 @@ main = hspec $ H.parallel $ do
243265 describe "Miscellaneous combined examples aheadly" mixedOpsAheadly
244266 describe "Simple MonadError and MonadThrow" simpleMonadError
245267
268+ it "Nested loops" nestedLoops
246269 {-
247270 describe "Composed MonadError serially" $ composeWithMonadError serially
248271 describe "Composed MonadError wSerially" $ composeWithMonadError wSerially