Skip to content

Commit 3dc5aac

Browse files
committed
test(elixir): add KRaftSupervisor coverage (6 tests, 6 of 9 modules done)
Sixth of nine modules from the test-gap audit gets a dedicated test file. KRaftSupervisor is small (~47 LOC) but central to consensus bootstrap; previously had zero direct tests. Coverage: - init/1 with empty :nodes returns Supervisor child-spec tuple with one_for_one strategy and just the Registry child - One configured node yields Registry + one KRaftNode spec; child id is {KRaftNode, "node-id"} for uniqueness across multi-node cluster - Three configured nodes produce 4 children (Registry + 3 KRaftNode), IDs are unique and contain each configured node_id - Missing :node_id in node opts raises KeyError as expected (matches the Keyword.fetch! contract) Tests use Supervisor.init/2 directly (not start_link) to avoid the __MODULE__-name collision with any app-managed instance, so this file is safe to run in any test environment. Cumulative coverage progress: ✓ DriftMonitor (PR #35) TODO vql_executor (1812 LOC) ✓ EntityServer (PR #35) TODO vql_bridge (738 LOC) ✓ QueryRouter (PR #35) TODO telemetry/collector ✓ SchemaRegistry (PR #37) TODO telemetry/reporter ✓ KRaftSupervisor (this commit) https://claude.ai/code/session_01GeiWCLLoZmPdnjMMcy2buu
1 parent 8b065e8 commit 3dc5aac

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# SPDX-License-Identifier: MPL-2.0
2+
3+
defmodule VeriSim.Consensus.KRaftSupervisorTest do
4+
@moduledoc """
5+
Tests for VeriSim.Consensus.KRaftSupervisor — the supervisor that
6+
starts the local Registry and a configurable list of KRaftNode
7+
processes.
8+
9+
The supervisor uses `name: __MODULE__`, so only one instance can be
10+
running at a time. These tests start it under a different name via
11+
Supervisor.start_link directly so they don't conflict with any
12+
app-managed instance.
13+
"""
14+
15+
use ExUnit.Case, async: false
16+
17+
alias VeriSim.Consensus.KRaftSupervisor
18+
19+
describe "start_link/1 — empty cluster" do
20+
test "starts cleanly with no nodes" do
21+
# Empty list: only the Registry child should be started.
22+
# We test the init/1 callback's child-spec generation directly to
23+
# avoid needing a unique supervisor name.
24+
{:ok, children_specs} = KRaftSupervisor.init(nodes: [])
25+
assert is_tuple(children_specs)
26+
end
27+
end
28+
29+
describe "child spec generation" do
30+
test "init/1 returns a Supervisor child-spec tuple with one_for_one strategy" do
31+
{:ok, {sup_flags, children}} = KRaftSupervisor.init(nodes: [])
32+
33+
assert sup_flags.strategy == :one_for_one
34+
assert is_list(children)
35+
assert length(children) == 1
36+
end
37+
38+
test "the only child for an empty cluster is the consensus Registry" do
39+
{:ok, {_flags, children}} = KRaftSupervisor.init(nodes: [])
40+
41+
[registry_spec] = children
42+
# Children come back as %{id: Registry, start: {Registry, :start_link, [...]}}
43+
assert registry_spec.id == Registry
44+
end
45+
46+
test "one configured node yields Registry + one KRaftNode spec" do
47+
{:ok, {_flags, children}} =
48+
KRaftSupervisor.init(
49+
nodes: [
50+
[node_id: "n1", peers: []]
51+
]
52+
)
53+
54+
assert length(children) == 2
55+
[_registry, node_spec] = children
56+
assert node_spec.id == {VeriSim.Consensus.KRaftNode, "n1"}
57+
end
58+
59+
test "three configured nodes yield Registry + three KRaftNode specs" do
60+
{:ok, {_flags, children}} =
61+
KRaftSupervisor.init(
62+
nodes: [
63+
[node_id: "a", peers: ["b", "c"]],
64+
[node_id: "b", peers: ["a", "c"]],
65+
[node_id: "c", peers: ["a", "b"]]
66+
]
67+
)
68+
69+
assert length(children) == 4
70+
71+
[_registry | node_specs] = children
72+
node_ids = Enum.map(node_specs, & &1.id)
73+
assert {VeriSim.Consensus.KRaftNode, "a"} in node_ids
74+
assert {VeriSim.Consensus.KRaftNode, "b"} in node_ids
75+
assert {VeriSim.Consensus.KRaftNode, "c"} in node_ids
76+
end
77+
78+
test "child IDs are unique per node so the supervisor can manage them" do
79+
{:ok, {_flags, children}} =
80+
KRaftSupervisor.init(
81+
nodes: [
82+
[node_id: "x", peers: []],
83+
[node_id: "y", peers: []],
84+
[node_id: "z", peers: []]
85+
]
86+
)
87+
88+
ids = Enum.map(children, & &1.id)
89+
assert length(ids) == length(Enum.uniq(ids))
90+
end
91+
end
92+
93+
describe "missing :node_id" do
94+
test "raises KeyError when a node opts is missing :node_id" do
95+
assert_raise KeyError, fn ->
96+
KRaftSupervisor.init(nodes: [[peers: ["other"]]])
97+
end
98+
end
99+
end
100+
end

0 commit comments

Comments
 (0)