2020-01-08 10:18:38 +13:00
|
|
|
defmodule WaferDriverElixirALE.GPIO.DispatcherTest do
|
2019-12-30 13:12:42 +13:00
|
|
|
use ExUnit.Case, async: true
|
2020-01-08 10:18:38 +13:00
|
|
|
alias Wafer.Driver.ElixirALE.GPIO.Dispatcher, as: Dispatcher
|
2020-01-08 11:31:15 +13:00
|
|
|
alias Wafer.Driver.ElixirALE.GPIO.Wrapper
|
2020-01-06 13:48:16 +13:00
|
|
|
alias Wafer.InterruptRegistry, as: IR
|
2019-12-30 13:12:42 +13:00
|
|
|
import Mimic
|
|
|
|
@moduledoc false
|
|
|
|
|
2020-01-08 15:16:21 +13:00
|
|
|
setup do
|
|
|
|
Supervisor.terminate_child(Wafer.Supervisor, IR)
|
|
|
|
Supervisor.restart_child(Wafer.Supervisor, IR)
|
|
|
|
{:ok, []}
|
|
|
|
end
|
|
|
|
|
2019-12-30 13:12:42 +13:00
|
|
|
describe "handle_call/3" do
|
|
|
|
test "enabling rising interrupts" do
|
|
|
|
conn = conn()
|
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2020-01-06 13:48:16 +13:00
|
|
|
|> expect(:set_int, 1, fn pid, pin_condition ->
|
2019-12-30 13:12:42 +13:00
|
|
|
assert pid == conn.pid
|
2020-01-06 13:48:16 +13:00
|
|
|
assert pin_condition == :rising
|
2019-12-30 13:12:42 +13:00
|
|
|
:ok
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :rising, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
assert IR.count_subscriptions({Dispatcher, 1}, :rising) == 1
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enabling falling interrupts" do
|
|
|
|
conn = conn()
|
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2020-01-06 13:48:16 +13:00
|
|
|
|> expect(:set_int, 1, fn pid, pin_condition ->
|
2019-12-30 13:12:42 +13:00
|
|
|
assert pid == conn.pid
|
2020-01-06 13:48:16 +13:00
|
|
|
assert pin_condition == :falling
|
2019-12-30 13:12:42 +13:00
|
|
|
:ok
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :falling, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
assert IR.count_subscriptions({Dispatcher, 1}, :falling) == 1
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "enabling both interrupts" do
|
|
|
|
conn = conn()
|
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2020-01-06 13:48:16 +13:00
|
|
|
|> expect(:set_int, 1, fn pid, pin_condition ->
|
2019-12-30 13:12:42 +13:00
|
|
|
assert pid == conn.pid
|
2020-01-06 13:48:16 +13:00
|
|
|
assert pin_condition == :both
|
2019-12-30 13:12:42 +13:00
|
|
|
:ok
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :both, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
assert IR.count_subscriptions({Dispatcher, 1}, :both) == 1
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "disabling rising interrupts" do
|
|
|
|
conn = conn()
|
2019-12-30 20:37:34 +13:00
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2019-12-30 20:37:34 +13:00
|
|
|
|> stub(:set_int, fn _, _ -> :ok end)
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :rising, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
|
|
|
Dispatcher.handle_call({:disable, conn, :rising}, nil, state())
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
refute IR.subscribers?({Dispatcher, 1}, :rising)
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "disabling falling interrupts" do
|
|
|
|
conn = conn()
|
2019-12-30 20:37:34 +13:00
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2019-12-30 20:37:34 +13:00
|
|
|
|> stub(:set_int, fn _, _ -> :ok end)
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :falling, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
|
|
|
Dispatcher.handle_call({:disable, conn, :falling}, nil, state())
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
refute IR.subscribers?({Dispatcher, 1}, :falling)
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "disabling both interrupts" do
|
|
|
|
conn = conn()
|
2019-12-30 20:37:34 +13:00
|
|
|
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2019-12-30 20:37:34 +13:00
|
|
|
|> stub(:set_int, fn _, _ -> :ok end)
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn, :both, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
|
|
|
assert {:reply, {:ok, conn}, _state} =
|
|
|
|
Dispatcher.handle_call({:disable, conn, :both}, nil, state())
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
refute IR.subscribers?({Dispatcher, 1}, :both)
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "handle_info/2" do
|
|
|
|
test "publishing rising interrupts" do
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2019-12-30 20:37:34 +13:00
|
|
|
|> stub(:set_int, fn _, _ -> :ok end)
|
|
|
|
|
2019-12-30 13:12:42 +13:00
|
|
|
{:reply, {:ok, conn}, state} =
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call({:enable, conn(), :both, :metadata, self()}, nil, state())
|
2019-12-30 13:12:42 +13:00
|
|
|
|
|
|
|
{:noreply, _state} = Dispatcher.handle_info({:gpio_interrupt, 1, :rising}, state)
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
assert_received {:interrupt, ^conn, :rising, :metadata}
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "publishing falling interrupts" do
|
2020-01-08 10:18:38 +13:00
|
|
|
Wrapper
|
2019-12-30 20:37:34 +13:00
|
|
|
|> stub(:set_int, fn _, _ -> :ok end)
|
|
|
|
|
2019-12-30 13:12:42 +13:00
|
|
|
{:reply, {:ok, conn}, state} =
|
2020-01-06 13:48:16 +13:00
|
|
|
Dispatcher.handle_call(
|
|
|
|
{:enable, conn(), :both, :metadata, self()},
|
|
|
|
nil,
|
|
|
|
state()
|
|
|
|
)
|
2019-12-30 13:12:42 +13:00
|
|
|
|
|
|
|
{:noreply, _state} = Dispatcher.handle_info({:gpio_interrupt, 1, :falling}, state)
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
assert_received {:interrupt, ^conn, :falling, :metadata}
|
2019-12-30 13:12:42 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp conn(opts \\ []), do: Enum.into(opts, %{pin: pin(), pid: self()})
|
|
|
|
defp state(opts \\ []), do: Enum.into(opts, %{})
|
|
|
|
defp pin, do: 1
|
|
|
|
end
|