-- | Every test runs with the default unlifting strategy of the caller,
-- 'SeqUnlift': the wrapped combinators must work without changing it.
module Main (main) where
import Control.Concurrent (threadDelay)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
import Control.Exception (ErrorCall (..), SomeException, throwIO, try)
import Data.IORef (atomicModifyIORef', newIORef, readIORef)
import Data.List (sort)
import Effectful (Eff, MonadIO (liftIO), runEff)
import Effectful.Error.Static (runErrorNoCallStack, throwError)
import Effectful.Reader.Static (ask, runReader)
import Effectful.State.Static.Local qualified as SL (modify, runState)
import Effectful.State.Static.Shared qualified as SS (modify, runState)
import Effectful.Streamly.Stream (Stream, bracket, fold, fromCallback,
fromEffect, fromList, fromPure, interject,
ordered, parConcatMap, parMapM, parReplicateM,
parZipWith, repeatM, sampleIntervalEnd,
takeInterval, toList)
import Effectful.Streamly.Stream qualified as ES (mapM, parList, take)
import Streamly.Data.Fold qualified as F (sum)
import Streamly.Data.Stream.Prelude qualified as S (parList)
import Test.Tasty (TestTree, defaultMain, localOption, mkTimeout, testGroup)
import Test.Tasty.HUnit (assertFailure, testCase, (@?=))
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests =
localOption (mkTimeout 10_000_000) $
testGroup
"effectful-streamly"
[ testCase "streamly alone fails in Eff" $ do
outcome <- try @SomeException . runEff $ toList (S.parList id [one 1, one 2])
case outcome of
Left _ -> pure ()
Right values -> assertFailure ("expected a failure, got " <> show values)
, testGroup
"combinators"
[ testCase "parList" $ do
values <- runEff $ toList (ES.parList id (map one [1 .. 50]))
sort values @?= [1 .. 50]
, testCase "parMapM" $ do
values <- runEff $ toList (parMapM id (pure . (* 2)) (fromList [1 .. 20]))
sort values @?= map (* 2) [1 .. 20 :: Int]
, testCase "parMapM, ordered" $ do
values <- runEff $ toList (parMapM (ordered True) (pure . (* 2)) (fromList [1 .. 20]))
values @?= map (* 2) [1 .. 20 :: Int]
, testCase "parConcatMap" $ do
values <- runEff $ toList (parConcatMap id (\n -> fromList [n, n]) (fromList [1, 2, 3]))
sort values @?= [1, 1, 2, 2, 3, 3 :: Int]
, testCase "parZipWith" $ do
values <- runEff $ toList (parZipWith id (+) (fromList [1, 2, 3]) (fromList [10, 20, 30]))
values @?= [11, 22, 33 :: Int]
, testCase "parReplicateM" $ do
values <- runEff $ toList (parReplicateM id 5 (pure 'x'))
values @?= "xxxxx"
, testCase "fromCallback" $ do
values <- runEff $ do
let source = fromCallback (\push -> mapM_ push [1, 2, 3 :: Int])
toList (ES.take 3 source)
values @?= [1, 2, 3]
, testCase "interject" $ do
values <- runEff . toList . ES.take 3 $
interject (pure 0) 0.01 (repeatM (liftIO (threadDelay 1_000_000) >> pure 1))
values @?= [0, 0, 0 :: Int]
, testCase "takeInterval yields nothing (streamly 0.11.1 defect)" $ do
-- composewell/streamly#3271. When this fails, streamly has fixed
-- takeInterval: update the documentation of the wrapper.
values <- runEff $ toList (takeInterval 1 (fromList [1 .. 10 :: Int]))
values @?= []
, testCase "sampleIntervalEnd" $ do
values <- runEff . toList . sampleIntervalEnd 0.05 $
ES.mapM (\n -> liftIO (threadDelay 5_000) >> pure n) (fromList [1 .. 40 :: Int])
last values @?= 40
, testCase "bracket releases when the stream stops" $ do
released <- newIORef (0 :: Int)
values <- runEff . toList $
bracket (pure ()) (\() -> liftIO (atomicModifyIORef' released (\n -> (n + 1, ())))) (\() -> fromList "ab")
values @?= "ab"
readIORef released >>= (@?= 1)
]
, testGroup
"effects in workers"
[ testCase "the streams really run at the same time" $ do
(left, right) <- (,) <$> newEmptyMVar <*> newEmptyMVar
let ping = fromEffect (liftIO (putMVar left () >> takeMVar right) >> pure "ping")
pong = fromEffect (liftIO (takeMVar left >> putMVar right ()) >> pure "pong")
values <- runEff $ toList (ES.parList id [ping, pong])
sort values @?= ["ping", "pong" :: String]
, testCase "a Reader is seen" $ do
values <- runEff . runReader (10 :: Int) $
toList (parMapM id (\n -> (+ n) <$> ask @Int) (fromList [1, 2, 3]))
sort values @?= [11, 12, 13]
, testCase "local state stays in the worker" $ do
(_, final) <- runEff . SL.runState (0 :: Int) $
toList (ES.parList id (replicate 3 (fromEffect (SL.modify @Int (+ 1)))))
final @?= 0
, testCase "shared state reaches the consumer" $ do
(_, final) <- runEff . SS.runState (0 :: Int) $
toList (ES.parList id (replicate 3 (fromEffect (SS.modify @Int (+ 1)))))
final @?= 3
, testCase "an Error reaches the handler of the consumer" $ do
outcome <- runEff . runErrorNoCallStack @String $
toList (ES.parList id [one 1, fromEffect (throwError @String "boom"), one 3])
outcome @?= Left "boom"
, testCase "an exception reaches the consumer" $ do
outcome <- try @ErrorCall . runEff $
toList (ES.parList id [one 1, fromEffect (liftIO (throwIO (ErrorCall "boom")))])
outcome @?= Left (ErrorCall "boom")
, testCase "a fold over a concurrent stream" $ do
total <- runEff $ fold F.sum (parMapM id pure (fromList [1 .. 100 :: Int]))
total @?= 5050
]
]
where
one :: Int -> Stream (Eff es) Int
one = fromPure