Summary
The current test suite simulates multi-machine scenarios sequentially in a single thread. We need to add true parallel execution tests using Node.js Worker threads to catch actual race conditions and ensure thread safety.
Current State
collision.test.ts generates 1-3M IDs sequentially.
- Multi-machine tests currently run in a single thread (simulated parallelism).
- No actual race condition testing for high-concurrency environments.
Proposed Implementation
1. Worker Thread Parallel Generation
Create a test that spawns actual Node.js workers to generate IDs simultaneously, verifying that no duplicates occur even when the system is under load.
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
// Plan:
// 1. Main thread spawns N workers
// 2. Each worker instantiates a SnowflakeGenerator with a unique machineId
// 3. Workers generate IDs as fast as possible
// 4. Main thread collects all IDs and checks for collisions
2. Same-Millisecond Race Condition
Specifically target the sequence number logic by forcing multiple workers to generate IDs within the same millisecond window.
3. Clock Drift Simulation
Simulate scenario where different workers have slightly different system times (drift) to ensure validity of the drift protection mechanisms.
Acceptance Criteria
Labels
testing, enhancement
Summary
The current test suite simulates multi-machine scenarios sequentially in a single thread. We need to add true parallel execution tests using Node.js Worker threads to catch actual race conditions and ensure thread safety.
Current State
collision.test.tsgenerates 1-3M IDs sequentially.Proposed Implementation
1. Worker Thread Parallel Generation
Create a test that spawns actual Node.js workers to generate IDs simultaneously, verifying that no duplicates occur even when the system is under load.
2. Same-Millisecond Race Condition
Specifically target the sequence number logic by forcing multiple workers to generate IDs within the same millisecond window.
3. Clock Drift Simulation
Simulate scenario where different workers have slightly different system times (drift) to ensure validity of the drift protection mechanisms.
Acceptance Criteria
Labels
testing,enhancement