1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
{-# LANGUAGE CPP #-}
module Data.Streaming.ProcessSpec (spec, main) where
import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Control.Concurrent.Async (concurrently)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString as S
import System.Exit
import Control.Concurrent (threadDelay)
import Data.Streaming.Process
import System.IO (hClose)
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
#ifndef WINDOWS
prop "cat" $ \wss -> do
let lbs = L.fromChunks $ map S.pack wss
(sink, source, Inherited, cph) <- streamingProcess (shell "cat")
((), bs) <- concurrently
(do
L.hPut sink lbs
hClose sink)
(S.hGetContents source)
L.fromChunks [bs] `shouldBe` lbs
ec <- waitForStreamingProcess cph
ec `shouldBe` ExitSuccess
it "closed stream" $ do
(ClosedStream, source, Inherited, cph) <- streamingProcess (shell "cat")
bss <- S.hGetContents source
bss `shouldBe` S.empty
ec <- waitForStreamingProcess cph
ec `shouldBe` ExitSuccess
it "checked process" $ do
let isRightException ProcessExitedUnsuccessfully {} = True
withCheckedProcess (proc "false" [])
(\Inherited Inherited Inherited -> return ())
`shouldThrow` isRightException
#endif
it "blocking vs non-blocking" $ do
(ClosedStream, ClosedStream, ClosedStream, cph) <- streamingProcess (shell "sleep 1")
mec1 <- getStreamingProcessExitCode cph
mec1 `shouldBe` Nothing
threadDelay 1500000
mec2 <- getStreamingProcessExitCode cph
mec2 `shouldBe` Just ExitSuccess
ec <- waitForStreamingProcess cph
ec `shouldBe` ExitSuccess
|