|
| 1 | +-- | |
| 2 | +-- Module : Streamly.Test.Data.Scanl.Concurrent |
| 3 | +-- Copyright : (c) 2020 Composewell Technologies |
| 4 | +-- |
| 5 | +-- License : BSD-3-Clause |
| 6 | + |
| 7 | +-- Stability : experimental |
| 8 | +-- Portability : GHC |
| 9 | + |
| 10 | +module Streamly.Test.Data.Scanl.Concurrent (main) where |
| 11 | + |
| 12 | +import Control.Concurrent (threadDelay) |
| 13 | +import Data.Function ( (&) ) |
| 14 | +import Data.IORef (newIORef, atomicModifyIORef') |
| 15 | +import Data.List (sort) |
| 16 | +import Streamly.Data.Scanl (Scanl) |
| 17 | +import Test.Hspec as H |
| 18 | + |
| 19 | +import qualified Streamly.Data.Fold as Fold |
| 20 | +import qualified Streamly.Data.Stream as Stream |
| 21 | +import qualified Streamly.Data.Stream.Prelude as Stream |
| 22 | +import qualified Streamly.Internal.Data.Scanl as Scanl |
| 23 | +import qualified Streamly.Internal.Data.Scanl.Prelude as Scanl |
| 24 | + |
| 25 | +moduleName :: String |
| 26 | +moduleName = "Data.Scanl.Concurrent" |
| 27 | + |
| 28 | +--------------------------------------------------------------------------- |
| 29 | +-- Main |
| 30 | +--------------------------------------------------------------------------- |
| 31 | + |
| 32 | +evenScan :: Scanl IO Int (Maybe Int) |
| 33 | +evenScan = |
| 34 | + Scanl.filtering even |
| 35 | + & Scanl.lmapM (\x -> threadDelay 100 >> pure x) |
| 36 | + |
| 37 | +oddScan :: Scanl IO Int (Maybe Int) |
| 38 | +oddScan = |
| 39 | + Scanl.filtering odd |
| 40 | + & Scanl.lmapM (\x -> threadDelay 100 >> pure x) |
| 41 | + |
| 42 | +parDistributeScanTest :: (Stream.Config -> Stream.Config) -> IO () |
| 43 | +parDistributeScanTest concOpts = do |
| 44 | + ref <- newIORef [evenScan, oddScan] |
| 45 | + let gen = atomicModifyIORef' ref (\xs -> ([], xs)) |
| 46 | + inpStream = Stream.enumerateFromTo 1 10_000 |
| 47 | + res1 <- |
| 48 | + Scanl.parDistributeScan concOpts gen inpStream |
| 49 | + & Stream.concatMap Stream.fromList |
| 50 | + & Stream.catMaybes |
| 51 | + & Stream.fold Fold.toList |
| 52 | + sort res1 `shouldBe` [1..9999] |
| 53 | + |
| 54 | +main :: IO () |
| 55 | +main = hspec |
| 56 | + $ H.parallel |
| 57 | +#ifdef COVERAGE_BUILD |
| 58 | + $ modifyMaxSuccess (const 10) |
| 59 | +#endif |
| 60 | + $ describe moduleName $ do |
| 61 | + it "parDistributeScan (maxBuffer 1)" |
| 62 | + $ parDistributeScanTest (Stream.maxBuffer 1) |
0 commit comments