test_executors 0.4.1

Simple async executors for testing.
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-License-Identifier: MIT OR Apache-2.0
/*!
This crate provides extremely simple, yet useful, async executors. They are primarily useful for writing unit tests
without bringing in a full-blown executor such as [tokio](https://tokio.rs).

![logo](../../../art/logo.png)

# Quick Start

```
use test_executors::{spin_on, sleep_on};

// Run a simple async function
let result = spin_on(async {
    42
});
assert_eq!(result, 42);

// Run an async function that sleeps
let result = sleep_on(async {
    // Your async code here
    "Hello, async!"
});
assert_eq!(result, "Hello, async!");
```

# Available Executors

The crate provides three main executors:

* [`spin_on`] - Polls a future in a busy loop on the current thread. Best for CPU-bound tasks or when latency is critical.
* [`sleep_on`] - Polls a future on the current thread, sleeping between polls. Best for I/O-bound tasks to avoid burning CPU.
* [`spawn_on`] - Spawns a future on a new thread, polling it there. Best for fire-and-forget tasks.

# Platform Support

## Native Platforms
All executors work as described above on native platforms (Linux, macOS, Windows, etc.).

## WebAssembly Support
This crate has special support for `wasm32` targets:
- The `async_test` macro automatically adapts to use `wasm-bindgen-test` on WASM
- `spawn_local` uses `wasm_bindgen_futures::spawn_local` on WASM targets

# Features

## `async_test` Macro
The [`async_test`] macro allows you to write async tests that work on both native and WASM targets:

```
use test_executors::async_test;

#[async_test]
async fn my_test() {
    let value = async { 42 }.await;
    assert_eq!(value, 42);
}
```

## Integration with `some_executor`
This crate implements the [some_executor](https://crates.io/crates/some_executor) trait for all executors,
allowing them to be used in executor-agnostic code:

```
use test_executors::aruntime::SpinRuntime;
use some_executor::SomeExecutor;

let mut runtime = SpinRuntime::new();
// Use runtime with some_executor traits
```

# Utilities

The crate also provides utility functions and types:
- [`poll_once`] and [`poll_once_pin`] - Poll a future exactly once
- [`spawn_local`] - Platform-aware spawning that works on both native and WASM

*/

pub mod aruntime;
mod noop_waker;
mod sys;

use crate::noop_waker::new_context;
use logwise::declare_logging_domain;
/**
Re-export `some_executor` crate to allow using this executor with `some_executor` traits.
*/
pub use some_executor;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
pub use test_executors_proc::async_test;
declare_logging_domain!();

extern crate self as test_executors;

/// Blocks the calling thread until a future is ready, using a spinloop.
///
/// This executor continuously polls the future in a tight loop without yielding the thread.
/// It's the most responsive executor but also the most CPU-intensive.
///
/// # When to Use
/// - When you need minimal latency
/// - For CPU-bound async tasks
/// - In tests where you want deterministic behavior
/// - When the future is expected to complete quickly
///
/// # Example
/// ```
/// use test_executors::spin_on;
///
/// let result = spin_on(async {
///     // Simulate some async work
///     let value = async { 21 }.await;
///     value * 2
/// });
/// assert_eq!(result, 42);
/// ```
///
/// # Performance Note
/// This executor will consume 100% CPU while waiting. For I/O-bound tasks or
/// long-running futures, consider using [`sleep_on`] instead.
pub fn spin_on<F: Future>(mut future: F) -> F::Output {
    //we inherit the parent dlog::context here.
    let mut context = new_context();
    let mut future = unsafe { Pin::new_unchecked(&mut future) };
    loop {
        if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
            return val;
        }
        std::hint::spin_loop();
    }
}

struct SimpleWakeShared {
    /// Flag indicating whether a wake has been signaled.
    /// Acts as a "sticky" notification - once set, it stays set until consumed.
    woken: Mutex<bool>,
    condvar: Condvar,
}

impl SimpleWakeShared {
    fn new() -> Self {
        Self {
            woken: Mutex::new(false),
            condvar: Condvar::new(),
        }
    }

    /// Signal that the future should be polled again.
    /// This is safe to call even if no one is waiting yet - the flag preserves the wake.
    fn signal(&self) {
        let mut guard = self.woken.lock().unwrap();
        *guard = true;
        self.condvar.notify_one();
    }

    /// Wait until signaled. Resets the flag after waking.
    fn wait(&self) {
        let mut guard = self.woken.lock().unwrap();
        while !*guard {
            guard = self.condvar.wait(guard).unwrap();
        }
        *guard = false;
    }
}

static CONDVAR_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
    // clone
    |ctx| {
        let ctx = unsafe { Arc::from_raw(ctx as *const SimpleWakeShared) };
        let ctx2 = ctx.clone();
        std::mem::forget(ctx);
        RawWaker::new(Arc::into_raw(ctx2) as *const (), &CONDVAR_WAKER_VTABLE)
    },
    // wake (takes ownership)
    |ctx| {
        let ctx = unsafe { Arc::from_raw(ctx as *const SimpleWakeShared) };
        logwise::trace_sync!("waking");
        ctx.signal();
    },
    // wake_by_ref
    |ctx| {
        let ctx = unsafe { Arc::from_raw(ctx as *const SimpleWakeShared) };
        logwise::trace_sync!("waking (by ref)");
        ctx.signal();
        std::mem::forget(ctx);
    },
    // drop
    |ctx| {
        let ctx = unsafe { Arc::from_raw(ctx as *const SimpleWakeShared) };
        std::mem::drop(ctx);
    },
);
/// Blocks the calling thread until a future is ready, sleeping between polls.
///
/// This executor uses a condition variable to sleep the thread when the future
/// returns `Poll::Pending`, waking up only when the waker is triggered.
/// This is more CPU-efficient than [`spin_on`] but may have higher latency.
///
/// # When to Use
/// - For I/O-bound async tasks
/// - When you want to avoid burning CPU cycles
/// - For longer-running futures
/// - In tests that involve actual async I/O or timers
///
/// # Example
/// ```
/// use test_executors::sleep_on;
/// use std::future::Future;
/// use std::pin::Pin;
/// use std::task::{Context, Poll};
///
/// # struct Counter {
/// #     count: u32,
/// # }
/// #
/// # impl Future for Counter {
/// #     type Output = u32;
/// #     
/// #     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
/// #         self.count += 1;
/// #         if self.count >= 3 {
/// #             Poll::Ready(self.count)
/// #         } else {
/// #             cx.waker().wake_by_ref();
/// #             Poll::Pending
/// #         }
/// #     }
/// # }
/// let result = sleep_on(Counter { count: 0 });
/// assert_eq!(result, 3);
/// ```
///
/// # Implementation Details
/// The executor will properly handle spurious wakeups and re-poll the future
/// as needed. The waker implementation uses a semaphore to signal readiness.
pub fn sleep_on<F: Future>(mut future: F) -> F::Output {
    //we inherit the parent dlog::context here.
    let shared = Arc::new(SimpleWakeShared::new());
    let local = shared.clone();
    let raw_waker = RawWaker::new(Arc::into_raw(shared) as *const (), &CONDVAR_WAKER_VTABLE);
    let waker = unsafe { Waker::from_raw(raw_waker) };
    let mut context = Context::from_waker(&waker);
    let mut future = unsafe { Pin::new_unchecked(&mut future) };

    loop {
        logwise::trace_sync!("polling future");
        if let Poll::Ready(val) = future.as_mut().poll(&mut context) {
            logwise::trace_sync!("future is ready");
            return val;
        }
        logwise::trace_sync!("future is not ready");
        // The boolean flag in SimpleWakeShared acts as a "sticky" notification,
        // so wakes that happen during poll() are not lost.
        local.wait();
        logwise::trace_sync!("woken");
    }
}

/// Spawns a future on a new thread and returns immediately without waiting for completion.
///
/// This function creates a new OS thread with the given name and runs the future on that
/// thread using [`sleep_on`]. The calling thread returns immediately, making this useful
/// for fire-and-forget tasks.
///
/// # Parameters
/// - `thread_name`: The name to give to the spawned thread (must be a static string)
/// - `future`: The future to execute on the new thread
///
/// # Requirements
/// - The future must be `Send` because it will be moved to another thread
/// - The future must be `'static` because the spawned thread may outlive the caller
///
/// # Example
/// ```
/// use test_executors::spawn_on;
/// use std::sync::{Arc, Mutex};
/// use std::time::Duration;
///
/// let data = Arc::new(Mutex::new(Vec::new()));
/// let data_clone = data.clone();
///
/// spawn_on("worker", async move {
///     // Simulate some async work
///     data_clone.lock().unwrap().push(42);
/// });
///
/// // Give the spawned thread time to complete
/// std::thread::sleep(Duration::from_millis(50));
///
/// // Check the result
/// assert_eq!(*data.lock().unwrap(), vec![42]);
/// ```
///
/// # Panics
/// Panics if the thread cannot be spawned (e.g., due to resource exhaustion).
///
/// # See Also
/// - [`spawn_local`] for a platform-aware version that works on WASM
pub fn spawn_on<F: Future + Send + 'static>(thread_name: &'static str, future: F) {
    let prior_context = logwise::context::Context::current();
    let new_context = logwise::context::Context::new_task(
        Some(prior_context),
        thread_name.to_string(),
        logwise::Level::Info,
        true,
    );
    std::thread::Builder::new()
        .name(thread_name.to_string())
        .spawn(move || {
            let pushed_id = new_context.context_id();
            logwise::context::Context::set_current(new_context);

            sleep_on(future);
            logwise::context::Context::pop(pushed_id);
        })
        .expect("Cant spawn thread");
}

/// Spawns a future in a platform-appropriate way without waiting for completion.
///
/// This function automatically selects the appropriate executor based on the target platform:
/// - On native platforms (Linux, macOS, Windows, etc.): Uses [`sleep_on`] to run the future
///   on the current thread
/// - On `wasm32` targets: Uses `wasm_bindgen_futures::spawn_local` to integrate with the
///   browser's event loop
///
/// # Parameters
/// - `future`: The future to execute
/// - `_debug_label`: A label for debugging purposes (used for logging context on WASM)
///
/// # Example
/// ```
/// use test_executors::spawn_local;
///
/// spawn_local(async {
///     // This will run correctly on both native and WASM platforms
///     println!("Hello from async!");
/// }, "example_task");
/// ```
///
/// # Platform Behavior
/// ## Native Platforms
/// The future is executed immediately on the current thread using [`sleep_on`].
/// This blocks until the future completes.
///
/// ## WebAssembly
/// The future is scheduled to run on the browser's event loop and this function
/// returns immediately. The future will run when the JavaScript runtime is ready.
///
/// # Note
/// Unlike [`spawn_on`], this function does not require the future to be `Send`
/// since it may run on the current thread.
pub fn spawn_local<F: Future + 'static>(future: F, _debug_label: &'static str) {
    #[cfg(not(target_arch = "wasm32"))]
    sleep_on(future);
    #[cfg(target_arch = "wasm32")]
    {
        let c = logwise::context::Context::current();
        let new_context = logwise::context::Context::new_task(
            Some(c),
            _debug_label.to_string(),
            logwise::Level::Info,
            true,
        );
        wasm_bindgen_futures::spawn_local(async move {
            logwise::context::ApplyContext::new(new_context, future).await;
        });
    }
}

/// Polls a pinned future exactly once and returns the result.
///
/// This function is useful for testing futures or implementing custom executors.
/// It creates a no-op waker that does nothing when `wake()` is called.
///
/// # Parameters
/// - `future`: A pinned mutable reference to the future to poll
///
/// # Returns
/// - `Poll::Ready(value)` if the future completed on this poll
/// - `Poll::Pending` if the future is not yet ready
///
/// # Example
/// ```
/// use test_executors::poll_once;
/// use std::task::Poll;
///
/// let mut future = std::future::pending::<()>();
/// let result = poll_once(std::pin::Pin::new(&mut future));
/// assert_eq!(result, Poll::Pending);
/// ```
///
/// # Testing Example
/// ```
/// use test_executors::poll_once;
/// use std::future::Future;
/// use std::pin::Pin;
/// use std::task::{Context, Poll};
///
/// struct CounterFuture {
///     count: u32,
/// }
///
/// impl Future for CounterFuture {
///     type Output = u32;
///     
///     fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
///         self.count += 1;
///         if self.count >= 3 {
///             Poll::Ready(self.count)
///         } else {
///             Poll::Pending
///         }
///     }
/// }
///
/// let mut future = CounterFuture { count: 0 };
/// let mut pinned = std::pin::pin!(future);
///
/// assert_eq!(poll_once(pinned.as_mut()), Poll::Pending);
/// assert_eq!(poll_once(pinned.as_mut()), Poll::Pending);
/// assert_eq!(poll_once(pinned.as_mut()), Poll::Ready(3));
/// ```
///
/// # See Also
/// - [`poll_once_pin`] for a version that takes ownership and pins the future for you
pub fn poll_once<F: Future>(future: Pin<&mut F>) -> Poll<F::Output> {
    let mut context = new_context();
    future.poll(&mut context)
}

/// Polls a future exactly once after pinning it.
///
/// This is a convenience function that takes ownership of the future, pins it,
/// and polls it once. Unlike [`poll_once`], you don't need to pin the future yourself.
///
/// # Parameters
/// - `future`: The future to poll (takes ownership)
///
/// # Returns
/// - `Poll::Ready(value)` if the future completed on this poll
/// - `Poll::Pending` if the future is not yet ready
///
/// # Example
/// ```
/// use test_executors::poll_once_pin;
/// use std::task::Poll;
///
/// let future = std::future::pending::<()>();
/// let result = poll_once_pin(future);
/// assert_eq!(result, Poll::Pending);
/// ```
///
/// # Comparison with `poll_once`
/// ```
/// use test_executors::{poll_once, poll_once_pin};
/// use std::task::Poll;
///
/// // Using poll_once_pin (takes ownership)
/// let future1 = async { 42 };
/// assert_eq!(poll_once_pin(future1), Poll::Ready(42));
///
/// // Using poll_once (borrows)
/// let mut future2 = async { 42 };
/// let mut pinned = std::pin::pin!(future2);
/// assert_eq!(poll_once(pinned.as_mut()), Poll::Ready(42));
/// ```
///
/// # Limitations
/// Since this function takes ownership of the future, you cannot poll it again
/// after calling this function. If you need to poll a future multiple times,
/// use [`poll_once`] instead.
pub fn poll_once_pin<F: Future>(future: F) -> Poll<F::Output> {
    let mut context = new_context();
    let pinned = std::pin::pin!(future);
    pinned.poll(&mut context)
}

#[cfg(test)]
mod tests {
    use std::future::Future;
    use std::task::Poll;

    #[cfg(target_arch = "wasm32")]
    wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

    #[test]
    fn test_sleep_reentrant() {
        struct F(bool);
        impl Future for F {
            type Output = ();
            fn poll(
                mut self: std::pin::Pin<&mut Self>,
                cx: &mut std::task::Context<'_>,
            ) -> std::task::Poll<Self::Output> {
                if !self.0 {
                    self.0 = true;
                    cx.waker().wake_by_ref();
                    Poll::Pending
                } else {
                    Poll::Ready(())
                }
            }
        }
        let f = F(false);
        super::sleep_on(f);
    }

    #[crate::async_test]
    async fn hello_world() {
        let f = async { "hello world" };
        assert_eq!(f.await, "hello world");
    }

    #[test]
    fn poll_once_test() {
        let f = std::future::pending::<()>();
        let mut pinned = std::pin::pin!(f);
        let result = super::poll_once(pinned.as_mut());
        assert_eq!(result, Poll::Pending);

        let result2 = super::poll_once(pinned.as_mut());
        assert_eq!(result2, Poll::Pending);
    }
}