Skip to content

test: Add multi-threaded concurrency tests (Worker threads) #7

@maulik-mk

Description

@maulik-mk

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

  • Test passes with 4+ parallel workers running simultaneously
  • Each worker generates at least 100k IDs
  • Zero collisions found across the combined dataset
  • Test runs successfully in the CI pipeline

Labels

testing, enhancement

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesttestingIncludes test cases and scripts for verifying fixes and new features.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions