fluxion-stream-time 0.5.0

Time-based operators extending fluxion-stream with chrono timestamps
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
500
501
502
503
504
505
506
507
508
509
510
# Testing Time-Dependent Operators


Testing asynchronous code that relies on the passage of time (e.g., `debounce`, `throttle`, `delay`) presents unique challenges. This document outlines the strategies available, the specific constraints imposed by the Tokio runtime, and a recommended architecture for robust, runtime-agnostic testing.

---

## Table of Contents


1. [The Fundamental Challenge]#the-fundamental-challenge
2. [Testing Approaches]#testing-approaches
   - [Approach 1: Real Time]#approach-1-real-time-not-recommended
   - [Approach 2: Simulated Time (Global Clock)]#approach-2-simulated-time-global-clock
   - [Approach 3: Abstracted Time (Dependency Injection)]#approach-3-abstracted-time-dependency-injection
3. [Tokio-Specific Considerations]#tokio-specific-considerations
   - [`start_paused = true` vs. `pause()`]#start_paused--true-vs-pause
   - [The `yield_now()` Requirement]#the-yield_now-requirement
   - [Limitations of Simulated Time]#limitations-of-simulated-time
4. [Multi-Runtime Roadmap Implications]#multi-runtime-roadmap-implications
5. [The Recommended Architecture]#the-recommended-architecture
   - [Step 1: Define a Time Trait]#step-1-define-a-time-trait
   - [Step 2: Implement for Each Runtime]#step-2-implement-for-each-runtime
   - [Step 3: Inject into Operators]#step-3-inject-into-operators
   - [Step 4: Default for Ergonomics]#step-4-default-for-ergonomics
   - [Step 5: MockTimer for Tests]#step-5-mocktimer-for-tests
6. [Summary of Trade-offs]#summary-of-trade-offs
7. [Feature-Gated Runtime Implementations]#feature-gated-runtime-implementations
8. [Parameterized Tests Across Runtimes]#parameterized-tests-across-runtimes
9. [Assessment: Is the Current Approach Sufficient?]#assessment-is-the-current-approach-sufficient

---

## The Fundamental Challenge


In production, time flows continuously. A `debounce(500ms)` operator waits half a second after the last event before emitting. Testing this behavior with *real* time is problematic:

1.  **Performance**: A test suite with hundreds of time-dependent tests could take minutes or hours.
2.  **Flakiness**: System load, GC pauses, or scheduler jitter can cause real sleeps to over- or under-shoot, leading to non-deterministic failures.

The goal of any testing strategy is to **decouple the logic of time from the wall clock**, allowing tests to be fast, deterministic, and reproducible.

---

## Testing Approaches


### Approach 1: Real Time (Not Recommended)


Use actual `std::thread::sleep` or `tokio::time::sleep` with real durations.

| Aspect      | Details                                                                 |
|-------------|-------------------------------------------------------------------------|
| **Pros**    | Zero infrastructure; tests mirror production exactly.                   |
| **Cons**    | Extremely slow; inherently flaky; unsuitable for CI.                    |
| **Verdict** | **Avoid entirely.** Only acceptable for manual smoke tests.             |

---

### Approach 2: Simulated Time (Global Clock)


Use the runtime's built-in time-mocking facilities (e.g., `tokio::time::pause()` and `tokio::time::advance()`).

**Mechanism:**
- The runtime intercepts all calls to its timer APIs (`sleep`, `timeout`, `interval`).
- Time is "frozen" at a specific instant.
- Calling `advance(duration)` instantly fires all timers scheduled to expire within that window, without wall-clock delay.

| Aspect         | Details                                                                                                |
|----------------|--------------------------------------------------------------------------------------------------------|
| **Pros**       | No changes to production code; tests are fast; linear, readable test flow.                             |
| **Cons**       | Tightly coupled to a specific runtime; susceptible to concurrency races with `tokio::spawn`.           |
| **Verdict**    | **Good for single-runtime projects.** Requires careful synchronization (`yield_now()`).                |

---

### Approach 3: Abstracted Time (Dependency Injection)


Define an abstract `Timer` trait and inject the implementation into operators.

**Mechanism:**
- Production code calls `timer.sleep(duration)` instead of `tokio::time::sleep(duration)`.
- In production, `timer` is a `TokioTimer` (or `AsyncStdTimer`, `SmolTimer`, etc.).
- In tests, `timer` is a `MockTimer` that allows the test to manually control when each sleep completes.

| Aspect         | Details                                                                                                |
|----------------|--------------------------------------------------------------------------------------------------------|
| **Pros**       | Fully runtime-agnostic; 100% deterministic tests; no concurrency races; zero-cost with monomorphization.|
| **Cons**       | More verbose operator signatures; initial setup cost.                                                  |
| **Verdict**    | **Best for multi-runtime libraries.** The investment pays off in long-term maintainability.            |

---

## Tokio-Specific Considerations


### `start_paused = true` vs. `pause()`


Tokio offers two ways to freeze time:

1.  **`#[tokio::test(start_paused = true)]`**: Time is paused *before* the async runtime starts the test body. This is the most "aggressive" freeze.
2.  **Explicit `tokio::time::pause()`**: Time is paused at a specific point within the test, after some setup code has run.

**The Problem with `start_paused = true`:**

When time is paused from the very beginning, and an operator uses `tokio::spawn` to run background logic, there is a **race condition**:

```
Test Body                     Spawned Task
----------                     ------------
1. Creates operator
2. Calls advance(500ms)        (may not have run yet!)
                               3. Registers its Sleep timer
                               4. (Timer deadline is now in the "past")
```

In this scenario, the spawned task's timer never fires because the test advanced past its deadline before the task even registered it.

**Why `pause()` is often more stable:**

By calling `pause()` *after* the initial setup (channel creation, operator construction), you give the Tokio scheduler more opportunity to initialize background tasks before freezing the clock.

---

### The `yield_now()` Requirement

Even with `pause()`, you may need to use `tokio::task::yield_now().await` to ensure background tasks have progressed to their `await` points before advancing time. This "forces a context switch," allowing the executor to run pending tasks.

```rust
tx.send(value)?;
tokio::task::yield_now().await; // <-- Ensures operator's internal task has processed the send
advance(Duration::from_millis(500)).await;
```

This is a fragile pattern, as it relies on implementation details of the operator. However, it is often necessary when using Tokio's simulated time with operators that spawn internal tasks.

---

### Limitations of Simulated Time


1.  **Runtime Lock-in**: `tokio::time::pause()` only works with Tokio. It cannot be used with `async-std`, `smol`, or WASM runtimes.
2.  **Global State**: The paused clock is a process-global state, which can cause interference if tests run in parallel within the same process (though `#[tokio::test]` spawns isolated runtimes by default).
3.  **Opacity**: Tests become dependent on internal implementation details (e.g., whether the operator uses `spawn` or polls inline).

---

## Multi-Runtime Roadmap Implications


If the roadmap includes supporting `async-std`, `smol`, or `wasm-bindgen-futures`:

-   `tokio::time::pause()` is **not portable**. There is no equivalent in `async-std`.
-   Each runtime has its own timer wheel implementation.
-   The only portable solution is **Abstracted Time (Approach 3)**, where the timer is a generic parameter or injected dependency.

---

## The Recommended Architecture


To achieve the stated goals of **zero performance impact**, **minimal code intrusion**, and **full test control**, the following architecture is recommended:

### Step 1: Define a Time Trait


Create a trait that abstracts timer operations.

```rust
use std::future::Future;
use std::time::{Duration, Instant};

pub trait AsyncTimer: Send + Sync + 'static {
    type Sleep: Future<Output = ()> + Send;

    /// Creates a future that completes after `duration`.
    fn sleep(&self, duration: Duration) -> Self::Sleep;

    /// Returns the current instant according to this timer.
    fn now(&self) -> Instant;
}
```

### Step 2: Implement for Each Runtime


Provide zero-cost implementations for each supported runtime. Because these are unit structs with no fields, they compile away completely.

```rust
// --- Tokio ---
pub struct TokioTimer;

impl AsyncTimer for TokioTimer {
    type Sleep = tokio::time::Sleep;

    fn sleep(&self, duration: Duration) -> Self::Sleep {
        tokio::time::sleep(duration)
    }

    fn now(&self) -> Instant {
        tokio::time::Instant::now().into_std()
    }
}

// --- async-std (future) ---
// pub struct AsyncStdTimer;
// impl AsyncTimer for AsyncStdTimer { ... }
```

### Step 3: Inject into Operators


Use generics to accept any timer implementation. This avoids `Box<dyn ...>` overhead.

```rust
pub fn debounce_with_timer<S, T, Timer>(
    stream: S,
    duration: Duration,
    timer: Timer,
) -> impl Stream<Item = StreamItem<T>>
where
    S: Stream<Item = StreamItem<T>>,
    T: Send,
    Timer: AsyncTimer,
{
    DebounceStream {
        stream,
        duration,
        timer,
        // ...
    }
}
```

### Step 4: Default for Ergonomics


Provide a convenience function that uses the default runtime's timer, so users don't have to pass it explicitly.

```rust
/// Debounces a stream using the default Tokio timer.
pub fn debounce<S, T>(
    stream: S,
    duration: Duration,
) -> impl Stream<Item = StreamItem<T>>
where
    S: Stream<Item = StreamItem<T>>,
    T: Send,
{
    debounce_with_timer(stream, duration, TokioTimer)
}
```

### Step 5: MockTimer for Tests


Create a `MockTimer` that allows manual control over time.

```rust
use std::sync::{Arc, Mutex};
use std::task::Waker;

pub struct MockTimer {
    inner: Arc<Mutex<MockTimerState>>,
}

struct MockTimerState {
    current_time: Instant,
    pending_sleeps: Vec<(Instant, Waker)>, // deadline, waker
}

impl MockTimer {
    pub fn new() -> Self { ... }

    /// Advances the mock clock, waking all sleeps whose deadline has passed.
    pub fn advance(&self, duration: Duration) {
        let mut state = self.inner.lock().unwrap();
        state.current_time += duration;
        state.pending_sleeps.retain(|(deadline, waker)| {
            if *deadline <= state.current_time {
                waker.wake_by_ref();
                false // Remove from pending
            } else {
                true // Keep in pending
            }
        });
    }
}

impl AsyncTimer for MockTimer {
    type Sleep = MockSleep;
    fn sleep(&self, duration: Duration) -> Self::Sleep { ... }
    fn now(&self) -> Instant { ... }
}
```

With this setup, tests look like:

```rust
#[tokio::test]

async fn test_debounce() {
    let mock_timer = MockTimer::new();
    let (tx, stream) = test_channel();
    let mut debounced = debounce_with_timer(stream, Duration::from_millis(500), mock_timer.clone());

    tx.send(value)?;
    mock_timer.advance(Duration::from_millis(500));

    assert_eq!(debounced.next().await, Some(value));
}
```

**No `yield_now()`, no `pause()`, no race conditions.** The `MockTimer` is explicit, synchronous state.

---

## Summary of Trade-offs


| Approach                 | Performance Impact | Code Readability | Test Determinism | Multi-Runtime |
|--------------------------|--------------------|------------------|------------------|---------------|
| Real Time                | N/A (unusable)     | High             | None             | Yes           |
| Simulated (Tokio pause)  | None               | High             | Medium (races)   | **No**        |
| Abstracted (DI + Mock)   | None (monomorphic) | Medium           | **Full**         | **Yes**       |

---

## Feature-Gated Runtime Implementations


When supporting multiple runtimes, each timer implementation should be gated behind a Cargo feature to avoid pulling in unnecessary dependencies.

### Cargo.toml Configuration


```toml
[features]
default = ["tokio-runtime"]
tokio-runtime = ["tokio"]
async-std-runtime = ["async-std"]
smol-runtime = ["smol"]

[dependencies]
tokio = { version = "1", features = ["time"], optional = true }
async-std = { version = "1", optional = true }
smol = { version = "2", optional = true }
```

### Conditional Compilation


```rust
// The trait is always available
pub trait AsyncTimer: Send + Sync + 'static {
    type Sleep: Future<Output = ()> + Send;
    fn sleep(&self, duration: Duration) -> Self::Sleep;
}

// Each implementation is conditionally compiled
#[cfg(feature = "tokio-runtime")]

pub struct TokioTimer;

#[cfg(feature = "tokio-runtime")]

impl AsyncTimer for TokioTimer { ... }

#[cfg(feature = "async-std-runtime")]

pub struct AsyncStdTimer;

#[cfg(feature = "async-std-runtime")]

impl AsyncTimer for AsyncStdTimer { ... }
```

**Benefits:**
1.  **No bloat**: Users only compile the runtime they use.
2.  **No conflicts**: Avoids pulling in multiple async runtimes simultaneously.
3.  **Clear API**: Default convenience functions use whichever runtime feature is enabled.

This is the standard pattern used by ecosystem crates like `reqwest`, `sqlx`, and `tonic`.

---

## Parameterized Tests Across Runtimes


When the same test logic needs to run against multiple runtimes, several strategies are available:

### Option 1: Macros (Most Common)


Define test logic once, then invoke it for each runtime via a macro:

```rust
macro_rules! runtime_tests {
    ($runtime_attr:meta, $timer:expr) => {
        #[$runtime_attr]
        async fn test_debounce_emits_after_quiet_period() {
            let timer = $timer;
            // ... test body using `timer` ...
        }

        #[$runtime_attr]
        async fn test_debounce_resets_on_new_value() {
            let timer = $timer;
            // ... test body ...
        }
    };
}

#[cfg(feature = "tokio-runtime")]

mod tokio_tests {
    use super::*;
    runtime_tests!(tokio::test, TokioTimer);
}

#[cfg(feature = "async-std-runtime")]

mod async_std_tests {
    use super::*;
    runtime_tests!(async_std::test, AsyncStdTimer);
}
```

| Aspect      | Details                                                        |
|-------------|----------------------------------------------------------------|
| **Pros**    | No extra dependencies; works with any test harness.            |
| **Cons**    | Macro syntax can be verbose; test names include module prefix. |

---

### Option 2: `rstest` with Features


Use [`rstest`](https://crates.io/crates/rstest) for parameterized tests:

```rust
use rstest::rstest;

#[rstest]

#[cfg_attr(feature = "tokio-runtime", case::tokio(TokioTimer))]

#[cfg_attr(feature = "async-std-runtime", case::async_std(AsyncStdTimer))]

#[tokio::test] // or use a runtime-agnostic executor

async fn test_debounce<T: AsyncTimer>(#[case] timer: T) {
    // ... test body ...
}
```

| Aspect      | Details                                                                    |
|-------------|----------------------------------------------------------------------------|
| **Pros**    | Clean, declarative syntax.                                                 |
| **Cons**    | Requires choosing one `#[..::test]` attribute (or using a wrapper crate).  |

---

### Option 3: Generic Test Functions + Per-Runtime Modules


Write test logic as a generic async function, then call it from runtime-specific test modules:

```rust
// tests/common.rs
pub async fn debounce_test_logic<T: AsyncTimer>(timer: T) {
    // ... all assertions here ...
}

// tests/tokio_tests.rs
#[cfg(feature = "tokio-runtime")]

mod tokio {
    #[tokio::test]
    async fn test_debounce() {
        super::super::common::debounce_test_logic(TokioTimer).await;
    }
}

// tests/async_std_tests.rs
#[cfg(feature = "async-std-runtime")]

mod async_std {
    #[async_std::test]
    async fn test_debounce() {
        super::super::common::debounce_test_logic(AsyncStdTimer).await;
    }
}
```

| Aspect      | Details                                           |
|-------------|---------------------------------------------------|
| **Pros**    | No macros; simple function calls; easy to debug.  |
| **Cons**    | Boilerplate for each test invocation.             |

---

### Recommendation for Multi-Runtime Testing


For a multi-runtime library, **Option 1 (Macros)** or **Option 3 (Generic functions)** are the most pragmatic:

-   They require no extra dependencies.
-   They work with each runtime's native `#[..::test]` attribute.
-   They scale well as more operators are added.

The macro approach is more DRY, while the generic function approach is more explicit and easier to debug.

---

## Assessment: Is the Current Approach Sufficient?


### Current State


Fluxion currently uses **Simulated Time (Tokio pause)** for time-based operator tests. We have stabilized the tests by:

1.  Using explicit `tokio::time::pause()` instead of `start_paused = true` where needed.
2.  Adding `tokio::task::yield_now()` calls to synchronize with spawned tasks.

### Is It Good Enough?


**Yes, for the current phase.**

| Criterion                     | Assessment                                                                 |
|-------------------------------|----------------------------------------------------------------------------|
| **Tokio-Only Support**        | ✅ The current approach is appropriate for a Tokio-only library.            |
| **Test Stability**            | ✅ Tests pass reliably with `pause()` and `yield_now()`.                    |
| **Production Performance**    | ✅ No test infrastructure affects production code.                          |
| **Code Readability**          | ✅ Production code has no timer generics; tests are straightforward.        |

### When to Refactor


The refactoring to **Abstracted Time** should be prioritized when:

1.  **Multi-runtime support** (`async-std`, `smol`, WASM) is added to the roadmap.
2.  The number of time-based operators grows, making `yield_now()` boilerplate unacceptable.
3.  Flaky tests become a recurring CI issue.

Until then, the current approach represents a reasonable trade-off between engineering investment and immediate needs.