From 80101cd5dbca8794171df95407053aa65d88013b Mon Sep 17 00:00:00 2001 From: FujiApple Date: Sat, 6 Jan 2024 21:59:19 +0800 Subject: [PATCH] move to async #4 --- .github/workflows/ci.yml | 203 +++++++++++++++++++++------------------ Cargo.toml | 2 +- tests/sim/tests.rs | 24 +++-- tests/sim/tracer.rs | 7 +- 4 files changed, 136 insertions(+), 100 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e0111890..0b4dc575d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,67 +7,67 @@ on: name: CI jobs: - test: - runs-on: ${{ matrix.os }} - strategy: - matrix: - build: - - linux-stable - - linux-musl-stable - - linux-beta - - linux-nightly - - macos-stable - - windows-stable - include: - - build: linux-stable - os: ubuntu-20.04 - target: x86_64-unknown-linux-gnu - rust: stable - - build: linux-musl-stable - os: ubuntu-20.04 - target: x86_64-unknown-linux-musl - rust: stable - - build: linux-beta - os: ubuntu-22.04 - target: x86_64-unknown-linux-gnu - rust: beta - - build: linux-nightly - os: ubuntu-22.04 - target: x86_64-unknown-linux-gnu - rust: nightly - - build: macos-stable - os: macos-latest - target: x86_64-apple-darwin - rust: stable - - build: windows-stable - os: windows-latest - target: x86_64-pc-windows-msvc - rust: stable - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: ${{ matrix.rust }} - target: ${{ matrix.target }} - - run: cargo test --target ${{ matrix.target }} +# test: +# runs-on: ${{ matrix.os }} +# strategy: +# matrix: +# build: +# - linux-stable +# - linux-musl-stable +# - linux-beta +# - linux-nightly +# - macos-stable +# - windows-stable +# include: +# - build: linux-stable +# os: ubuntu-20.04 +# target: x86_64-unknown-linux-gnu +# rust: stable +# - build: linux-musl-stable +# os: ubuntu-20.04 +# target: x86_64-unknown-linux-musl +# rust: stable +# - build: linux-beta +# os: ubuntu-22.04 +# target: x86_64-unknown-linux-gnu +# rust: beta +# - build: linux-nightly +# os: ubuntu-22.04 +# target: x86_64-unknown-linux-gnu +# rust: nightly +# - build: macos-stable +# os: macos-latest +# target: x86_64-apple-darwin +# rust: stable +# - build: windows-stable +# os: windows-latest +# target: x86_64-pc-windows-msvc +# rust: stable +# steps: +# - uses: actions/checkout@v4 +# - uses: dtolnay/rust-toolchain@stable +# with: +# toolchain: ${{ matrix.rust }} +# target: ${{ matrix.target }} +# - run: cargo test --target ${{ matrix.target }} sim-test: runs-on: ${{ matrix.os }} strategy: matrix: build: - - linux-stable - - macos-stable +# - linux-stable +# - macos-stable - windows-stable include: - - build: linux-stable - os: ubuntu-20.04 - target: x86_64-unknown-linux-gnu - rust: stable - - build: macos-stable - os: macos-latest - target: x86_64-apple-darwin - rust: stable +# - build: linux-stable +# os: ubuntu-20.04 +# target: x86_64-unknown-linux-gnu +# rust: stable +# - build: macos-stable +# os: macos-latest +# target: x86_64-apple-darwin +# rust: stable - build: windows-stable os: windows-latest target: x86_64-pc-windows-msvc @@ -94,47 +94,68 @@ jobs: if: ${{ ! startsWith(matrix.build, 'windows') }} run: sudo -E env "PATH=$PATH" cargo test --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture + - name: Run simulation test on ${{ matrix.build }} + if: startsWith(matrix.build, 'windows') + run: cargo test --target --target ${{ matrix.target }} --features sim-tests --test sim -- --exact --nocapture - fmt: - runs-on: ubuntu-22.04 - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: stable - components: rustfmt - - run: cargo fmt --all -- --check - - clippy: - runs-on: ${{ matrix.os }} - strategy: - matrix: - build: - - linux-stable - - macos-stable - - windows-stable - include: - - build: linux-stable - os: ubuntu-20.04 - target: x86_64-unknown-linux-gnu - rust: stable - - build: macos-stable - os: macos-latest - target: x86_64-apple-darwin - rust: stable - - build: windows-stable - os: windows-latest - target: x86_64-pc-windows-msvc - rust: stable - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: ${{ matrix.rust }} - components: clippy - - run: cargo clippy --workspace --all-features --target ${{ matrix.target }} --tests -- -Dwarnings +# fmt: +# runs-on: ubuntu-22.04 +# steps: +# - uses: actions/checkout@v4 +# - uses: dtolnay/rust-toolchain@stable +# with: +# toolchain: stable +# components: rustfmt +# - run: cargo fmt --all -- --check +# +# clippy: +# runs-on: ${{ matrix.os }} +# strategy: +# matrix: +# build: +# - linux-stable +# - macos-stable +# - windows-stable +# include: +# - build: linux-stable +# os: ubuntu-20.04 +# target: x86_64-unknown-linux-gnu +# rust: stable +# - build: macos-stable +# os: macos-latest +# target: x86_64-apple-darwin +# rust: stable +# - build: windows-stable +# os: windows-latest +# target: x86_64-pc-windows-msvc +# rust: stable +# steps: +# - uses: actions/checkout@v4 +# - uses: dtolnay/rust-toolchain@stable +# with: +# toolchain: ${{ matrix.rust }} +# components: clippy +# - run: cargo clippy --workspace --all-features --target ${{ matrix.target }} --tests -- -Dwarnings cargo-deny: runs-on: ubuntu-22.04 diff --git a/Cargo.toml b/Cargo.toml index 5cb012366..b84e12173 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,7 @@ test-case = "3.3.1" tun = { version = "0.6.1", features = [ "async" ] } serde_yaml = "0.9.30" tokio = { version = "1.35.1", features = [ "full" ] } -tokio-util = "0.7.10" +tokio-util = { version = "0.7.10" } [features] # Enable simulation integration tests diff --git a/tests/sim/tests.rs b/tests/sim/tests.rs index b75fbfd13..7a1d344fc 100644 --- a/tests/sim/tests.rs +++ b/tests/sim/tests.rs @@ -2,6 +2,7 @@ use crate::simulation::Simulation; use crate::tun_device::tun; use crate::{network, tracer}; use std::sync::Arc; +use std::time::Duration; use tokio_util::sync::CancellationToken; macro_rules! sim { @@ -31,17 +32,28 @@ async fn run_simulation(simulation: Simulation) -> anyhow::Result<()> { let handle = tokio::spawn(network::run(tun, sim.clone(), token.clone())); // spawn the tracer as a blocking task and wait for it to finish or fail. - tokio::task::spawn_blocking(move || tracer::Tracer::new(sim).trace()).await??; + // let handle2 = tokio::task::spawn_blocking(move || tracer::Tracer::new(sim, token).trace()); - println!("tracing complete"); + let handle2 = tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(2000)).await; + token.cancel(); + }); - // notify the network simulator to stop. - token.cancel(); + // println!("tracing complete"); - println!("cancel signalled"); + // // notify the network simulator to stop. + // token.cancel(); + // + // println!("cancel signalled"); // join the network simulator task once it has shutdown. - handle.await??; + // handle.await??; + + // handle.await??; + + let (a, b) = tokio::try_join!(handle, handle2)?; + + a?; println!("joined sim"); diff --git a/tests/sim/tracer.rs b/tests/sim/tracer.rs index 878024bc5..9c60d05fb 100644 --- a/tests/sim/tracer.rs +++ b/tests/sim/tracer.rs @@ -1,17 +1,19 @@ use crate::simulation::{Response, Simulation, SingleHost}; use std::num::NonZeroUsize; use std::sync::Arc; +use tokio_util::sync::CancellationToken; use trippy::tracing::{ Builder, CompletionReason, MaxRounds, ProbeStatus, TimeToLive, TraceId, TracerRound, }; pub struct Tracer { sim: Arc, + pub token: CancellationToken, } impl Tracer { - pub fn new(sim: Arc) -> Self { - Self { sim } + pub fn new(sim: Arc, token: CancellationToken) -> Self { + Self { sim, token } } pub fn trace(&self) -> anyhow::Result<()> { @@ -19,6 +21,7 @@ impl Tracer { .trace_identifier(TraceId(self.sim.icmp_identifier)) .max_rounds(MaxRounds(NonZeroUsize::MIN)) .start()?; + self.token.cancel(); Ok(()) }