Skip to content

Instantly share code, notes, and snippets.

@clouseauu
Created July 25, 2016 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clouseauu/de9688e86b57f7b0e864534de2af069a to your computer and use it in GitHub Desktop.
Save clouseauu/de9688e86b57f7b0e864534de2af069a to your computer and use it in GitHub Desktop.
ExActor - July 2016
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
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
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