Skip to content

Commit 9341c96

Browse files
committed
Handle child spec options in Chaotic workers/supervisors
1 parent a91d512 commit 9341c96

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

lib/chaos_spawn/chaotic/chaotic_supervisor.ex

+1-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@ defmodule ChaosSpawn.Chaotic.ChaoticSupervisor do
44
register with ChaosSpawn as a killable process.
55
66
"""
7-
alias Supervisor.Spec, as: OriginalSupervisor
87
alias ChaosSpawn.Chaotic.Supervisor.Wrapper
98

109
def supervisor(module, args, opts \\ []) do
11-
start_link_function = opts |> Keyword.get(:function, :start_link)
12-
id = opts |> Keyword.get(:id, module)
13-
OriginalSupervisor.supervisor(
14-
Wrapper,
15-
[module, start_link_function, args],
16-
id: id,
17-
function: :start_link_wrapper
18-
)
10+
Wrapper.child_spec(:supervisor, module, args, opts)
1911
end
2012
end

lib/chaos_spawn/chaotic/chaotic_worker.ex

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@ defmodule ChaosSpawn.Chaotic.ChaoticWorker do
33
Provides worker/2 and worker/3 that wrap around Supervisor.Spec's worker
44
functions and registered any spawned pids with ChaosSpawn's process killer.
55
"""
6-
alias Supervisor.Spec, as: OriginalSupervisor
76
alias ChaosSpawn.Chaotic.Supervisor.Wrapper
87

98
def worker(module, args, opts \\ []) do
10-
start_link_function = opts |> Keyword.get(:function, :start_link)
11-
id = opts |> Keyword.get(:id, module)
12-
OriginalSupervisor.worker(
13-
Wrapper,
14-
[module, start_link_function, args],
15-
id: id,
16-
function: :start_link_wrapper
17-
)
9+
Wrapper.child_spec(:worker, module, args, opts)
1810
end
19-
2011
end

lib/chaos_spawn/chaotic/supervisor/wrapper.ex

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ defmodule ChaosSpawn.Chaotic.Supervisor.Wrapper do
1919
|> register_unless_skipped(module, skipped)
2020
end
2121

22+
@doc false
23+
def child_spec(type, module, args, opts) do
24+
start_link_function = opts |> Keyword.get(:function, :start_link)
25+
args = [module, start_link_function, args]
26+
opts = opts
27+
|> Keyword.put(:function, :start_link_wrapper)
28+
|> Keyword.put_new(:id, module)
29+
|> Keyword.put_new(:modules, [module])
30+
apply(Supervisor.Spec, type, [__MODULE__, args, opts])
31+
end
32+
2233
defp register_unless_skipped(result, module, skipped_modules) do
2334
if (skip_module?(skipped_modules, module)) do
2435
result

test/chaos_spawn/chaotic/supervisor_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Chaotic.SupervisorTest do
1313
:permanent,
1414
:infinity,
1515
:supervisor,
16-
[Wrapper]
16+
[ModuleToCall]
1717
}
1818

1919
assert ChaoticSupervisor.supervisor(ModuleToCall, args) == expected

test/chaos_spawn/chaotic/worker_test.exs

+23-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Chaotic.WorkerTest do
1313
:permanent,
1414
5000,
1515
:worker,
16-
[Wrapper]
16+
[ModuleToCall]
1717
}
1818

1919
assert ChaoticWorker.worker(ModuleToCall, args) == expected
@@ -29,10 +29,31 @@ defmodule Chaotic.WorkerTest do
2929
:permanent,
3030
5000,
3131
:worker,
32-
[Wrapper]
32+
[ModuleToCall]
3333
}
3434

3535
worker = ChaoticWorker.worker(ModuleToCall, args, function: my_start_func)
3636
assert worker == expected
3737
end
38+
39+
test "worker/3 sets :id, :restart, :shutdown, :modules" do
40+
args = [:arg_one, :arg_two]
41+
42+
expected = {
43+
:special_id,
44+
{Wrapper, :start_link_wrapper, [ModuleToCall, :start_link, args]},
45+
:transient,
46+
:brutal_kill,
47+
:worker,
48+
:dynamic
49+
}
50+
51+
worker = ChaoticWorker.worker(ModuleToCall, args,
52+
id: :special_id,
53+
restart: :transient,
54+
shutdown: :brutal_kill,
55+
modules: :dynamic
56+
)
57+
assert worker == expected
58+
end
3859
end

0 commit comments

Comments
 (0)