fracture 0.1.1

Zero-config chaos testing, drop-in replacement for Tokio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Fracture

> ⚠️ **PROJECT IS IN ALPHA** - Fracture is in early development (v0.1.1). The core concepts work, but there are likely edge cases and bugs we haven't found yet. **Please report any issues you encounter!** The irony is not lost on us that a chaos testing tool needs help finding its own bugs. πŸ™ƒ

**Deterministic chaos testing for async Rust. Drop-in for Tokio.**

Fracture is a testing framework that helps you find bugs in async code by simulating failures, network issues, and race conditionsβ€”all deterministically and reproducibly. Note that Fracture is only a drop-in replacement for Tokio and does not work with any other async runtime.

## The Problem

Most async Rust code looks fine in tests but breaks in production:

```rust
async fn handle_request(db: &Database, api: &ExternalApi) -> Result<Response> {
    let user = db.get_user(user_id).await?;  // What if the DB times out?
    let data = api.fetch_data().await?;       // What if the API returns 500?
    Ok(process(user, data))
}
```

**Your tests pass because they assume the happy path.** Production doesn't.

## The Solution

Fracture runs your async code in a **simulated environment** with **deterministic chaos injection**:

```rust
#[fracture::test]
async fn test_with_chaos() {
    // Inject 30% network failure rate
    chaos::inject(ChaosOperation::TcpWrite, 0.3);
    
    // Your code runs with failures injected
    let result = handle_request(&db, &api).await;
    
    // Did your retry logic work? Did you handle timeouts?
    assert!(result.is_ok());
}
```

**Same seed = same failures = reproducible bugs.**

## Features

- βœ… **Deterministic** - Control randomness with seeds, reproduce bugs every time
- βœ… **Fast** - Pure in-memory simulation, no real network/filesystem
- βœ… **Chaos Injection** - Network failures, delays, partitions, timeouts, task aborts
- βœ… **Drop-in Testing** - Works like `#[tokio::test]` but with superpowers
- βœ… **Async Primitives** - Tasks, channels, timers, TCP, select!, timeouts
- βœ… **Scenario Builder** - Script complex failure sequences (partitions, delays, healing)

## Installation

⚠️ **Alpha warning:** API may change between minor versions until 1.0.0

Add to your `Cargo.toml`:

```toml
[dev-dependencies]
fracture = "0.1"
```

## Quick Start

### Basic Test

```rust
use fracture::prelude::*;

#[fracture::test]
async fn my_async_test() {
    let (tx, mut rx) = mpsc::unbounded();
    
    spawn(async move {
        tx.send("hello").unwrap();
    });
    
    assert_eq!(rx.recv().await, Some("hello"));
}
```


### Running Applications with `#[fracture::main]`

Use `#[fracture::main]` as a drop-in replacement for `#[tokio::main]`:

```rust
use fracture::prelude::*;

#[fracture::main]
async fn main() {
    println!("Starting server...");

    let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();

    loop {
        let (socket, _) = listener.accept().await.unwrap();
        spawn(async move {
            handle_connection(socket).await;
        });
    }
}
```

**How it works:**
- **With `simulation` feature enabled**: Runs using Fracture's deterministic runtime (for development/testing with chaos injection)
- **Without `simulation` feature**: Falls back to the real Tokio runtime (for production)

This lets you develop and test with Fracture's chaos testing capabilities, then deploy to production with zero code changes by simply disabling the `simulation` feature.

**Example Cargo.toml setup:**

```toml
[dependencies]
# Production: Uses real Tokio runtime
fracture = "0.1"

[dev-dependencies]
# Testing: Uses simulation runtime with chaos
fracture = { version = "0.1", features = ["simulation"] }
```

With this setup, `cargo run` uses the real Tokio runtime, while `cargo test` uses Fracture's simulation.

### With Chaos Injection

```rust
use fracture::prelude::*;
use fracture::chaos;

#[fracture::test]
async fn test_resilient_sender() {
    // 50% chance messages fail to send
    chaos::inject(ChaosOperation::MpscSend, 0.5);
    
    let (tx, mut rx) = mpsc::unbounded();
    
    // Your code needs retry logic to pass this test!
    let sender = spawn(async move {
        for i in 0..10 {
            let mut attempts = 0;
            loop {
                match tx.send(i) {
                    Ok(_) => break,
                    Err(_) => {
                        attempts += 1;
                        if attempts > 5 {
                            panic!("Failed after 5 retries");
                        }
                        sleep(Duration::from_millis(10)).await;
                    }
                }
            }
        }
    });
    
    sender.await.unwrap();
}
```

### Network Partition Scenarios

```rust
use fracture::chaos::{ChaosOperation, Scenario};

#[fracture::test(duration = "5s")]
async fn test_network_partition() {
    let scenario = Scenario::new()
        .seed(42)  // Deterministic chaos
        .partition("192.168.1.1:8000", "192.168.1.2:8000")
        .wait(Duration::from_secs(2))
        .heal_partition("192.168.1.1:8000", "192.168.1.2:8000");
    
    spawn(scenario.execute_chaos());
    
    // Your distributed system code runs here
    // Does it handle partitions correctly?
}
```

### Reproducing Bugs

When a test fails, Fracture shows you the seed:

```
Run with FRACTURE_SEED=17135321411058301739 to reproduce.
```

Set the environment variable to get the exact same failure:

```bash
FRACTURE_SEED=17135321411058301739 cargo test
```

## Testing External Libraries (reqwest, sqlx, aws-sdk)
By default, Fracture simulates your logic. External libraries that depend on the real tokio runtime (like database drivers or HTTP clients) will continue to use the real network and OS threads, ignoring your chaos settings.

To simulate chaos in external libraries, you must "patch" Tokio.

We provide a Shim Crate strategy that tricks the entire dependency tree into using Fracture instead of Tokio.

1. The Setup

In your Cargo.toml, add a patch directive to redirect tokio to the shim included in this repository:
```
[patch.crates-io]

⚠️ This forces every library in your tree to use Fracture as its runtime
tokio = { git = "https://github.com/ZA1815/fracture", path = "tokio-shim" }
```

2. The Rules

When patching is active:

Do NOT enable the tokio feature in fracture. Your Cargo.toml dependencies should look like this:

```
[dev-dependencies]
# Only enable simulation features, do not depend on the real tokio
fracture = { version = "0.1", features = ["simulation"] } 
Run tests normally: cargo test
```

Revert for production: Remove the [patch] section when building your actual application release.

Why do this?
Time Travel: fracture::time::sleep(Duration::from_secs(3600)) will instantly advance time for reqwest timeouts.

Network Chaos: You can inject packet loss into sqlx database connections.

Determinism: The entire stack becomes deterministic, including 3rd party driver behavior.

## Use Cases

### Web Backends & APIs

Test your HTTP handlers under real-world conditions:

```rust
#[fracture::test]
async fn test_api_with_database_timeouts() {
    chaos::inject(ChaosOperation::TcpRead, 0.2);  // 20% DB read failures
    
    let response = handle_get_user(user_id).await;
    
    // Does your code return a proper error? Retry? Use a fallback?
    assert!(response.is_ok());
}
```

### Distributed Systems

Test consensus algorithms, replication, leader election:

```rust
#[fracture::test]
async fn test_raft_with_network_partition() {
    let scenario = Scenario::new()
        .partition("node1", "node2")
        .wait(Duration::from_secs(1))
        .heal_partition("node1", "node2");
    
    spawn(scenario.execute_chaos());
    
    // Run your Raft nodes
    // Does leader election still work?
}
```

### Background Job Processors

Test task queues with failures:

```rust
#[fracture::test]
async fn test_job_retry_logic() {
    chaos::inject(ChaosOperation::TaskPanic, 0.1);  // 10% tasks panic
    
    let results = process_jobs(job_queue).await;
    
    // Did failed jobs retry? Did you lose any work?
    assert_eq!(results.len(), expected_count);
}
```

### Real-Time Systems

Test WebSocket servers, event streams, subscriptions:

```rust
#[fracture::test]
async fn test_websocket_reconnection() {
    chaos::inject(ChaosOperation::TcpWrite, 0.3);
    
    let client = WebSocketClient::connect().await?;
    
    // Does your client reconnect on failures?
    // Do you maintain message order?
}
```

## Available Chaos Operations

Fracture can inject failures into:

- **Tasks**: `TaskSpawn`, `TaskAbort`, `TaskPanic`, `TaskDeadlock`, `TaskStarvation`
- **Channels**: `MpscSend`, `MpscRecv`, `OneshotSend`, `OneshotRecv`
- **Network**: `TcpConnect`, `TcpAccept`, `TcpRead`, `TcpWrite`
- **Time**: `SleepShort`, `SleepLong`, `TimeoutEarly`, `TimeoutLate`
- **Threading**: `SpawnBlocking`, `ThreadPoolExhaustion`

Set chaos rates from 0.0 (never) to 1.0 (always):

```rust
chaos::inject(ChaosOperation::TcpWrite, 0.5);  // 50% failure rate
```

## How It Works

1. **Simulation Runtime** - Fracture provides a complete async runtime that runs entirely in-memory
2. **Deterministic Scheduling** - Task execution order is controlled by a seeded RNG
3. **Chaos Injection** - At key points (sends, receives, I/O), Fracture can inject failures
4. **Time Control** - Virtual time advances deterministically, no real sleeps
5. **Reproducibility** - Same seed β†’ same task order β†’ same failures β†’ same bugs

This is inspired by FoundationDB's approach to testing: run thousands of simulated scenarios to find rare edge cases.

## Testing Philosophy

**Traditional approach:**
```rust
#[tokio::test]
async fn test_happy_path() {
    let result = my_function().await;
    assert!(result.is_ok());  // βœ… Passes (but only in ideal conditions)
}
```

**Fracture approach:**
```rust
#[fracture::test]
async fn test_with_real_conditions() {
    chaos::inject(ChaosOperation::TcpWrite, 0.3);  // Real networks fail
    
    let result = my_function_with_retries().await;
    assert!(result.is_ok());  // ⚠️ Fails unless you handle errors properly
}
```

**Fracture forces you to write resilient code from the start.**

## API Overview


### Main Entry Point

```rust
#[fracture::main]                    // Basic async main (switches between Fracture/Tokio)
#[fracture::main(duration = "10s")]  // Optional duration parameter (when using simulation)
async fn main() { }
```

### Testing

```rust
#[fracture::test]                    // Basic test
#[fracture::test(duration = "10s")]  // Run for 10 simulated seconds
async fn my_test() { }
```

### Async Primitives

```rust
// Tasks
let handle = spawn(async { ... });
handle.await?;
handle.abort();

// Channels
let (tx, rx) = mpsc::unbounded();
let (tx, rx) = oneshot::channel();

// Time
sleep(Duration::from_millis(100)).await;
timeout(Duration::from_secs(1), operation).await?;

// Network (simulated)
let listener = TcpListener::bind("127.0.0.1:8000").await?;
let (stream, _) = listener.accept().await?;

// Select
select! {
    val = rx.recv() => { ... }
    _ = sleep(timeout) => { ... }
}
```

### Chaos Control

```rust
// Inject chaos
chaos::inject(ChaosOperation::TcpWrite, 0.5);

// Clear chaos
chaos::clear(ChaosOperation::TcpWrite);

// Scenarios
let scenario = Scenario::new()
    .seed(42)
    .delay("addr1", min, max)
    .partition("addr1", "addr2")
    .wait(duration)
    .heal_partition("addr1", "addr2");

spawn(scenario.execute_chaos());
```

## Examples

Check out the [tests/](fracture/tests/) directory for complete examples:

- **[demo_success.rs]fracture/tests/demo_success.rs** - Resilient message passing with retries
- **[demo_failure.rs]fracture/tests/demo_failure.rs** - Non-resilient code that fails under chaos
- **[simple_test.rs]fracture/tests/simple_test.rs** - Basic async primitives
- **[modules_test.rs]fracture/tests/modules_test.rs** - Testing different async modules

## Comparison

| Feature | Tokio | Loom | Jepsen | **Fracture** |
|---------|-------|------|--------|------------|
| Async runtime | βœ… | ❌ | ❌ | βœ… |
| Deterministic | ❌ | βœ… | ❌ | βœ… |
| Chaos injection | ❌ | ❌ | βœ… | βœ… |
| Fast (in-memory) | βœ… | βœ… | ❌ | βœ… |
| Network simulation | ❌ | ❌ | βœ… | βœ… |
| Reproducible bugs | ❌ | βœ… | βœ… | βœ… |

- **Tokio**: Production runtime, no chaos testing
- **Loom**: Concurrency testing (different problem space)
- **Jepsen**: Live cluster testing (slow, expensive)
- **Fracture**: Deterministic chaos testing for async code

## Limitations & Alpha Status

⚠️ **This is alpha software (v0.1.0).** We've tested it extensively, but async runtimes are complex and there are undoubtedly edge cases we haven't hit yet.

**Known limitations:**

- Only works with Fracture's async primitives (not Tokio/async-std directly)
- Limited chaos operations compared to real-world scenarios
- No file I/O simulation yet
- Performance not optimized for massive scale (1000+ tasks may be slow)
- Some edge cases in the runtime may cause panics or incorrect behavior

**We NEED your bug reports!** 

If you find issues (crashes, incorrect behavior, missing features), please:
1. **[Open an issue]https://github.com/yourusername/fracture/issues** with details
2. Include the seed (`FRACTURE_SEED`) if the bug is reproducible
3. Minimal reproduction code is incredibly helpful

The more people use this in real scenarios, the more robust it becomes. Help us make async Rust more reliable by breaking Fracture first! πŸ”¨

## Contributing

Found a bug? Want a feature? Open an issue or PR!

Areas we'd love help with:
- More chaos operations
- Performance improvements
- Integration with existing async ecosystems
- Better error messages
- More examples

## Roadmap

- [ ] File I/O simulation and chaos
- [ ] Clock skew injection
- [ ] Memory pressure simulation
- [ ] Better integration with Tokio/async-std
- [ ] Visualization of test execution traces
- [ ] Property-based testing integration
- [ ] More comprehensive network simulation

## Why "Fracture"?

Because we intentionally **break** your code to make it stronger. Chaos testing finds the **fracture points** in your system before production does.

## License

MIT License. See [LICENSE](../LICENSE) for details.

## Acknowledgments

Inspired by:
- FoundationDB's deterministic simulation testing
- [Jepsen]https://jepsen.io/ for pioneering chaos testing
- The Rust async ecosystem (Tokio, async-std, futures)