2020-01-06 13:48:16 +13:00
|
|
|
defmodule WaferInterruptRegistryTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
alias Wafer.InterruptRegistry, as: IR
|
|
|
|
@moduledoc false
|
|
|
|
|
2020-01-08 12:20:39 +13:00
|
|
|
setup do
|
|
|
|
Supervisor.terminate_child(Wafer.Supervisor, IR)
|
|
|
|
Supervisor.restart_child(Wafer.Supervisor, IR)
|
|
|
|
{:ok, []}
|
|
|
|
end
|
|
|
|
|
2020-01-06 13:48:16 +13:00
|
|
|
describe "subscribe/3" do
|
|
|
|
test "it subscribes the receiver with `nil` metadata" do
|
|
|
|
receiver = self()
|
2020-01-21 19:16:04 +13:00
|
|
|
assert :ok = IR.subscribe(:key, :rising, :conn)
|
2020-01-06 13:48:16 +13:00
|
|
|
assert [{:rising, :conn, nil, ^receiver}] = IR.subscriptions(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-21 19:16:04 +13:00
|
|
|
describe "subscribe/4" do
|
2020-01-06 13:48:16 +13:00
|
|
|
test "it subscribes the receiver with metadata" do
|
|
|
|
receiver = self()
|
2020-01-21 19:16:04 +13:00
|
|
|
assert :ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
assert [{:rising, :conn, :metadata, ^receiver}] = IR.subscriptions(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "unsubscribe/3" do
|
2020-01-21 19:16:04 +13:00
|
|
|
test "unsubscribes the caller" do
|
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert :ok = IR.unsubscribe(:key, :rising, :conn)
|
|
|
|
assert [] = IR.subscriptions(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscriptions/1" do
|
|
|
|
test "lists all subscriptions for `key`" do
|
|
|
|
receiver = self()
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :falling, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert [
|
|
|
|
{:rising, :conn, :metadata, ^receiver},
|
|
|
|
{:falling, :conn, :metadata, ^receiver},
|
|
|
|
{:both, :conn, :metadata, ^receiver}
|
|
|
|
] = IR.subscriptions(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscriptions/2" do
|
|
|
|
test "lists all subscriptions for `key` and `pin_condition`" do
|
|
|
|
receiver = self()
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :falling, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert [{:falling, :conn, :metadata, ^receiver}] = IR.subscriptions(:key, :falling)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "count_subscriptions/1" do
|
|
|
|
test "returns the number of subscriptions for `key`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :falling, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert 3 = IR.count_subscriptions(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "count_subscriptions/2" do
|
|
|
|
test "returns the number of subscriptions for `key`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :falling, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert 1 = IR.count_subscriptions(:key, :falling)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscribers?/1" do
|
|
|
|
test "returns true if there are any subscribers for `key`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert IR.subscribers?(:key)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns false if there are no subscribers for `key`" do
|
|
|
|
refute IR.subscribers?(:key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscribers?/2" do
|
|
|
|
test "returns true if there are any subscribers for `key` and `pin_condition`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert IR.subscribers?(:key, :rising)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns false if there are no subscribers for `key` and `pin_condition`" do
|
|
|
|
receiver = self()
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
refute IR.subscribers?(:key, :falling)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "publish/2" do
|
|
|
|
test "publishes to subscribers of `key` and `pin_condition`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
IR.publish(:key, :rising)
|
|
|
|
|
|
|
|
assert_received {:interrupt, :conn, :rising, :metadata}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "publishes to subscribers of `key` and `:both`" do
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
IR.publish(:key, :rising)
|
|
|
|
|
|
|
|
assert_received {:interrupt, :conn, :rising, :metadata}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns the number of messages sent" do
|
|
|
|
assert {:ok, 0} = IR.publish(:key, :rising)
|
|
|
|
|
2020-01-21 19:16:04 +13:00
|
|
|
:ok = IR.subscribe(:key, :rising, :conn, :metadata)
|
|
|
|
:ok = IR.subscribe(:key, :both, :conn, :metadata)
|
2020-01-06 13:48:16 +13:00
|
|
|
|
|
|
|
assert {:ok, 2} = IR.publish(:key, :rising)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|