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 #21 from rbobin/master
Browse files Browse the repository at this point in the history
Update for PureScript 0.12
  • Loading branch information
cryogenian authored Jun 26, 2018
2 parents 4073694 + 1c223be commit 073c39c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
dist: trusty
sudo: required
node_js: 6
node_js: 9
install:
- npm install -g bower
- npm install
Expand Down
8 changes: 4 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"package.json"
],
"dependencies": {
"purescript-aff": "^4.0.2",
"purescript-refs": "^3.0.0"
"purescript-aff": "^5.0.0",
"purescript-refs": "^4.1.0"
},
"devDependencies": {
"purescript-test-unit": "^13.0.0",
"purescript-parallel": "^3.3.1"
"purescript-test-unit": "^14.0.0",
"purescript-parallel": "^4.0.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test": "pulp test"
},
"devDependencies": {
"pulp": "^12.0.1",
"purescript": "^0.11.7",
"pulp": "^12.3.0",
"purescript": "^0.12.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Control.Monad.Aff.Reattempt where
module Effect.Aff.Reattempt where

import Prelude

import Control.Monad.Aff (Aff, delay, forkAff, supervise)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Ref (newRef, readRef, writeRef, REF)
import Effect.Aff (Aff, delay, forkAff, supervise)
import Effect.Class (liftEffect)
import Effect.Ref (new, read, write)
import Control.Monad.Error.Class (catchError, throwError)
import Data.Time.Duration (Milliseconds)

Expand All @@ -18,14 +18,14 @@ import Data.Time.Duration (Milliseconds)
-- | When an attempt to run the provided `Aff` succeeds the `Aff` returned by `reattempt`
-- | 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 a. Milliseconds Aff a Aff a
reattempt ms aff = supervise do
elapsed ← liftEff $ newRef false
elapsed ← liftEffect $ new false
_ ← forkAff do
delay ms
liftEff $ writeRef elapsed true
liftEffect $ write true elapsed
let attempt = aff `catchError` \error → do
shouldRethrow ← liftEff $ readRef elapsed
shouldRethrow ← liftEffect $ read elapsed
if shouldRethrow
then throwError error
else attempt
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 073c39c

Please sign in to comment.