Replace timer-based tests with event-driven patterns#16
Merged
Conversation
This change eliminates sleep() calls from tests in favor of event-driven patterns, making tests faster and more reliable. Changes: - Add test/helpers/events.ts with event-driven testing utilities: - waitForEvents(): Wait for N events without polling - createLatch(): Manually resolvable promises for coordination - promisifyCallback(): Convert callback-based APIs to promises - waitForCallbacks(): Wait for N callback invocations - Re-export once() for single-event waiting - Update all test files to use event-driven patterns: - test/request-response.test.ts: Use waitForEvents for job completion - test/deduplication.test.ts: Use waitForEvents for deduplication checks - test/reaper.test.ts: Use once() for reaper events - test/redis-storage.test.ts: Use promisifyCallback for notifications - test/file-storage.test.ts: Use once() for file storage events - test/memory-storage.test.ts: Use once() for memory storage events - test/integration/e2e.test.ts: Use waitForEvents for E2E flows Benefits: - Tests run faster (no arbitrary sleep delays) - Tests are more reliable (no race conditions from insufficient delays) - Tests complete exactly when conditions are met - Resource cleanup is cleaner (no lingering timers) Note: TTL/expiry tests still use real timers where wall-clock time is required by the feature being tested. Key pattern: Event listeners must be registered BEFORE operations that trigger those events to avoid race conditions.
- Add space before function parentheses - Use _resolve for promise parameters - Remove unused imports Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR eliminates
sleep()calls from tests in favor of event-driven patterns, making tests faster and more reliable. Tests now complete exactly when conditions are met instead of waiting for arbitrary timeouts.Key Changes
New Testing Utilities (
test/helpers/events.ts)Added a comprehensive set of event-driven testing helpers:
waitForEvents(emitter, event, count): Wait for N events without pollingcreateLatch(): Manually resolvable promises for test coordinationpromisifyCallback(subscribe): Convert callback-based APIs to promiseswaitForCallbacks(count): Wait for N callback invocationsonce: Re-exported from Node.js for single-event waitingUpdated Test Files
All test files now use event-driven patterns:
test/request-response.test.ts: UsewaitForEvents()for job completiontest/deduplication.test.ts: UsewaitForEvents()for deduplication checkstest/reaper.test.ts: Useonce()for reaper eventstest/redis-storage.test.ts: UsepromisifyCallback()for notificationstest/file-storage.test.ts: Useonce()for file storage eventstest/memory-storage.test.ts: Useonce()for memory storage eventstest/integration/e2e.test.ts: UsewaitForEvents()for E2E flowsBenefits
Important Pattern
Event listeners must be registered BEFORE operations that trigger those events to avoid race conditions. For example:
Exceptions
TTL/expiry tests still use real timers where wall-clock time is required by the feature being tested. Event-driven patterns cannot replace actual time-based expiration logic.
Testing
All existing tests pass with the new event-driven patterns. The test suite should now exit cleanly without any lingering resources.