Skip to content

test: End-to-end cluster integration tests with failure injection - #39

Merged
rapsealk merged 2 commits into
mainfrom
test/e2e-cluster-integration
Mar 30, 2026
Merged

rapsealk merged 2 commits into
mainfrom
test/e2e-cluster-integration

Conversation

@rapsealk

Copy link
Copy Markdown
Member

Summary

  • Add tests/test_e2e.py with a RaftCluster helper class that manages in-process Raft clusters with real gRPC networking
  • Implement 6 end-to-end integration tests: leader election, leader re-election after crash (failure injection), client write replication, read-via-lease, add-server, and remove-server
  • All tests use unique port ranges to avoid conflicts and try/finally for cleanup

Test plan

  • ruff check passes with no errors
  • ruff format produces no changes
  • All 6 tests pass via pytest tests/test_e2e.py -v (4.33s)

Closes #36

🤖 Generated with Claude Code

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 rapsealk left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_length or log_entries property to Raft for testability, or
  • Verifying replication indirectly via commit_index on 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/finally for cleanup in every test.
  • wait_for_leader with deadline-based polling is the right pattern.
  • The RaftCluster helper 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 rapsealk left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final Review — E2E Cluster Integration Tests

All three issues from the previous review have been properly fixed:

  1. Racy term assertion — Now uses wait_for_term_agreement() with a poll-based deadline loop. Correct.
  2. Sleep-based replication wait — Now uses wait_for_replication() with a poll-based deadline loop. Correct.
  3. Name-mangled _Raft__log access — Now uses the new public node.log property, which returns a defensive shallow copy via list(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. A conftest fixture that allocates ephemeral ports would be more robust long-term.
  • pytest.raises(CancelledError) (line 196): If the cancelled task finishes between cancel() and await, no exception is raised and pytest.raises will fail. Low probability in practice but try/except (CancelledError, ...) would be safer.
  • log property return type: list could be tightened to list[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

@rapsealk
rapsealk merged commit 2b55e5e into main Mar 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: End-to-end cluster integration tests with failure injection

1 participant