Repository navigation
test: End-to-end cluster integration tests with failure injection - #39
Conversation
Add tests/test_e2e.py with a RaftCluster helper and 6 integration tests that spin up real in-process gRPC clusters: - Leader election (3 nodes) - Leader re-election after crash - Client request committed and replicated - Read request via leader lease - Add server to cluster - Remove server from cluster Closes #36 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
rapsealk
left a comment
There was a problem hiding this comment.
E2E Test Review
Overall this is a solid set of integration tests with good coverage of the core Raft operations. A few issues worth addressing:
Port Conflicts (Medium)
Each test uses a hardcoded base_port (50100, 50200, ..., 50600). If pytest runs tests in parallel (e.g., pytest-xdist), or if any of these ports are already in use on the CI host, tests will fail with Address already in use. Consider using port 0 (OS-assigned ephemeral ports) or a fixture that probes for free ports. At minimum, document that these tests must run sequentially.
Cleanup Reliability (Medium)
stop() cancels tasks but does not explicitly shut down the gRPC servers. Depending on GrpcRaftServer internals, cancelled server.run() tasks may not release the listening socket immediately, which can cause port conflicts on fast re-runs or between tests. Consider calling a graceful shutdown method on each server (if available) before cancelling, and/or adding a small await asyncio.sleep(0) after gather to let the event loop finalize cleanup.
Flakiness: Term Agreement Assertion (High)
In test_leader_election_3_nodes (line 156), the assertion that all nodes share the same term as the leader is racy. A follower may still be in an earlier term if it hasn't yet received the leader's first heartbeat. A poll-with-timeout pattern (like wait_for_leader) would be more robust here.
Flakiness: Replication Check via sleep(1.0) (Medium)
In test_client_request_committed (line 213), a fixed 1-second sleep is used to wait for replication. On a loaded CI machine this may not be enough; on a fast machine it wastes time. Replace with a poll loop that checks commit_index on all followers, similar to the last_applied poll in test_read_request_with_lease.
Brittleness: Name-Mangled _Raft__log Access (Medium)
test_client_request_committed (line 221) accesses node._Raft__log via Python name mangling. This is fragile -- any rename of the private attribute silently breaks the test. Consider either:
- Adding a public
log_lengthorlog_entriesproperty to Raft for testability, or - Verifying replication indirectly via
commit_indexon all nodes (which is already a public property and sufficient to prove replication).
Minor: pytest.raises(CancelledError) may not trigger (Low)
In test_leader_reelection_after_kill (line 176), await main_task after cancel() will raise CancelledError, but only if the task hasn't already finished. If the task completed between the cancel and await calls, no exception is raised and the pytest.raises context manager will fail the test. Safer to use return_exceptions=True with gather, or catch both cases.
Missing: new_addr unused variable (Nit)
In test_add_server_to_cluster (line 281), new_addr is constructed but only used as an argument to leader.add_server(). This is fine, but since cluster.add_node(new_port) already adds it to cluster._addrs, you could derive it from there to avoid duplication.
Positive Notes
- Good use of
try/finallyfor cleanup in every test. wait_for_leaderwith deadline-based polling is the right pattern.- The
RaftClusterhelper is well-structured and reusable. - Port ranges are well-separated (100 apart) to avoid overlap between tests in sequential runs.
🤖 Generated with Claude Code
- Replace racy term agreement assertion with poll loop (wait_for_term_agreement) - Replace hardcoded sleep(1.0) for replication with poll loop (wait_for_replication) - Add asyncio.sleep(0) after task cancellation in stop() for socket cleanup - Add public `log` property to Raft class; use it instead of name-mangled _Raft__log Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
rapsealk
left a comment
There was a problem hiding this comment.
Final Review — E2E Cluster Integration Tests
All three issues from the previous review have been properly fixed:
- Racy term assertion — Now uses
wait_for_term_agreement()with a poll-based deadline loop. Correct. - Sleep-based replication wait — Now uses
wait_for_replication()with a poll-based deadline loop. Correct. - Name-mangled
_Raft__logaccess — Now uses the new publicnode.logproperty, which returns a defensive shallow copy vialist(self.__log). Correct.
Remaining minor items (non-blocking):
- Hardcoded ports: Tests still rely on fixed port ranges (50100-50600). Acceptable for sequential runs but will break under
pytest-xdist. Aconftestfixture that allocates ephemeral ports would be more robust long-term. pytest.raises(CancelledError)(line 196): If the cancelled task finishes betweencancel()andawait, no exception is raised andpytest.raiseswill fail. Low probability in practice buttry/except (CancelledError, ...)would be safer.logproperty return type:listcould be tightened tolist[raft_pb2.Log]for type checker support. Cosmetic.await asyncio.sleep(0.3)for lease validity (line 276): Acceptable — lease freshness is inherently time-based and harder to poll for.
No new issues found. The RaftCluster helper is well-structured, all tests use try/finally for cleanup, and the polling patterns are correct. LGTM for merge.
🤖 Generated with Claude Code
Summary
tests/test_e2e.pywith aRaftClusterhelper class that manages in-process Raft clusters with real gRPC networkingtry/finallyfor cleanupTest plan
ruff checkpasses with no errorsruff formatproduces no changespytest tests/test_e2e.py -v(4.33s)Closes #36
🤖 Generated with Claude Code