File: larger_tasks.rs

package info (click to toggle)
rust-async-executor 1.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: makefile: 2; sh: 1
file content (99 lines) | stat: -rw-r--r-- 2,489 bytes parent folder | download | duplicates (2)
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
//! Test for larger tasks.

use async_executor::Executor;
use futures_lite::future::{self, block_on};
use futures_lite::prelude::*;

use std::sync::Arc;
use std::thread;
use std::time::Duration;

fn do_run<Fut: Future<Output = ()>>(mut f: impl FnMut(Arc<Executor<'static>>) -> Fut) {
    // This should not run for longer than two minutes.
    #[cfg(not(miri))]
    let _stop_timeout = {
        let (stop_timeout, stopper) = async_channel::bounded::<()>(1);
        thread::spawn(move || {
            block_on(async move {
                let timeout = async {
                    async_io::Timer::after(Duration::from_secs(2 * 60)).await;
                    eprintln!("test timed out after 2m");
                    std::process::exit(1)
                };

                let _ = stopper.recv().or(timeout).await;
            })
        });
        stop_timeout
    };

    let ex = Arc::new(Executor::new());

    // Test 1: Use the `run` command.
    block_on(ex.run(f(ex.clone())));

    // Test 2: Loop on `tick`.
    block_on(async {
        let ticker = async {
            loop {
                ex.tick().await;
            }
        };

        f(ex.clone()).or(ticker).await
    });

    // Test 3: Run on many threads.
    thread::scope(|scope| {
        let (_signal, shutdown) = async_channel::bounded::<()>(1);

        for _ in 0..16 {
            let shutdown = shutdown.clone();
            let ex = &ex;
            scope.spawn(move || block_on(ex.run(shutdown.recv())));
        }

        block_on(f(ex.clone()));
    });

    // Test 4: Tick loop on many threads.
    thread::scope(|scope| {
        let (_signal, shutdown) = async_channel::bounded::<()>(1);

        for _ in 0..16 {
            let shutdown = shutdown.clone();
            let ex = &ex;
            scope.spawn(move || {
                block_on(async move {
                    let ticker = async {
                        loop {
                            ex.tick().await;
                        }
                    };

                    shutdown.recv().or(ticker).await
                })
            });
        }

        block_on(f(ex.clone()));
    });
}

#[test]
fn smoke() {
    do_run(|ex| async move { ex.spawn(async {}).await });
}

#[test]
fn yield_now() {
    do_run(|ex| async move { ex.spawn(future::yield_now()).await })
}

#[test]
fn timer() {
    do_run(|ex| async move {
        ex.spawn(async_io::Timer::after(Duration::from_millis(5)))
            .await;
    })
}