|
1 | 1 | module Test.Process where |
2 | 2 |
|
3 | 3 | import Prelude |
4 | | - |
5 | 4 | import Control.Monad.Free (Free) |
6 | 5 | import Data.Either (Either(..), isLeft) |
| 6 | +import Data.Time.Duration (Milliseconds(..)) |
7 | 7 | import Effect.Class (liftEffect) |
8 | 8 | import Effect.Exception (throw) |
9 | | -import Erl.Process (ExitReason, ProcessM, receive, receiveWithTrap, self, spawn, spawnLink, trapExit, (!)) |
| 9 | +import Erl.Process (ExitReason, ProcessM, receive, receiveWithTimeout, self, spawn, spawnLink, trapExit, (!)) |
10 | 10 | import Erl.Process.Raw as Raw |
11 | 11 | import Erl.Test.EUnit (TestF, suite, test) |
12 | 12 | import Foreign as Foreign |
13 | 13 | import Test.Assert (assertTrue) |
14 | 14 | import Unsafe.Coerce (unsafeCoerce) |
15 | 15 |
|
16 | | -data Foo = Foo Int | Blah String | Whatever Boolean Number |
| 16 | +data Foo |
| 17 | + = Foo Int |
| 18 | + | Blah String |
| 19 | + | Whatever Boolean Number |
17 | 20 | derive instance eqFoo :: Eq Foo |
18 | 21 |
|
19 | 22 | tests :: Free TestF Unit |
20 | | -tests = |
| 23 | +tests = |
21 | 24 | suite "process tests" do |
22 | 25 | -- Use raw process communication to talk to the test process as it is not a typed Process |
23 | 26 | test "send stuff to spawned process" do |
24 | 27 | parent <- Raw.self |
25 | 28 | -- We can also do this inline or infer the types |
26 | | - let proc :: ProcessM Int Unit |
27 | | - proc = do |
28 | | - a :: Int <- receive |
29 | | - b :: Int <- receive |
30 | | - liftEffect $ parent `Raw.send` (a == 1 && b == 2) |
| 29 | + let |
| 30 | + proc :: ProcessM Int Unit |
| 31 | + proc = do |
| 32 | + a :: Int <- receive |
| 33 | + b :: Int <- receive |
| 34 | + liftEffect $ parent `Raw.send` (a == 1 && b == 2) |
31 | 35 | p <- spawn proc |
32 | 36 | p ! 1 |
33 | 37 | p ! 2 |
34 | 38 | Raw.receive >>= assertTrue |
35 | | - |
36 | 39 | test "send stuff to spawned process, another type" do |
37 | 40 | parent <- Raw.self |
38 | | - p <- spawn do |
39 | | - a <- receive |
40 | | - b <- receive |
41 | | - liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) |
| 41 | + p <- |
| 42 | + spawn do |
| 43 | + a <- receive |
| 44 | + b <- receive |
| 45 | + liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) |
42 | 46 | p ! Foo 42 |
43 | 47 | p ! Whatever true 1.0 |
44 | 48 | Raw.receive >>= assertTrue |
45 | | - |
46 | 49 | test "sending tospawnLinked" do |
47 | 50 | parent <- Raw.self |
48 | | - p <- spawnLink do |
49 | | - a <- receive |
50 | | - b <- receive |
51 | | - liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) |
| 51 | + p <- |
| 52 | + spawnLink do |
| 53 | + a <- receive |
| 54 | + b <- receive |
| 55 | + liftEffect $ parent `Raw.send` (a == Foo 42 && b == Whatever true 1.0) |
52 | 56 | p ! Foo 42 |
53 | 57 | p ! Whatever true 1.0 |
54 | 58 | Raw.receive >>= assertTrue |
55 | | - |
| 59 | + test "receive timeout" do |
| 60 | + parent <- Raw.self |
| 61 | + _p <- |
| 62 | + spawnLink do |
| 63 | + a <- receiveWithTimeout (Milliseconds 10.0) "default" |
| 64 | + liftEffect $ parent `Raw.send` (a == "default") |
| 65 | + Raw.receive >>= assertTrue |
56 | 66 | test "self eq" do |
57 | 67 | parent <- Raw.self |
58 | | - p <- spawnLink do |
59 | | - child <- self |
60 | | - liftEffect $ parent `Raw.send` child |
| 68 | + p <- |
| 69 | + spawnLink do |
| 70 | + child <- self |
| 71 | + liftEffect $ parent `Raw.send` child |
61 | 72 | p' <- Raw.receive |
62 | 73 | assertTrue $ p == p' |
63 | | - |
64 | 74 | test "trapExit" do |
65 | 75 | testPid <- Raw.self |
66 | | - void $ spawnLink do |
67 | | - parent <- self |
68 | | - |
69 | | - trapExit do |
70 | | - _ <- liftEffect $ spawnLink do |
71 | | - liftEffect $ parent ! 1 |
72 | | - liftEffect $ Raw.exit (Foreign.unsafeToForeign true) |
73 | | - pure unit |
74 | | - |
75 | | - first <- receiveWithTrap |
76 | | - liftEffect $ case (unsafeCoerce first) :: Either ExitReason Int of |
77 | | - Right 1 -> pure unit |
78 | | - _other -> do |
79 | | - throw "failed recv" |
80 | | - second <- receiveWithTrap |
81 | | - liftEffect $ testPid `Raw.send` (isLeft second) |
| 76 | + void |
| 77 | + $ spawnLink do |
| 78 | + parent <- self |
| 79 | + trapExit do |
| 80 | + _ <- |
| 81 | + liftEffect |
| 82 | + $ spawnLink do |
| 83 | + liftEffect $ parent ! 1 |
| 84 | + liftEffect $ Raw.exit (Foreign.unsafeToForeign true) |
| 85 | + pure unit |
| 86 | + first <- receive |
| 87 | + liftEffect |
| 88 | + $ case (unsafeCoerce first) :: Either ExitReason Int of |
| 89 | + Right 1 -> pure unit |
| 90 | + _other -> do |
| 91 | + throw "failed recv" |
| 92 | + second <- receive |
| 93 | + liftEffect $ testPid `Raw.send` (isLeft second) |
| 94 | + Raw.receive >>= assertTrue |
| 95 | + test "receive with trap timeout" do |
| 96 | + testPid <- Raw.self |
| 97 | + void |
| 98 | + $ spawnLink do |
| 99 | + trapExit do |
| 100 | + _ <- |
| 101 | + liftEffect |
| 102 | + $ spawnLink do |
| 103 | + _ <- receive |
| 104 | + pure unit |
| 105 | + a <- receiveWithTimeout (Milliseconds 100.0) "default" |
| 106 | + liftEffect |
| 107 | + $ case a of |
| 108 | + Right "default" -> testPid `Raw.send` true |
| 109 | + _ -> testPid `Raw.send` false |
82 | 110 | Raw.receive >>= assertTrue |
0 commit comments