Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from am-kantox/feature/#7-multicall
Browse files Browse the repository at this point in the history
`multi_call/3` → closes #7
  • Loading branch information
am-kantox committed Aug 4, 2022
2 parents 00b6bd1 + 9b92bc9 commit 8258081
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/siblings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ defmodule Siblings do
|> InternalWorker.call(message)
end

@doc """
Performs a `GenServer.call/3` on all the workers.
"""
@spec multi_call(module(), Worker.message()) ::
[Worker.call_result() | {:error, :callback_not_implemented}]
def multi_call(name \\ default_fqn(), message) do
:pids
|> children(name)
|> Task.async_stream(&InternalWorker.call(&1, message))
|> Enum.map(&elem(&1, 1))
end

@doc """
Resets the the named worker’s interval.
"""
Expand Down
40 changes: 39 additions & 1 deletion test/siblings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule SiblingsTest do

setup do
%{siblings: start_supervised!(Siblings)}
# on_exit(fn -> Process.sleep(100) end)
end

test "Worker with FSM" do
Expand Down Expand Up @@ -54,7 +55,7 @@ defmodule SiblingsTest do
assert [] == Siblings.children()
end

test "Multi" do
test "#multi_transition/3" do
{:ok, _pid1} =
Siblings.start_child(Siblings.Test.NoPerform, "NoPerform1", %{pid: self()}, interval: 60_000)

Expand All @@ -76,4 +77,41 @@ defmodule SiblingsTest do

assert [] == Siblings.children()
end

test "#call/3" do
{:ok, _pid1} =
Siblings.start_child(Siblings.Test.NoPerform, "Callable1", %{pid: self()}, interval: 60_000)

{:ok, _pid2} =
Siblings.start_child(Siblings.Test.Callable, "Callable2", %{pid: self()}, interval: 60_000)

assert {:error, :callback_not_implemented} == Siblings.call("Callable1", 42)
assert 84 == Siblings.call("Callable2", 42)

Siblings.multi_transition(:to_s2, nil)
Siblings.multi_transition(:to_s3, nil)
Siblings.multi_transition(:__end__, nil)

Process.sleep(100)

assert [] == Siblings.children()
end

test "#multi_call/2" do
{:ok, _pid1} =
Siblings.start_child(Siblings.Test.NoPerform, "Callable1", %{pid: self()}, interval: 60_000)

{:ok, _pid2} =
Siblings.start_child(Siblings.Test.Callable, "Callable2", %{pid: self()}, interval: 60_000)

assert [84, {:error, :callback_not_implemented}] == Enum.sort(Siblings.multi_call(42))

Siblings.multi_transition(:to_s2, nil)
Siblings.multi_transition(:to_s3, nil)
Siblings.multi_transition(:__end__, nil)

Process.sleep(100)

assert [] == Siblings.children()
end
end
21 changes: 21 additions & 0 deletions test/support/callable.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Siblings.Test.Callable do
@moduledoc false

require Logger

@behaviour Siblings.Worker

@impl Siblings.Worker
def perform(_, _, _), do: :noop

@impl Siblings.Worker
def on_call(message, state) when is_integer(message),
do: {message * 2, state}

@fsm """
s1 --> |to_s2| s2
s2 --> |to_s3| s3
"""

use Finitomata, @fsm
end

0 comments on commit 8258081

Please sign in to comment.