-
-
Notifications
You must be signed in to change notification settings - Fork 937
test: recover orchestration coverage from PR 426 #789
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
Closed
bendogabriel
wants to merge
2
commits into
SynkraAI:main
from
bendogabriel:recover/pr-426-orchestration-tests
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| /** | ||
| * Unit tests for circuit-breaker module | ||
| * | ||
| * Tests the CircuitBreaker pattern implementation with | ||
| * CLOSED, OPEN, and HALF_OPEN states. | ||
| */ | ||
|
|
||
| const { | ||
| CircuitBreaker, | ||
| STATE_CLOSED, | ||
| STATE_OPEN, | ||
| STATE_HALF_OPEN, | ||
| DEFAULT_FAILURE_THRESHOLD, | ||
| DEFAULT_SUCCESS_THRESHOLD, | ||
| DEFAULT_RESET_TIMEOUT_MS, | ||
| } = require('../../../.aiox-core/core/ids/circuit-breaker'); | ||
|
|
||
| describe('CircuitBreaker', () => { | ||
| let breaker; | ||
| const RESET_TIMEOUT_DELTA_MS = 1000; | ||
|
|
||
| beforeEach(() => { | ||
| breaker = new CircuitBreaker(); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // Constants | ||
| // ============================================================ | ||
| describe('constants', () => { | ||
| test('state constants are defined', () => { | ||
| expect(STATE_CLOSED).toBe('CLOSED'); | ||
| expect(STATE_OPEN).toBe('OPEN'); | ||
| expect(STATE_HALF_OPEN).toBe('HALF_OPEN'); | ||
| }); | ||
|
|
||
| test('default thresholds are defined', () => { | ||
| expect(DEFAULT_FAILURE_THRESHOLD).toBe(5); | ||
| expect(DEFAULT_SUCCESS_THRESHOLD).toBe(3); | ||
| expect(DEFAULT_RESET_TIMEOUT_MS).toBe(60000); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // Constructor | ||
| // ============================================================ | ||
| describe('constructor', () => { | ||
| test('starts in CLOSED state', () => { | ||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
| }); | ||
|
|
||
| test('uses default thresholds', () => { | ||
| const stats = breaker.getStats(); | ||
| expect(stats.failureCount).toBe(0); | ||
| expect(stats.successCount).toBe(0); | ||
| expect(stats.totalTrips).toBe(0); | ||
| }); | ||
|
|
||
| test('accepts custom options', () => { | ||
| const custom = new CircuitBreaker({ | ||
| failureThreshold: 3, | ||
| successThreshold: 2, | ||
| resetTimeoutMs: 30000, | ||
| }); | ||
| // Trip it to verify custom threshold | ||
| for (let i = 0; i < 3; i++) custom.recordFailure(); | ||
| expect(custom.getState()).toBe(STATE_OPEN); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // isAllowed | ||
| // ============================================================ | ||
| describe('isAllowed', () => { | ||
| test('allows requests when CLOSED', () => { | ||
| expect(breaker.isAllowed()).toBe(true); | ||
| }); | ||
|
|
||
| test('blocks requests when OPEN', () => { | ||
| // Trip the breaker | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
| expect(breaker.isAllowed()).toBe(false); | ||
| }); | ||
|
|
||
| test('transitions to HALF_OPEN after reset timeout', () => { | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
|
|
||
| // Simulate timeout passing | ||
| breaker._lastFailureTime = Date.now() - (DEFAULT_RESET_TIMEOUT_MS + RESET_TIMEOUT_DELTA_MS); | ||
|
|
||
| expect(breaker.isAllowed()).toBe(true); | ||
| expect(breaker.getState()).toBe(STATE_HALF_OPEN); | ||
| }); | ||
|
|
||
| test('allows one probe in HALF_OPEN', () => { | ||
| breaker._state = STATE_HALF_OPEN; | ||
| breaker._halfOpenProbeInFlight = false; | ||
|
|
||
| expect(breaker.isAllowed()).toBe(true); | ||
| expect(breaker.isAllowed()).toBe(false); // second request blocked | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // recordSuccess | ||
| // ============================================================ | ||
| describe('recordSuccess', () => { | ||
| test('resets failure count in CLOSED state', () => { | ||
| breaker.recordFailure(); | ||
| breaker.recordFailure(); | ||
| expect(breaker.getStats().failureCount).toBe(2); | ||
|
|
||
| breaker.recordSuccess(); | ||
| expect(breaker.getStats().failureCount).toBe(0); | ||
| }); | ||
|
|
||
| test('counts successes in HALF_OPEN', () => { | ||
| breaker._state = STATE_HALF_OPEN; | ||
| breaker._halfOpenProbeInFlight = true; | ||
|
|
||
| breaker.recordSuccess(); | ||
| expect(breaker.getStats().successCount).toBe(1); | ||
| }); | ||
|
|
||
| test('closes circuit after success threshold in HALF_OPEN', () => { | ||
| breaker._state = STATE_HALF_OPEN; | ||
|
|
||
| for (let i = 0; i < 3; i++) { | ||
| breaker._halfOpenProbeInFlight = true; | ||
| breaker.recordSuccess(); | ||
| } | ||
|
|
||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
| expect(breaker.getStats().failureCount).toBe(0); | ||
| expect(breaker.getStats().successCount).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // recordFailure | ||
| // ============================================================ | ||
| describe('recordFailure', () => { | ||
| test('increments failure count', () => { | ||
| breaker.recordFailure(); | ||
| expect(breaker.getStats().failureCount).toBe(1); | ||
| }); | ||
|
|
||
| test('opens circuit at threshold', () => { | ||
| for (let i = 0; i < 4; i++) breaker.recordFailure(); | ||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
|
|
||
| breaker.recordFailure(); // 5th failure | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
| }); | ||
|
|
||
| test('increments totalTrips when opening', () => { | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| expect(breaker.getStats().totalTrips).toBe(1); | ||
| }); | ||
|
|
||
| test('re-opens circuit from HALF_OPEN on failure', () => { | ||
| breaker._state = STATE_HALF_OPEN; | ||
| breaker._halfOpenProbeInFlight = true; | ||
|
|
||
| breaker.recordFailure(); | ||
|
|
||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
| expect(breaker.getStats().totalTrips).toBe(1); | ||
| }); | ||
|
|
||
| test('records lastFailureTime', () => { | ||
| const before = Date.now(); | ||
| breaker.recordFailure(); | ||
| const after = Date.now(); | ||
|
|
||
| expect(breaker.getStats().lastFailureTime).toBeGreaterThanOrEqual(before); | ||
| expect(breaker.getStats().lastFailureTime).toBeLessThanOrEqual(after); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // getStats | ||
| // ============================================================ | ||
| describe('getStats', () => { | ||
| test('returns complete stats object', () => { | ||
| const stats = breaker.getStats(); | ||
|
|
||
| expect(stats).toHaveProperty('state'); | ||
| expect(stats).toHaveProperty('failureCount'); | ||
| expect(stats).toHaveProperty('successCount'); | ||
| expect(stats).toHaveProperty('totalTrips'); | ||
| expect(stats).toHaveProperty('lastFailureTime'); | ||
| }); | ||
|
|
||
| test('lastFailureTime is null initially', () => { | ||
| expect(breaker.getStats().lastFailureTime).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // reset | ||
| // ============================================================ | ||
| describe('reset', () => { | ||
| test('resets to CLOSED state', () => { | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
|
|
||
| breaker.reset(); | ||
|
|
||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
| expect(breaker.getStats().failureCount).toBe(0); | ||
| expect(breaker.getStats().successCount).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| // ============================================================ | ||
| // Full lifecycle | ||
| // ============================================================ | ||
| describe('full lifecycle', () => { | ||
| test('CLOSED -> OPEN -> HALF_OPEN -> CLOSED', () => { | ||
| // 1. Start CLOSED | ||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
|
|
||
| // 2. Trip to OPEN | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
|
|
||
| // 3. Wait and transition to HALF_OPEN | ||
| breaker._lastFailureTime = Date.now() - (DEFAULT_RESET_TIMEOUT_MS + RESET_TIMEOUT_DELTA_MS); | ||
| breaker.isAllowed(); | ||
| expect(breaker.getState()).toBe(STATE_HALF_OPEN); | ||
|
|
||
| // 4. Simulate 3 consecutive successful probes by resetting the | ||
| // half-open probe flag between successes | ||
| breaker.recordSuccess(); | ||
| breaker._halfOpenProbeInFlight = true; | ||
| breaker.recordSuccess(); | ||
| breaker._halfOpenProbeInFlight = true; | ||
| breaker.recordSuccess(); | ||
| expect(breaker.getState()).toBe(STATE_CLOSED); | ||
| }); | ||
|
|
||
| test('CLOSED -> OPEN -> HALF_OPEN -> OPEN (failure in half-open)', () => { | ||
| for (let i = 0; i < 5; i++) breaker.recordFailure(); | ||
| breaker._lastFailureTime = Date.now() - (DEFAULT_RESET_TIMEOUT_MS + RESET_TIMEOUT_DELTA_MS); | ||
| breaker.isAllowed(); // HALF_OPEN | ||
|
|
||
| breaker.recordFailure(); // re-opens | ||
| expect(breaker.getState()).toBe(STATE_OPEN); | ||
| expect(breaker.getStats().totalTrips).toBe(2); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Use an absolute import path instead of a relative traversal.
Line 16 uses a relative import (
../../../...), which violates the repo rule requiring absolute imports for JS/TS-family files. Please switch this to the project’s absolute import form used in this codebase.As per coding guidelines, "
**/*.{js,jsx,ts,tsx}: Use absolute imports instead of relative imports in all code".🤖 Prompt for AI Agents
Source: Coding guidelines