Skip to content

Commit 8847b78

Browse files
authored
Merge pull request #287 from dylantirandaz/add-specs-and-quorum-tests
Add typespecs to internal modules and quorum tests
2 parents 241abd4 + 7c81eb9 commit 8847b78

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/horde/registry_impl.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ defmodule Horde.RegistryImpl do
77

88
defmodule State do
99
@moduledoc false
10+
11+
@type t :: %__MODULE__{
12+
name: atom() | nil,
13+
nodes: MapSet.t(node()),
14+
members: MapSet.t({atom(), node()}),
15+
registry_ets_table: atom() | nil,
16+
pids_ets_table: atom() | nil,
17+
keys_ets_table: atom() | nil,
18+
members_ets_table: atom() | nil,
19+
listeners: [atom()]
20+
}
21+
1022
defstruct name: nil,
1123
nodes: MapSet.new(),
1224
members: MapSet.new(),

lib/horde/signal_shutdown.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ defmodule Horde.SignalShutdown do
44
use GenServer
55
require Logger
66

7+
@spec child_spec(keyword()) :: Supervisor.child_spec()
78
def child_spec(options) do
89
%{
910
id: __MODULE__,
1011
start: {GenServer, :start_link, [__MODULE__, Keyword.get(options, :signal_to)]}
1112
}
1213
end
1314

15+
@impl GenServer
16+
@spec init([GenServer.server()]) :: {:ok, [GenServer.server()]}
1417
def init(signal_to) do
1518
Process.flag(:trap_exit, true)
1619
{:ok, signal_to}
1720
end
1821

22+
@impl GenServer
23+
@spec terminate(term(), [GenServer.server()]) :: :ok
1924
def terminate(_reason, signal_to) do
2025
Enum.each(signal_to, fn destination ->
2126
:ok = GenServer.call(destination, :horde_shutting_down)

test/uniform_quorum_distribution_test.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,49 @@ defmodule UniformQuorumDistributionTest do
5959
end
6060
end
6161
end
62+
63+
test "has_quorum? returns false for empty list" do
64+
refute Horde.UniformQuorumDistribution.has_quorum?([])
65+
end
66+
67+
test "has_quorum? returns nil when all members are shutting_down" do
68+
members = [
69+
%{status: :shutting_down, name: :a},
70+
%{status: :shutting_down, name: :b},
71+
%{status: :shutting_down, name: :c}
72+
]
73+
74+
assert Horde.UniformQuorumDistribution.has_quorum?(members) == nil
75+
end
76+
77+
test "has_quorum? returns true when majority is alive" do
78+
members = [
79+
%{status: :alive, name: :a},
80+
%{status: :alive, name: :b},
81+
%{status: :dead, name: :c}
82+
]
83+
84+
assert Horde.UniformQuorumDistribution.has_quorum?(members)
85+
end
86+
87+
test "has_quorum? returns false when majority is dead" do
88+
members = [
89+
%{status: :alive, name: :a},
90+
%{status: :dead, name: :b},
91+
%{status: :dead, name: :c}
92+
]
93+
94+
refute Horde.UniformQuorumDistribution.has_quorum?(members)
95+
end
96+
97+
test "choose_node returns quorum_not_met when no quorum" do
98+
members = [
99+
%{status: :alive, name: :a},
100+
%{status: :dead, name: :b},
101+
%{status: :dead, name: :c}
102+
]
103+
104+
assert {:error, :quorum_not_met} =
105+
Horde.UniformQuorumDistribution.choose_node(%{id: :test, start: {:test}}, members)
106+
end
62107
end

0 commit comments

Comments
 (0)