aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlthms <contact@thomasletan.fr>2017-12-29 20:25:30 +0000
committerThomas Letan <contact@thomasletan.fr>2018-01-24 08:11:28 +0100
commitdead0cae81d06884145e697f80ac469f3325c02c (patch)
tree0df6d65d011bc3aad9dd61e9d04ca5bdf6cebba3
parentinstance: Keep track of the currently registered puppets (diff)
puppeteer: Add stop/2 and stop/1 to kill a given Puppeteer
-rw-r--r--lib/lkn/core/puppeteer.ex15
-rw-r--r--test/core_test.exs17
2 files changed, 29 insertions, 3 deletions
diff --git a/lib/lkn/core/puppeteer.ex b/lib/lkn/core/puppeteer.ex
index 9150c2a..6433b49 100644
--- a/lib/lkn/core/puppeteer.ex
+++ b/lib/lkn/core/puppeteer.ex
@@ -164,6 +164,10 @@ defmodule Lkn.Core.Puppeteer do
end
unquote(plugin_clients)
+
+ def terminate(reason, state) do
+ destroy(state.puppeteer_key, state.state, state.instance_key, reason)
+ end
end
end
end
@@ -176,9 +180,10 @@ defmodule Lkn.Core.Puppeteer do
end
@callback init_state(init_args) :: {:ok, state}|:error
- @callback leave_instance(state, Instance.k) :: state
- @callback puppet_enter(state, Lkn.Core.Instance.k, Lkn.Core.Puppet.k, Lkn.Core.Entity.digest) :: state
- @callback puppet_leave(state, Lkn.Core.Instance.k, Lkn.Core.Puppet.k) :: state
+ @callback leave_instance(s :: state, instance_key :: Lkn.Core.Instance.k) :: state
+ @callback puppet_enter(s :: state, instance_key :: Lkn.Core.Instance.k, puppet_key :: Lkn.Core.Puppet.k, digest :: Lkn.Core.Entity.digest) :: state
+ @callback puppet_leave(s :: state, instance_key :: Lkn.Core.Instance.k, puppet_key :: Lkn.Core.Puppet.k) :: state
+ @callback destroy(puppeteer_key :: k, s :: state, instance_key :: Option.t(Lkn.Core.Instance.k), reason :: any) :: term
@spec leave_instance(k, Instance.k) :: :ok
def leave_instance(puppeteer_key, instance_key) do
@@ -197,4 +202,8 @@ defmodule Lkn.Core.Puppeteer do
def puppet_leave(puppeteer_key, instance_key, puppet_key) do
GenServer.cast(Name.puppeteer(puppeteer_key), {:puppet_leave, instance_key, puppet_key})
end
+
+ def stop(puppeteer_key, reason \\ :normal) do
+ GenServer.stop(Name.puppeteer(puppeteer_key), reason)
+ end
end
diff --git a/test/core_test.exs b/test/core_test.exs
index 74ae4cb..4949bda 100644
--- a/test/core_test.exs
+++ b/test/core_test.exs
@@ -80,6 +80,19 @@ defmodule Lkn.Core.Test do
Option.some(4) = Lkn.Core.Entity.read(entity_key, :level)
end
+ test "spawning puppeteer just to stop it" do
+ puppeteer_key = UUID.uuid4()
+
+ {:ok, _} = Test.Puppeteer.start_link(puppeteer_key)
+
+ Lkn.Core.Puppeteer.stop(puppeteer_key)
+
+ receive do
+ {:destroyed, key} -> assert puppeteer_key == key
+ after 100 -> raise "should have received a destroy notification already"
+ end
+ end
+
test "spawning puppeteer and testing its private cast" do
puppeteer_key = UUID.uuid4()
@@ -497,6 +510,10 @@ defmodule Test.Puppeteer do
state
end
+ def destroy(puppeteer_key, state, _instance_key, _reason) do
+ send state, {:destroyed, puppeteer_key}
+ end
+
def leave_instance(target, _instance_key) do
send(target, :kick)
target