Skip to content

Commit

Permalink
test(runtime): add test for send timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed May 28, 2021
1 parent e615232 commit ec2bd05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/runtime-prototype/src/StuntDouble/Time.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fakeTimeEpoch = do
v <- newTVarIO t0
return (Time (readTVarIO v), FakeTimeHandle v)

advanceFakeTime :: FakeTimeHandle -> IO ()
advanceFakeTime (FakeTimeHandle v) =
atomically (modifyTVar' v (Time.addUTCTime 1))
advanceFakeTime :: FakeTimeHandle -> Time.NominalDiffTime -> IO ()
advanceFakeTime (FakeTimeHandle v) seconds =
atomically (modifyTVar' v (Time.addUTCTime seconds))

-- XXX: move to test directory.
test :: IO ()
Expand All @@ -36,5 +36,5 @@ test = do
(time, h) <- fakeTime t
print =<< getCurrentTime time
print =<< getCurrentTime time
advanceFakeTime h
advanceFakeTime h 1
print =<< getCurrentTime time
37 changes: 30 additions & 7 deletions src/runtime-prototype/test/StuntDouble/ActorMapTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import StuntDouble.Time

------------------------------------------------------------------------

withEventLoop :: EventLoopName -> (EventLoop -> IO ()) -> IO ()
withEventLoop :: EventLoopName -> (EventLoop -> FakeTimeHandle -> IO ()) -> IO ()
withEventLoop name k = do
(time, h) <- fakeTimeEpoch
el <- makeEventLoop time (NamedPipe "/tmp") name
k el
k el h
quit el

eventLoopA :: String -> EventLoopName
Expand All @@ -36,15 +36,15 @@ testActor (Message "hi") = Actor (return (Message "bye!"))
------------------------------------------------------------------------

unit_actorMapInvoke :: Assertion
unit_actorMapInvoke = withEventLoop (eventLoopA "invoke") $ \el -> do
unit_actorMapInvoke = withEventLoop (eventLoopA "invoke") $ \el _h -> do
lref <- spawn el testActor emptyState
reply <- ainvoke el lref (Message "hi")
reply @?= Message "bye!"

unit_actorMapSend :: Assertion
unit_actorMapSend = do
let ev = eventLoopA "send"
withEventLoop ev $ \el -> do
withEventLoop ev $ \el _h -> do
lref <- spawn el testActor emptyState
let rref = localToRemoteRef ev lref
a <- asend el rref (Message "hi")
Expand Down Expand Up @@ -99,7 +99,7 @@ testActor3 (Message "go") = Actor $ do
return (Message "done")

unit_actorMapIO :: Assertion
unit_actorMapIO = withEventLoop (eventLoopA "io") $ \el -> do
unit_actorMapIO = withEventLoop (eventLoopA "io") $ \el _h -> do
lref <- spawn el testActor3 (stateFromList [("x", Integer 0)])
_done <- ainvoke el lref (Message "go")
threadDelay 100000
Expand All @@ -113,11 +113,34 @@ testActor4 (Message "go") = Actor $ do
return (Message "done")

unit_actorMapIOFail :: Assertion
unit_actorMapIOFail = withEventLoop (eventLoopA "io_fail") $ \el -> do
unit_actorMapIOFail = withEventLoop (eventLoopA "io_fail") $ \el _h -> do
lref <- spawn el testActor4 (stateFromList [("x", Integer 0)])
_done <- ainvoke el lref (Message "go")
threadDelay 100000
s <- getActorState el lref
s @?= stateFromList [("x", Integer 1)]

-- XXX: timeout tests
------------------------------------------------------------------------

testActor5 :: RemoteRef -> Message -> Actor
testActor5 rref (Message "go") = Actor $ do
p <- send rref (Message "hi")
on p (\(Left _exception) -> modify (add "x" 1))
return (Message "done")

unit_actorMapSendTimeout :: Assertion
unit_actorMapSendTimeout = do
let ev = eventLoopA "send_timeout"
withEventLoop ev $ \el h -> do
let rref = RemoteRef "doesnt_exist" 0
lref <- spawn el (testActor5 rref) (stateFromList [("x", Integer 0)])
_done <- ainvoke el lref (Message "go")
-- Timeout happens after 60 seconds.
advanceFakeTime h 59
threadDelay 100000
s <- getActorState el lref
s @?= stateFromList [("x", Integer 0)]
advanceFakeTime h 1
threadDelay 100000
s' <- getActorState el lref
s' @?= stateFromList [("x", Integer 1)]

0 comments on commit ec2bd05

Please sign in to comment.