Skip to content

Commit 0c4cfc9

Browse files
authored
test(s2n-events): Generate test events system (#2795)
1 parent f51926a commit 0c4cfc9

File tree

13 files changed

+1854
-11
lines changed

13 files changed

+1854
-11
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -669,23 +669,26 @@ jobs:
669669
with:
670670
submodules: true
671671

672-
# nightly features are used for formatting
673-
- name: Install rust nightly toolchain
674-
id: nightly-toolchain
675-
run: |
676-
rustup toolchain install nightly --component rustfmt
677-
678-
- name: Install rust stable toolchain
679-
id: stable-toolchain
672+
- name: Install rust toolchain
673+
id: toolchain
680674
run: |
681-
rustup toolchain install stable
682-
rustup override set stable
675+
rustup toolchain install ${{ env.RUST_NIGHTLY_TOOLCHAIN }} --profile minimal --component rustfmt
676+
rustup override set ${{ env.RUST_NIGHTLY_TOOLCHAIN }}
683677
684678
- uses: camshaft/rust-cache@v1
685679

686680
- name: format
687681
working-directory: ./tools/s2n-events
688-
run: cargo +nightly fmt -- --check
682+
run: cargo fmt -- --check
683+
684+
- name: generate test events
685+
run: |
686+
cargo run --manifest-path ./tools/s2n-events/Cargo.toml
687+
688+
- name: ensure test events are up to date
689+
run: |
690+
# If this fails you need to run `cargo run --manifest-path ./tools/s2n-events/Cargo.toml`
691+
git diff --exit-code
689692
690693
- name: test
691694
working-directory: ./tools/s2n-events

tools/s2n-events/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ license = "Apache-2.0"
99
# This is a commit-time crate and should not be published
1010
publish = false
1111

12+
[features]
13+
testing = []
14+
1215
[dependencies]
1316
glob = "0.3"
1417
heck = "0.5"
@@ -18,3 +21,7 @@ syn = { version = "2", features = ["full", "extra-traits"] }
1821

1922
[workspace]
2023
members = ["."]
24+
25+
[dev-dependencies]
26+
s2n-quic-core = { path = "../../quic/s2n-quic-core", features = ["testing"] }
27+
tracing = { version = "0.1" }
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use proc_macro2::TokenStream;
5+
use quote::quote;
6+
use s2n_events::{parser, validation, Output, Result};
7+
8+
struct EventInfo<'a> {
9+
input_path: &'a str,
10+
output_path: &'a str,
11+
crate_name: &'a str,
12+
s2n_quic_core_path: TokenStream,
13+
builder: TokenStream,
14+
tracing_subscriber_def: TokenStream,
15+
}
16+
17+
impl EventInfo<'_> {
18+
fn test_c_ffi_events() -> Self {
19+
let tracing_subscriber_def = quote!(
20+
/// Emits events with [`tracing`](https://docs.rs/tracing)
21+
#[derive(Clone, Debug)]
22+
pub struct Subscriber {
23+
root: tracing::Span,
24+
}
25+
26+
impl Default for Subscriber {
27+
fn default() -> Self {
28+
let root = tracing::span!(target: "c_ffi_events_test", tracing::Level::DEBUG, "c_ffi_events_test");
29+
30+
Self {
31+
root,
32+
}
33+
}
34+
}
35+
36+
impl Subscriber {
37+
fn parent<M: crate::event::Meta>(&self, _meta: &M) -> Option<tracing::Id> {
38+
self.root.id()
39+
}
40+
}
41+
);
42+
43+
EventInfo {
44+
crate_name: "c_ffi_events",
45+
input_path: concat!(
46+
env!("CARGO_MANIFEST_DIR"),
47+
"/tests/c_ffi_events/events/**/*.rs"
48+
),
49+
output_path: concat!(env!("CARGO_MANIFEST_DIR"), "/tests/c_ffi_events/event"),
50+
s2n_quic_core_path: quote!(s2n_quic_core),
51+
builder: quote! {
52+
pub use s2n_quic_core::event::builder::SocketAddress;
53+
},
54+
tracing_subscriber_def,
55+
}
56+
}
57+
}
58+
59+
fn main() -> Result<()> {
60+
let event_paths = [EventInfo::test_c_ffi_events()];
61+
62+
for event_info in event_paths {
63+
let mut files = vec![];
64+
65+
let input_path = event_info.input_path;
66+
67+
for path in glob::glob(input_path)? {
68+
let path = path?;
69+
eprintln!("loading {}", path.canonicalize().unwrap().display());
70+
let file = std::fs::read_to_string(&path)?;
71+
files.push(parser::parse(&file, path).unwrap());
72+
}
73+
74+
// make sure events are in a deterministic order
75+
files.sort_by(|a, b| a.path.as_os_str().cmp(b.path.as_os_str()));
76+
77+
// validate the events
78+
validation::validate(&files);
79+
80+
let root = std::path::Path::new(event_info.output_path);
81+
let _ = std::fs::create_dir_all(root);
82+
let root = root.canonicalize()?;
83+
84+
let mut output = Output {
85+
s2n_quic_core_path: event_info.s2n_quic_core_path,
86+
builders: event_info.builder,
87+
tracing_subscriber_def: event_info.tracing_subscriber_def,
88+
crate_name: event_info.crate_name,
89+
root,
90+
..Default::default()
91+
};
92+
93+
output.generate(&files);
94+
}
95+
96+
Ok(())
97+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
pub use s2n_quic_core::event::{Event, IntoEvent, Timestamp};
5+
6+
#[cfg(any(test, feature = "testing"))]
7+
use s2n_quic_core::event::snapshot;
8+
9+
pub trait Meta: core::fmt::Debug {
10+
fn subject(&self) -> api::Subject;
11+
}
12+
13+
impl Meta for api::ConnectionMeta {
14+
fn subject(&self) -> api::Subject {
15+
builder::Subject::Connection { id: self.id }.into_event()
16+
}
17+
}
18+
19+
impl Meta for api::EndpointMeta {
20+
fn subject(&self) -> api::Subject {
21+
builder::Subject::Endpoint {}.into_event()
22+
}
23+
}
24+
25+
mod generated;
26+
pub use generated::*;
27+
pub use s2n_quic_core::time::Duration;
28+
29+
pub mod metrics {
30+
pub use crate::event::generated::metrics::*;
31+
pub use s2n_quic_core::event::metrics::Recorder;
32+
33+
pub mod aggregate {
34+
pub use crate::event::generated::metrics::aggregate::*;
35+
pub use s2n_quic_core::event::metrics::aggregate::{
36+
info, AsVariant, BoolRecorder, Info, Metric, NominalRecorder, Recorder, Registry, Units,
37+
};
38+
39+
pub mod probe {
40+
pub use crate::event::generated::metrics::probe::*;
41+
pub use s2n_quic_core::event::metrics::aggregate::probe::dynamic;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)