Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit 5824399

Browse files
committed
Carry the input message correctly
1 parent 6875c24 commit 5824399

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/Control/Distributed/Process/FSM/Client.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ call pid msg = bracket (monitor pid) unmonitor $ \mRef -> do
8181
send pid (wrapMessage msg, sp)
8282
msg' <- receiveWait [ matchChan rp return
8383
, matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
84-
(\_ -> die $ ExitOther "ServerUnreachable")
84+
(\(ProcessMonitorNotification _ _ r) -> die $ ExitOther (show r))
8585
] :: Process Message
8686
mR <- unwrapMessage msg'
8787
case mR of

src/Control/Distributed/Process/FSM/Internal/Process.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ handleRpcRawInputs :: forall s d . (Show s) => State s d
9191
-> (P.Message, SendPort P.Message)
9292
-> Action (State s d)
9393
handleRpcRawInputs st@State{..} (msg, port) =
94-
handleInput msg $ st { stReply = (sendChan port), stTrans = Q.empty }
94+
handleInput msg $ st { stReply = (sendChan port), stTrans = Q.empty, stInput = Just msg }
9595

9696
handleAllRawInputs :: forall s d. (Show s) => State s d
9797
-> P.Message
9898
-> Action (State s d)
9999
handleAllRawInputs st@State{..} msg =
100-
handleInput msg $ st { stReply = noOp, stTrans = Q.empty }
100+
handleInput msg $ st { stReply = noOp, stTrans = Q.empty, stInput = Just msg }
101101

102102
noOp :: P.Message -> Process ()
103103
noOp = const $ return ()

src/Control/Distributed/Process/FSM/Internal/Types.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ currentState = ST.get >>= return . stName
154154
stateData :: FSM s d d
155155
stateData = ST.get >>= return . stData
156156

157+
currentMessage :: forall s d . FSM s d P.Message
158+
currentMessage = ST.get >>= return . fromJust . stInput
159+
160+
currentInput :: forall s d m . (Serializable m) => FSM s d (Maybe m)
161+
currentInput = currentMessage >>= \m -> lift (unwrapMessage m :: Process (Maybe m))
162+
157163
addTransition :: Transition s d -> FSM s d ()
158164
addTransition t = ST.modify (\s -> fromJust $ enqueue s (Just t) )
159165

tests/TestFSM.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import qualified Control.Distributed.Process.Extras (__remoteTable)
1515
import Control.Distributed.Process.Extras.Time hiding (timeout)
1616
import Control.Distributed.Process.Extras.Timer
1717
import Control.Distributed.Process.FSM
18-
import Control.Distributed.Process.FSM.Client (call)
18+
import Control.Distributed.Process.FSM.Client (call, callTimeout)
1919
import Control.Distributed.Process.FSM.Internal.Process
2020
import Control.Distributed.Process.FSM.Internal.Types hiding (State, liftIO)
2121
import Control.Distributed.Process.SysTest.Utils
@@ -37,6 +37,7 @@ import qualified Network.Transport as NT
3737
-- import Control.Distributed.Process.Serializable (Serializable)
3838
-- import Control.Monad (void)
3939
import Data.Binary (Binary)
40+
import Data.Maybe (fromJust)
4041
import Data.Typeable (Typeable)
4142
import GHC.Generics
4243

@@ -104,8 +105,11 @@ deepFSM :: SendPort () -> SendPort () -> Step State ()
104105
deepFSM on off = initState Off ()
105106
^. ((event :: Event State) ~> (allState $ \s -> enter s))
106107
.| ( (Off ~@ resume)
107-
|> ((event :: Event ())
108-
~> (allState $ \s -> (lift $ sendChan off s) >> resume))
108+
|> ( ((event :: Event ())
109+
~> (allState $ \s -> (lift $ sendChan off s) >> resume))
110+
.| (((event :: Event String) ~> (always $ \(_ :: String) -> resume))
111+
|> (reply (currentInput >>= return . fromJust :: FSM State () String)))
112+
)
109113
)
110114
.| ( (On ~@ resume)
111115
|> ((event :: Event ())
@@ -129,11 +133,17 @@ verifyOuterStateHandler = do
129133
Nothing <- receiveChanTimeout (asTimeout $ seconds 3) rpOff
130134
() <- receiveChan rpOn
131135

136+
resp <- callTimeout pid "hello there" (seconds 3):: Process (Maybe String)
137+
resp `shouldBe` equalTo (Nothing :: Maybe String)
138+
132139
send pid Off
133140
send pid ()
134141
Nothing <- receiveChanTimeout (asTimeout $ seconds 3) rpOn
135142
() <- receiveChan rpOff
136143

144+
res <- call pid "hello" :: Process String
145+
res `shouldBe` equalTo "hello"
146+
137147
kill pid "bye bye"
138148

139149
verifyMailboxHandling :: Process ()

0 commit comments

Comments
 (0)