Skip to content

Commit 24e7ee8

Browse files
committed
Add a test-suite for concurrent scans
1 parent c192480 commit 24e7ee8

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

streamly.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ extra-source-files:
136136
test/Streamly/Test/Prelude/*.hs
137137
test/Streamly/Test/Unicode/*.hs
138138
test/Streamly/Test/Serialize/*.hs
139+
test/Streamly/Test/Data/Scanl/*.hs
139140
test/Streamly/Test/Data/Fold/*.hs
140141
test/lib/Streamly/Test/Common.hs
141142
test/lib/Streamly/Test/Prelude/Common.hs
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
-- |
2+
-- Module : Streamly.Test.Data.Scanl.Concurrent
3+
-- Copyright : (c) 2020 Composewell Technologies
4+
--
5+
-- License : BSD-3-Clause
6+
-- Maintainer : streamly@composewell.com
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+
parDistributeScanTestScanEnd :: (Stream.Config -> Stream.Config) -> IO ()
43+
parDistributeScanTestScanEnd concOpts = do
44+
let streamLen = 10000
45+
evenLen = 100
46+
ref <- newIORef [Scanl.take evenLen evenScan, oddScan]
47+
let gen = atomicModifyIORef' ref (\xs -> ([], xs))
48+
inpList = [1..streamLen]
49+
inpStream = Stream.fromList inpList
50+
res1 <-
51+
Scanl.parDistributeScan concOpts gen inpStream
52+
& Stream.concatMap Stream.fromList
53+
& Stream.catMaybes
54+
& Stream.fold Fold.toList
55+
sort res1 `shouldBe` [1..evenLen] ++ filter odd [(evenLen+1)..streamLen]
56+
57+
parDistributeScanTestStreamEnd :: (Stream.Config -> Stream.Config) -> IO ()
58+
parDistributeScanTestStreamEnd concOpts = do
59+
let streamLen = 10000
60+
ref <- newIORef [evenScan, oddScan]
61+
let gen = atomicModifyIORef' ref (\xs -> ([], xs))
62+
inpList = [1..streamLen]
63+
inpStream = Stream.fromList inpList
64+
res1 <-
65+
Scanl.parDistributeScan concOpts gen inpStream
66+
& Stream.concatMap Stream.fromList
67+
& Stream.catMaybes
68+
& Stream.fold Fold.toList
69+
sort res1 `shouldBe` inpList
70+
71+
main :: IO ()
72+
main = hspec
73+
$ H.parallel
74+
#ifdef COVERAGE_BUILD
75+
$ modifyMaxSuccess (const 10)
76+
#endif
77+
$ describe moduleName $ do
78+
it "parDistributeScan (stream end) (maxBuffer 1)"
79+
$ parDistributeScanTestStreamEnd (Stream.maxBuffer 1)
80+
it "parDistributeScan (scan end)"
81+
$ parDistributeScanTestScanEnd (Stream.maxBuffer 1)

test/streamly-tests.cabal

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ test-suite Data.RingArray
304304
main-is: Streamly/Test/Data/RingArray.hs
305305
ghc-options: -main-is Streamly.Test.Data.RingArray.main
306306

307+
test-suite Data.Scanl.Concurrent
308+
import: test-options
309+
type: exitcode-stdio-1.0
310+
main-is: Streamly/Test/Data/Scanl/Concurrent.hs
311+
ghc-options: -main-is Streamly.Test.Data.Scanl.Concurrent.main
312+
if flag(use-streamly-core)
313+
buildable: False
314+
307315
-- XXX Rename to MutByteArray
308316
test-suite Data.Serialize
309317
import: test-options

0 commit comments

Comments
 (0)