Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from i-am-the-slime/master
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
rbobin authored Jun 26, 2018
2 parents 0531a83 + b004856 commit 1c223be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Control.Monad.Aff.Reattempt where
module Effect.Aff.Reattempt where

import Prelude

Expand Down
31 changes: 14 additions & 17 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
module Test.Main where

import Prelude

import Effect.Aff
import Control.Alt ((<|>))
import Control.Monad.Aff (Aff, attempt, delay)
import Control.Monad.Aff.AVar (makeVar, takeVar, putVar, AVar, AVAR)
import Control.Monad.Aff.Reattempt (reattempt)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)
import Control.Monad.Eff.Ref (REF)
import Control.Parallel.Class (parallel, sequential)
import Control.Plus (empty)
import Data.Array (head, tail)
Expand All @@ -18,26 +12,29 @@ import Data.Maybe (maybe, fromMaybe)
import Data.Newtype (unwrap)
import Data.Time.Duration (Milliseconds(..))
import Data.Unfoldable (replicate)
import Effect (Effect)
import Effect.Aff.AVar (new, take, put, AVar)
import Effect.Aff.Reattempt (reattempt)
import Effect.Console (log)
import Test.Unit (test, suite)
import Test.Unit.Assert (assert)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)

failAffsForDurationAndNumberOfAttempts e. Milliseconds Int Array (Aff e Unit)
failAffsForDurationAndNumberOfAttempts Milliseconds Int Array (Aff Unit)
failAffsForDurationAndNumberOfAttempts timeout attemptCount = seq
where
seq = replicate attemptCount do
delay interval
empty
interval = Milliseconds (unwrap timeout / toNumber attemptCount)

affDouble e. AVar (Array (Aff (avar AVAR | e) Unit)) Aff (avar AVAR | e) Unit
affDouble AVar (Array (Aff Unit)) -> Aff Unit
affDouble affsVar = do
affs ← takeVar affsVar
putVar (fromMaybe [] (tail affs)) affsVar
maybe (pure unit) id $ head affs
affs ← take affsVar
put (fromMaybe [] (tail affs)) affsVar
maybe (pure unit) identity $ head affs

main eff. Eff (console CONSOLE, testOutput TESTOUTPUT, ref REF, avar AVAR | eff) Unit
main Effect Unit
main = runTest do
test "When the Aff never succeeds the returned Aff should fail" do
result ← attempt $ reattempt (Milliseconds 100.0) do
Expand All @@ -46,7 +43,7 @@ main = runTest do
assert "The returned Aff did not fail" $ Either.isLeft result

test "When the timeout will elapse before any attempts to run the Aff are successful the returned Aff should fail" do
seq ← makeVar $ failAffsForDurationAndNumberOfAttempts (Milliseconds 1000.0) 10
seq ← new $ failAffsForDurationAndNumberOfAttempts (Milliseconds 1000.0) 10
result ← attempt $ reattempt (Milliseconds 100.0) (affDouble seq)
assert "The returned Aff did not fail" $ Either.isLeft result

Expand All @@ -59,12 +56,12 @@ main = runTest do
suite "When the Aff will succeed during an attempt started before the timeout will elapse" do

test "The returned Aff should be successful" do
seq ← makeVar $ failAffsForDurationAndNumberOfAttempts (Milliseconds 100.0) 10
seq ← new $ failAffsForDurationAndNumberOfAttempts (Milliseconds 100.0) 10
result ← attempt $ reattempt (Milliseconds 100000000.0) (affDouble seq)
assert "The returned Aff failed" $ Either.isRight result

test "The returned Aff should not wait for the timeout to elapse in order to succeed" do
seq ← makeVar $ failAffsForDurationAndNumberOfAttempts (Milliseconds 100.0) 10
seq ← new $ failAffsForDurationAndNumberOfAttempts (Milliseconds 100.0) 10
let
parReattempt = parallel (reattempt (Milliseconds 100000000.0) (affDouble seq) $> true)
parLater = parallel do
Expand Down

0 comments on commit 1c223be

Please sign in to comment.