-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from id3as/master
receive type class and show instances
- Loading branch information
Showing
5 changed files
with
96 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,110 @@ | ||
module Test.Process where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Free (Free) | ||
import Data.Either (Either(..), isLeft) | ||
import Data.Time.Duration (Milliseconds(..)) | ||
import Effect.Class (liftEffect) | ||
import Effect.Exception (throw) | ||
import Erl.Process (ExitReason, ProcessM, receive, receiveWithTrap, self, spawn, spawnLink, trapExit, (!)) | ||
import Erl.Process (ExitReason, ProcessM, receive, receiveWithTimeout, self, spawn, spawnLink, trapExit, (!)) | ||
import Erl.Process.Raw as Raw | ||
import Erl.Test.EUnit (TestF, suite, test) | ||
import Foreign as Foreign | ||
import Test.Assert (assertTrue) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
data Foo = Foo Int | Blah String | Whatever Boolean Number | ||
data Foo | ||
= Foo Int | ||
| Blah String | ||
| Whatever Boolean Number | ||
derive instance eqFoo :: Eq Foo | ||
|
||
tests :: Free TestF Unit | ||
tests = | ||
tests = | ||
suite "process tests" do | ||
-- Use raw process communication to talk to the test process as it is not a typed Process | ||
test "send stuff to spawned process" do | ||
parent <- Raw.self | ||
-- We can also do this inline or infer the types | ||
let proc :: ProcessM Int Unit | ||
proc = do | ||
a :: Int <- receive | ||
b :: Int <- receive | ||
liftEffect $ parent `Raw.send` (a == 1 && b == 2) | ||
let | ||
proc :: ProcessM Int Unit | ||
proc = do | ||
a :: Int <- receive | ||
b :: Int <- receive | ||
liftEffect $ parent `Raw.send` (a == 1 && b == 2) | ||
p <- spawn proc | ||
p ! 1 | ||
p ! 2 | ||
Raw.receive >>= assertTrue | ||
|
||
test "send stuff to spawned process, another type" do | ||
parent <- Raw.self | ||
p <- spawn do | ||
a <- receive | ||
b <- receive | ||
liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) | ||
p <- | ||
spawn do | ||
a <- receive | ||
b <- receive | ||
liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) | ||
p ! Foo 42 | ||
p ! Whatever true 1.0 | ||
Raw.receive >>= assertTrue | ||
|
||
test "sending tospawnLinked" do | ||
parent <- Raw.self | ||
p <- spawnLink do | ||
a <- receive | ||
b <- receive | ||
liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) | ||
p <- | ||
spawnLink do | ||
a <- receive | ||
b <- receive | ||
liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) | ||
p ! Foo 42 | ||
p ! Whatever true 1.0 | ||
Raw.receive >>= assertTrue | ||
|
||
test "receive timeout" do | ||
parent <- Raw.self | ||
_p <- | ||
spawnLink do | ||
a <- receiveWithTimeout (Milliseconds 10.0) "default" | ||
liftEffect $ parent `Raw.send` (a == "default") | ||
Raw.receive >>= assertTrue | ||
test "self eq" do | ||
parent <- Raw.self | ||
p <- spawnLink do | ||
child <- self | ||
liftEffect $ parent `Raw.send` child | ||
p <- | ||
spawnLink do | ||
child <- self | ||
liftEffect $ parent `Raw.send` child | ||
p' <- Raw.receive | ||
assertTrue $ p == p' | ||
|
||
test "trapExit" do | ||
testPid <- Raw.self | ||
void $ spawnLink do | ||
parent <- self | ||
|
||
trapExit do | ||
_ <- liftEffect $ spawnLink do | ||
liftEffect $ parent ! 1 | ||
liftEffect $ Raw.exit (Foreign.unsafeToForeign true) | ||
pure unit | ||
|
||
first <- receiveWithTrap | ||
liftEffect $ case (unsafeCoerce first) :: Either ExitReason Int of | ||
Right 1 -> pure unit | ||
_other -> do | ||
throw "failed recv" | ||
second <- receiveWithTrap | ||
liftEffect $ testPid `Raw.send` (isLeft second) | ||
void | ||
$ spawnLink do | ||
parent <- self | ||
trapExit do | ||
_ <- | ||
liftEffect | ||
$ spawnLink do | ||
liftEffect $ parent ! 1 | ||
liftEffect $ Raw.exit (Foreign.unsafeToForeign true) | ||
pure unit | ||
first <- receive | ||
liftEffect | ||
$ case (unsafeCoerce first) :: Either ExitReason Int of | ||
Right 1 -> pure unit | ||
_other -> do | ||
throw "failed recv" | ||
second <- receive | ||
liftEffect $ testPid `Raw.send` (isLeft second) | ||
Raw.receive >>= assertTrue | ||
test "receive with trap timeout" do | ||
testPid <- Raw.self | ||
void | ||
$ spawnLink do | ||
trapExit do | ||
_ <- | ||
liftEffect | ||
$ spawnLink do | ||
_ <- receive | ||
pure unit | ||
a <- receiveWithTimeout (Milliseconds 100.0) "default" | ||
liftEffect | ||
$ case a of | ||
Right "default" -> testPid `Raw.send` true | ||
_ -> testPid `Raw.send` false | ||
Raw.receive >>= assertTrue |