-
-
Notifications
You must be signed in to change notification settings - Fork 108
refactor(contract): improve testability and error handling #2258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
How long should a successful initialziation take potentially? Should we track this? |
|
Re: tracking initialization duration - this will be addressed as part of the stale initialization cleanup. The
[AI-assisted - Claude] |
| /// Returns information about each stale initialization that was cleaned up. | ||
| /// This should be called periodically to prevent resource leaks from | ||
| /// initializations that never complete (e.g., due to bugs or crashes). | ||
| #[allow(dead_code)] // For future periodic cleanup integration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldnt we be using this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - cleanup is now called opportunistically at the start of each upsert_contract_state() call. Any initializations older than 30 seconds are purged and logged with a warning.
[AI-assisted - Claude]
be9c12a to
343d86c
Compare
- Replace todo!() panics with proper FatalExecutorError propagation - Extract ContractInitTracker state machine for testable init logic - Add test fixtures module for creating contract test data - Add 27 new unit tests covering init tracking, error handling, and fixtures The contract initialization tracking logic was previously embedded inline in upsert_contract_state() making it difficult to test race conditions. Now extracted into a dedicated ContractInitTracker with clear API. Fatal executor errors now propagate properly instead of crashing via todo!() - this was a production bug waiting to happen. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Address review feedback from iduartgomez regarding potential resource leaks in the ContractInitTracker. Changes: - Add SLOW_INIT_THRESHOLD (1s) and STALE_INIT_THRESHOLD (30s) constants - Add cleanup_stale_initializations() method to purge stuck initializations - Add StaleInitInfo struct to report cleaned up entries - Log warning when initialization takes > 1 second - Add initializing_count() for monitoring - Add 4 new tests for cleanup functionality The cleanup method is marked #[allow(dead_code)] as it needs to be integrated with the event loop or a periodic task runner. The API is ready for that integration. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Call cleanup_stale_initializations() opportunistically at the start of each upsert_contract_state() call. This ensures any initializations stuck for > 30 seconds are cleaned up and logged with a warning. Removes #[allow(dead_code)] annotations since the cleanup is now active. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
56e0dcc to
1a139dc
Compare
Problem
The contract module had several testability and reliability issues:
Production crash risk: Three
todo!()calls inmod.rs(lines 66-67, 128-129, 191-193) would panic on fatal executor errors instead of handling them gracefully. This was a bug waiting to happen in production.Untestable init logic: Contract initialization state tracking was embedded inline in
upsert_contract_state()across ~80 lines, making it impossible to unit test race conditions between PUT and UPDATE operations.Missing test infrastructure: No convenient way to create test fixtures for contract types (
ContractKey,WrappedState,Parameters), which made writing new tests tedious.This Solution
1. Replace
todo!()with proper error handlingAdded
FatalExecutorErrorvariant toContractErrorenum. Fatal errors now propagate properly instead of crashing:2. Extract
ContractInitTrackerstate machineCreated new module
executor/init_tracker.rs(~250 lines) that encapsulates all initialization tracking:This makes the state machine logic:
3. Add test fixtures module
Added
test_fixturesmodule with helpers:make_contract_key()/make_contract_key_with_code()make_state(),make_params(),make_delta()4. Add comprehensive tests
ContractInitTracker(all state transitions, edge cases)ExecutorError(error types, conversions, panic behavior)Testing
Files Changed
[AI-assisted - Claude]