Skip to content

Replace timer-based tests with event-driven patterns#16

Merged
mcollina merged 2 commits intomainfrom
remove-timer-dependencies-tests
Feb 16, 2026
Merged

Replace timer-based tests with event-driven patterns#16
mcollina merged 2 commits intomainfrom
remove-timer-dependencies-tests

Conversation

@mcollina
Copy link
Copy Markdown
Member

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 polling
  • createLatch(): Manually resolvable promises for test coordination
  • promisifyCallback(subscribe): Convert callback-based APIs to promises
  • waitForCallbacks(count): Wait for N callback invocations
  • once: Re-exported from Node.js for single-event waiting

Updated Test Files

All test files now 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

  • Faster tests: No arbitrary sleep delays (tests complete immediately when conditions are met)
  • More reliable: Eliminates race conditions from insufficient sleep delays
  • Cleaner resource management: No lingering timers that could prevent test exit
  • Better test clarity: Event-driven patterns make test intent explicit

Important Pattern

Event listeners must be registered BEFORE operations that trigger those events to avoid race conditions. For example:

// Correct: Register listener first
const promise = waitForEvents(queue, 'completed', 1)
await queue.enqueue({ type: 'test', data: {} })
await promise

// Incorrect: Race condition risk
await queue.enqueue({ type: 'test', data: {} })
await waitForEvents(queue, 'completed', 1) // Might miss the event!

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.

mcollina and others added 2 commits February 16, 2026 13:54
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>
@mcollina mcollina merged commit 8c61fc8 into main Feb 16, 2026
10 checks passed
@mcollina mcollina deleted the remove-timer-dependencies-tests branch February 16, 2026 13:27
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.

1 participant