-
Notifications
You must be signed in to change notification settings - Fork 1
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 #250 from symbiont-io/onawait
feat(runtime): add await and make state more flexible
- Loading branch information
Showing
11 changed files
with
163 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
module StuntDouble (module X) where | ||
|
||
import StuntDouble.Actor as X | ||
import StuntDouble.Actor.State as X | ||
import StuntDouble.EventLoop as X | ||
import StuntDouble.Message as X | ||
import StuntDouble.EventLoop.Event as X | ||
import StuntDouble.EventLoop.Transport as X | ||
import StuntDouble.EventLoop.Transport.Http as X | ||
import StuntDouble.EventLoop.State as X | ||
import StuntDouble.Reference as X |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module StuntDouble.Actor.State where | ||
|
||
import Data.Aeson.Types | ||
import Data.HashMap.Strict (HashMap) | ||
import qualified Data.HashMap.Strict as HashMap | ||
import Data.Text (Text) | ||
|
||
import StuntDouble.Datatype | ||
|
||
------------------------------------------------------------------------ | ||
|
||
newtype State = State { getState :: HashMap Text Datatype } | ||
deriving Show | ||
|
||
initState :: State | ||
initState = State HashMap.empty | ||
|
||
withHashMap :: (HashMap Text Datatype -> HashMap Text Datatype) -> State -> State | ||
withHashMap f (State hm) = State (f hm) | ||
withHashMap _f _otherwise = error "withHashMap: impossible, invalid state." | ||
|
||
setField :: Text -> Datatype -> State -> State | ||
setField k v = withHashMap (HashMap.insert k v) | ||
|
||
getField :: Text -> State -> Datatype | ||
getField k (State hm) = hm HashMap.! k | ||
getField _k _otherwise = error "getField: impossible, invalid state." | ||
|
||
add :: Text -> Integer -> State -> State | ||
add k v = withHashMap (HashMap.alter (Just . maybe (Integer v) (plus (Integer v))) k) |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module StuntDouble.Datatype where | ||
|
||
import Data.Heap (Heap) | ||
import Data.Map (Map) | ||
import Data.Text | ||
|
||
------------------------------------------------------------------------ | ||
|
||
data Datatype | ||
= Unit () | ||
| Integer Integer | ||
| Double Double | ||
| Bool Bool | ||
| Text Text | ||
| Enum [Text] | ||
| Pair Datatype Datatype | ||
| Inl Datatype | ||
| Inr Datatype | ||
-- | Timestamp UTCTime | ||
| Map (Map Datatype Datatype) | ||
| List [Datatype] | ||
| Heap (Heap Datatype) | ||
deriving Show | ||
|
||
------------------------------------------------------------------------ | ||
|
||
plus :: Datatype -> Datatype -> Datatype | ||
plus (Integer i) (Integer j) = Integer (i + j) |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module StuntDouble.SchedulerTest where | ||
|
||
import Control.Concurrent.Async | ||
import Test.HUnit | ||
|
||
import StuntDouble | ||
|
||
------------------------------------------------------------------------ | ||
|
||
fakeExecutor :: IO () | ||
fakeExecutor = do | ||
let port = 3004 | ||
t <- httpTransport port | ||
e <- transportReceive t | ||
envelopeMessage e @?= envelopeMessage e -- XXX: check if cmd is of the right shape | ||
let resp = replyEnvelope e (Message "XXX: needs the right shape") | ||
transportSend t resp | ||
|
||
fakeScheduler :: RemoteRef -> Message -> Actor | ||
fakeScheduler executor (Message "step") = do | ||
cmd <- undefined -- popHeap | ||
a <- remoteCall executor cmd | ||
resp <- unsafeAwait (Left a) | ||
-- assert resp -- XXX: check if of the right shape | ||
now <- undefined -- get "time" from state | ||
seed <- undefined -- get "seed" | ||
arrivalTime <- undefined -- genArrivalTime now seed | ||
-- pushHeap arrivalTime resp | ||
return (Now (Message "stepped")) | ||
|
||
unit_scheduler :: Assertion | ||
unit_scheduler = do | ||
aExecutor <- async fakeExecutor | ||
elog <- emptyEventLog | ||
let ev = EventLoopName "scheduler" | ||
el <- makeEventLoop "/tmp" ev elog | ||
|
||
let executorRef = RemoteRef "http://localhost:3004" 0 | ||
lref <- spawn el (fakeScheduler executorRef) | ||
a <- send el (localToRemoteRef ev lref) (Message "step") | ||
reply <- wait a | ||
reply @?= Message "stepped" | ||
|
||
quit el | ||
cancel aExecutor |