Skip to content

Commit

Permalink
Added alternative instance test.
Browse files Browse the repository at this point in the history
This is supposed to reproduce Issue #7, but it is not doing so.
  • Loading branch information
acowley committed Sep 18, 2017
1 parent ac95513 commit 1b4b758
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion concurrent-machines.cabal
Expand Up @@ -72,7 +72,7 @@ test-suite tests
type: exitcode-stdio-1.0
hs-source-dirs: tests
main-is: AllTests.hs
ghc-options: -Wall -O0
ghc-options: -Wall -O -threaded
default-language: Haskell2010
build-depends: base >= 4.6 && < 5, concurrent-machines, machines,
tasty, tasty-hunit, transformers, time
Expand Down
4 changes: 2 additions & 2 deletions stack.yaml
@@ -1,4 +1,4 @@
resolver: lts-8.21
resolver: nightly-2017-09-18

packages:
- ./.
Expand All @@ -15,4 +15,4 @@ flags: {}
extra-package-dbs: []

nix:
packages: [cairo pkgconfig zlib]
shell-file: shell.nix
28 changes: 26 additions & 2 deletions tests/AllTests.hs
@@ -1,4 +1,6 @@
{-# LANGUAGE RankNTypes #-}
import Data.Time.Clock (getCurrentTime, diffUTCTime)
import Control.Applicative ((<|>))
import Control.Concurrent (threadDelay)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Machine.Concurrent
Expand All @@ -17,6 +19,28 @@ timed m = do t1 <- liftIO getCurrentTime
t2 <- liftIO getCurrentTime
return (r, realToFrac $ t2 `diffUTCTime` t1)


-- Based on GitHub Issue 7
-- https://github.com/acowley/concurrent-machines/issues/7
alternativeWorks :: TestTree
alternativeWorks = testCase "alternative" $ do
xs <- runT (replicated 5 "Step" ~> construct aux)
assertEqual "Results" (replicate 5 "Step" ++ ["Done"]) xs
where aux = do x <- await <|> yield "Done" *> stop
yield x
aux

alternativeWorksDelay :: TestTree
alternativeWorksDelay = testCase "alternative with delay" $ do
xs <- runT (construct (gen 5) ~> construct aux)
assertEqual "Results" (replicate 5 "Step" ++ ["Done"]) xs
where aux = do x <- await <|> yield "Done" *> stop
yield x
aux
gen :: MonadIO m => Int -> PlanT k String m a
gen 0 = stop
gen n = yield "Step" >> liftIO (threadDelay 100000) >> gen (n-1)

pipeline :: TestTree
pipeline = testCaseSteps "pipeline" $ \step -> do
(r,dt) <- timed . runT . supply (repeat ()) $
Expand Down Expand Up @@ -46,6 +70,6 @@ workStealing = testCaseSteps "work stealing" $ \step -> do
yield (x * 2)

main :: IO ()
main = defaultMain $
main = defaultMain $
testGroup "concurrent-machines"
[ pipeline, workStealing ]
[ pipeline, workStealing, alternativeWorks, alternativeWorksDelay ]

0 comments on commit 1b4b758

Please sign in to comment.