Created
July 25, 2016 12:18
-
-
Save clouseauu/de9688e86b57f7b0e864534de2af069a to your computer and use it in GitHub Desktop.
ExActor - July 2016
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
defmodule CountActor do | |
use ExActor.GenServer, export: :counter | |
defstart start_link(state), do: initial_state(state) | |
defcall get, state: 2, do: reply(:two) | |
defcall get, state: state, do: reply(state) | |
defcast inc, state: state, do: new_state(state + 1) | |
defcast dec, state: state, do: new_state(state - 1) | |
end |
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
defmodule ExactorTestTest do | |
use ExUnit.Case | |
test "a basic actor" do | |
{ :ok, list } = ListActor.start_link | |
assert ListActor.get(list) == [] | |
:ok = ListActor.put list, :banana | |
assert ListActor.get(list) == [:banana] | |
:ok = ListActor.put list, :apple | |
assert ListActor.get(list) == [:banana, :apple] | |
:ok = ListActor.take list, :banana | |
assert ListActor.get(list) == [:apple] | |
end | |
test "a singleton" do | |
CountActor.start_link 1 | |
assert CountActor.get == 1 | |
CountActor.inc | |
assert CountActor.get == :two | |
CountActor.inc | |
CountActor.inc | |
CountActor.inc | |
assert CountActor.get == 5 | |
CountActor.dec | |
assert CountActor.get == 4 | |
end | |
end |
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
defmodule ListActor do | |
use ExActor.GenServer | |
defstart start_link, do: initial_state([]) | |
defcall get, state: state, do: reply(state) | |
defcast put(x), state: state, do: new_state(state ++ [x]) | |
defcast take(x), state: state, do: new_state(List.delete(state, x)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment