Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-metrics-subscriber/src/test_utils.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use std::sync::{Arc, LazyLock, Mutex};
5
6 use s2n_tls::{
7 security::{DEFAULT_TLS13, Policy},
8 testing::{TestPair, build_config, config_builder},
9 };
10
11 use crate::{
12 AggregatedMetricsSubscriber, Attribution, MetricRecord, telemetry_sink::TelemetrySink,
13 };
14
15 pub(crate) static ARBITRARY_POLICY_1: LazyLock<Policy> =
16 LazyLock::new(|| Policy::from_version("20240503").unwrap());
17
18 /// A test helper that implements [`TelemetrySink`] by collecting records into a Vec.
19 #[derive(Debug, Clone)]
20 pub(crate) struct VecSink {
21 pub(crate) records: Arc<Mutex<Vec<MetricRecord>>>,
22 }
23
24 impl VecSink {
25 pub(crate) fn new() -> Self {
26 Self {
27 records: Arc::new(Mutex::new(Vec::new())),
28 }
29 }
30 }
31
32 impl TelemetrySink for VecSink {
33 fn export_record(&self, record: &MetricRecord) {
34 self.records.lock().unwrap().push(record.clone());
35 }
36 }
37
38 pub(crate) struct TestEndpoint<S: TelemetrySink> {
39 pub server_config: s2n_tls::config::Config,
40 pub subscriber: AggregatedMetricsSubscriber<S>,
41 pub sink: S,
42 }
43
44 impl<S: TelemetrySink> TestEndpoint<S> {
45 pub fn client_handshake(&self, client_policy: &Policy) -> TestPair {
46 let client_config = build_config(client_policy).unwrap();
47 let mut pair = TestPair::from_configs(&client_config, &self.server_config);
48 pair.handshake().unwrap();
49 pair
50 }
51 }
52
53 impl TestEndpoint<VecSink> {
54 pub fn new() -> Self {
55 let sink = VecSink::new();
56 let attribution = Attribution {
57 service: "test_server".to_owned(),
58 resource: "test_resource".to_owned(),
59 component: "test_component".to_owned(),
60 };
61 let subscriber = AggregatedMetricsSubscriber::new(sink.clone(), attribution);
62 let server_config = {
63 let mut config = config_builder(&DEFAULT_TLS13).unwrap();
64 config.set_event_subscriber(subscriber.clone()).unwrap();
65 config.build().unwrap()
66 };
67 Self {
68 server_config,
69 subscriber,
70 sink,
71 }
72 }
73 }