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 #19 from slamdata/aff-4
Browse files Browse the repository at this point in the history
bump to aff4
  • Loading branch information
kritzcreek authored Jan 24, 2018
2 parents 5408a7e + 8d2e0b6 commit 4073694
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/bower_components/
/node_modules/
/output/
/package-lock.json
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"package.json"
],
"dependencies": {
"purescript-aff": "^3.0.0",
"purescript-aff": "^4.0.2",
"purescript-refs": "^3.0.0"
},
"devDependencies": {
"purescript-test-unit": "^11.0.0",
"purescript-parallel": "^3.0.0"
"purescript-test-unit": "^13.0.0",
"purescript-parallel": "^3.3.1"
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build --censor-lib --strict",
"build": "pulp build -- --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript": "^0.11.4",
"purescript-psa": "^0.5.1",
"rimraf": "^2.6.1"
"pulp": "^12.0.1",
"purescript": "^0.11.7",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
}
16 changes: 6 additions & 10 deletions src/Control/Monad/Aff/Reattempt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ module Control.Monad.Aff.Reattempt where

import Prelude

import Control.Monad.Aff (Aff, forkAff, delay, cancel)
import Control.Monad.Eff.Ref (newRef, readRef, writeRef, REF)
import Control.Monad.Aff (Aff, delay, forkAff, supervise)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Ref (newRef, readRef, writeRef, REF)
import Control.Monad.Error.Class (catchError, throwError)
import Control.Monad.Eff.Exception (Error, error)
import Data.Time.Duration (Milliseconds)

-- | `reattempt` repeatedly attempts to run the provided `Aff` until either an attempt
Expand All @@ -20,17 +19,14 @@ import Data.Time.Duration (Milliseconds)
-- | will succeed. When no attempts succeed the `Aff` returned by `reattempt` will fail
-- | with the `Error` raised by the last attempt.
reattempt e a. Milliseconds Aff (ref REF | e) a Aff (ref REF | e) a
reattempt ms aff = do
reattempt ms aff = supervise do
elapsed ← liftEff $ newRef false
forkedTimeout ← forkAff do
_ ← forkAff do
delay ms
liftEff $ writeRef elapsed true
let attempt = aff `catchError` \error → do
shouldRethrow ← liftEff $ readRef elapsed
if shouldRethrow
then throwError (error Error)
then throwError error
else attempt
result ← attempt
-- Process continues after returned aff succeeds if forked timeout isn't cancelled
_ ← cancel forkedTimeout (error "")
pure result
attempt
17 changes: 9 additions & 8 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Test.Main where

import Prelude

import Control.Alt ((<|>))
import Control.Monad.Aff (attempt, delay, Aff)
import Control.Monad.Aff.AVar (makeVar', takeVar, putVar, AVar, AVAR)
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)
import Control.Parallel.Class (parallel, sequential)
import Control.Plus (empty)
import Data.Array (head, tail)
import Data.Either as Either
Expand All @@ -33,7 +34,7 @@ failAffsForDurationAndNumberOfAttempts timeout attemptCount = seq
affDouble e. AVar (Array (Aff (avar AVAR | e) Unit)) Aff (avar AVAR | e) Unit
affDouble affsVar = do
affs ← takeVar affsVar
putVar affsVar (fromMaybe [] (tail affs))
putVar (fromMaybe [] (tail affs)) affsVar
maybe (pure unit) id $ head affs

main eff. Eff (console CONSOLE, testOutput TESTOUTPUT, ref REF, avar AVAR | eff) Unit
Expand All @@ -45,7 +46,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 ← makeVar $ 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 @@ -58,18 +59,18 @@ 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 ← makeVar $ 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 ← makeVar $ failAffsForDurationAndNumberOfAttempts (Milliseconds 100.0) 10
let
parReattempt = parallel (reattempt (Milliseconds 100000000.0) (affDouble seq) $> true)
parLater = parallel do
delay (Milliseconds 1000.0)
pure false
result ← unwrap $ parReattempt <|> parLater
result ← sequential $ parReattempt <|> parLater
assert "The returned Aff failed" result

-- TODO: Test that process finishes when attempt is successful.
Expand Down

0 comments on commit 4073694

Please sign in to comment.