Concurrency strategies
provide.testkit.chaos.concurrency_strategies
¶
Concurrency and parallelism chaos strategies.
Provides Hypothesis strategies for generating concurrent execution patterns, race conditions, deadlock scenarios, and threading/async edge cases.
Functions¶
async_event_patterns
¶
Generate async event patterns for coroutine testing.
Creates patterns of async events, delays, and scheduling scenarios.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
max_events
|
int
|
Maximum number of events to generate |
50
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of event dictionaries |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
deadlock_scenarios
¶
Generate resource lock patterns that may cause deadlocks.
Creates scenarios where circular dependencies between locks might occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
num_resources
|
int
|
Number of lockable resources |
5
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing deadlock scenario configuration |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
lock_contention_patterns
¶
lock_contention_patterns(
draw: DrawFn,
num_locks: int = 5,
num_operations: int = 20,
) -> dict[str, Any]
Generate lock contention patterns for testing synchronization.
Creates scenarios with varying levels of lock contention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
num_locks
|
int
|
Number of available locks |
5
|
num_operations
|
int
|
Number of operations competing for locks |
20
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing lock contention configuration |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
pid_recycling_scenarios
¶
Generate PID recycling attack scenarios.
Creates scenarios where PIDs might be reused, testing protection mechanisms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing PID recycling scenario |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
process_pool_patterns
¶
process_pool_patterns(
draw: DrawFn,
max_workers: int = 10,
max_tasks: int = 100,
) -> dict[str, Any]
Generate process pool execution patterns.
Creates scenarios for testing process pool behavior under various conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
max_workers
|
int
|
Maximum number of worker processes |
10
|
max_tasks
|
int
|
Maximum number of tasks to submit |
100
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing pool configuration |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
race_condition_triggers
¶
race_condition_triggers(
draw: DrawFn,
num_operations: int = 10,
max_delay: float = 0.1,
) -> list[tuple[int, float]]
Generate timing patterns designed to trigger race conditions.
Returns operation timings that maximize the chance of exposing race conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
num_operations
|
int
|
Number of concurrent operations |
10
|
max_delay
|
float
|
Maximum delay between operations in seconds |
0.1
|
Returns:
| Type | Description |
|---|---|
list[tuple[int, float]]
|
List of (operation_id, delay_before_execution) tuples |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
task_cancellation_patterns
¶
Generate task cancellation scenarios for async testing.
Creates patterns of task creation and cancellation to test cleanup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
num_tasks
|
int
|
Number of tasks in the pattern |
20
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of task configuration dictionaries |
Example
Source code in provide/testkit/chaos/concurrency_strategies.py
thread_counts
¶
thread_counts(
draw: DrawFn,
min_threads: int = 1,
max_threads: int = 100,
include_extremes: bool = True,
) -> int
Generate thread count scenarios for concurrency testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
Hypothesis draw function |
required |
min_threads
|
int
|
Minimum number of threads |
1
|
max_threads
|
int
|
Maximum number of threads |
100
|
include_extremes
|
bool
|
Include edge cases (1 thread, max threads) |
True
|
Returns:
| Type | Description |
|---|---|
int
|
Number of threads to spawn |