Skip to content

Commit

Permalink
move to async #4
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Jan 6, 2024
1 parent e629c82 commit 80101cd
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 100 deletions.
203 changes: 112 additions & 91 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions tests/sim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");

Expand Down
7 changes: 5 additions & 2 deletions tests/sim/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
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<Simulation>,
pub token: CancellationToken,
}

impl Tracer {
pub fn new(sim: Arc<Simulation>) -> Self {
Self { sim }
pub fn new(sim: Arc<Simulation>, token: CancellationToken) -> Self {
Self { sim, token }
}

pub fn trace(&self) -> anyhow::Result<()> {
Builder::new(self.sim.target, |round| self.validate_round(round))
.trace_identifier(TraceId(self.sim.icmp_identifier))
.max_rounds(MaxRounds(NonZeroUsize::MIN))
.start()?;
self.token.cancel();
Ok(())
}

Expand Down

0 comments on commit 80101cd

Please sign in to comment.