test: consolidate formatter and hub policy cases
This commit is contained in:
1 parent
00fd0ebb15
commit
39d414d588
2 files changed
+56
-92
No files matched your search
+15
-37
@@ -1175,45 +1175,23 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn machine_echo_reply_metadata_unavailable_prints_none() {
|
||||
let line = format_event(&reply_event(), OutputMode::Machine).unwrap();
|
||||
fn machine_echo_reply_metadata_prints_observed_and_unavailable_values() {
|
||||
let cases = [
|
||||
(PacketMeta::default(), ("none", "none", "none", "none")),
|
||||
(packet_meta(0, 0, 0), ("0", "0", "0", "none")),
|
||||
(packet_meta(186, 46, 2), ("186", "46", "2", "none")),
|
||||
(
|
||||
packet_meta_with_timestamp(Some(UNIX_EPOCH + Duration::new(1, 234))),
|
||||
("none", "none", "none", "1000000234"),
|
||||
),
|
||||
];
|
||||
|
||||
assert_machine_packet_meta(&line, "none", "none", "none", "none");
|
||||
}
|
||||
for (packet_meta, (traffic_class, dscp, ecn, kernel_rx_ns)) in cases {
|
||||
let line =
|
||||
format_event(&reply_event_with_meta(packet_meta), OutputMode::Machine).unwrap();
|
||||
|
||||
#[test]
|
||||
fn machine_echo_reply_metadata_observed_zero_prints_zero() {
|
||||
let line = format_event(
|
||||
&reply_event_with_meta(packet_meta(0, 0, 0)),
|
||||
OutputMode::Machine,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_machine_packet_meta(&line, "0", "0", "0", "none");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn machine_echo_reply_metadata_dscp_46_ecn_2_prints_values() {
|
||||
let line = format_event(
|
||||
&reply_event_with_meta(packet_meta(186, 46, 2)),
|
||||
OutputMode::Machine,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_machine_packet_meta(&line, "186", "46", "2", "none");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn machine_echo_reply_metadata_kernel_rx_timestamp_prints_ns() {
|
||||
let line = format_event(
|
||||
&reply_event_with_meta(packet_meta_with_timestamp(Some(
|
||||
UNIX_EPOCH + Duration::new(1, 234),
|
||||
))),
|
||||
OutputMode::Machine,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_machine_packet_meta(&line, "none", "none", "none", "1000000234");
|
||||
assert_machine_packet_meta(&line, traffic_class, dscp, ecn, kernel_rx_ns);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -290,14 +290,16 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn publishes_to_one_subscriber() {
|
||||
let hub = EventHub::new();
|
||||
let sub = hub.subscribe(SubscriberConfig::default()).unwrap();
|
||||
|
||||
hub.publish(event(1));
|
||||
|
||||
assert_eq!(event_message(sub.try_recv().unwrap().unwrap()), "event-1");
|
||||
fn assert_next(
|
||||
sub: &EventSubscription,
|
||||
expected: Result<Option<&str>, EventSubscriptionError>,
|
||||
) {
|
||||
match (sub.try_recv(), expected) {
|
||||
(Ok(Some(event)), Ok(Some(message))) => assert_eq!(event_message(event), message),
|
||||
(Ok(None), Ok(None)) => {}
|
||||
(Err(actual), Err(expected)) => assert_eq!(actual, expected),
|
||||
(actual, expected) => panic!("expected {expected:?}, got {actual:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -313,56 +315,40 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_newest_keeps_existing_events() {
|
||||
let hub = EventHub::new();
|
||||
let sub = hub
|
||||
.subscribe(SubscriberConfig {
|
||||
capacity: 1,
|
||||
overflow: SubscriberOverflow::DropNewest,
|
||||
})
|
||||
.unwrap();
|
||||
fn full_subscriber_overflow_policy_is_applied() {
|
||||
let cases = [
|
||||
(
|
||||
SubscriberOverflow::DropNewest,
|
||||
Ok(Some("event-1")),
|
||||
Ok(None),
|
||||
),
|
||||
(
|
||||
SubscriberOverflow::DropOldest,
|
||||
Ok(Some("event-2")),
|
||||
Ok(None),
|
||||
),
|
||||
(
|
||||
SubscriberOverflow::Disconnect,
|
||||
Err(EventSubscriptionError::Disconnected),
|
||||
Err(EventSubscriptionError::Disconnected),
|
||||
),
|
||||
];
|
||||
|
||||
hub.publish(event(1));
|
||||
hub.publish(event(2));
|
||||
for (overflow, first_expected, second_expected) in cases {
|
||||
let hub = EventHub::new();
|
||||
let sub = hub
|
||||
.subscribe(SubscriberConfig {
|
||||
capacity: 1,
|
||||
overflow,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(event_message(sub.try_recv().unwrap().unwrap()), "event-1");
|
||||
assert!(sub.try_recv().unwrap().is_none());
|
||||
}
|
||||
hub.publish(event(1));
|
||||
hub.publish(event(2));
|
||||
|
||||
#[test]
|
||||
fn drop_oldest_replaces_existing_events() {
|
||||
let hub = EventHub::new();
|
||||
let sub = hub
|
||||
.subscribe(SubscriberConfig {
|
||||
capacity: 1,
|
||||
overflow: SubscriberOverflow::DropOldest,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
hub.publish(event(1));
|
||||
hub.publish(event(2));
|
||||
|
||||
assert_eq!(event_message(sub.try_recv().unwrap().unwrap()), "event-2");
|
||||
assert!(sub.try_recv().unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_removes_full_subscriber() {
|
||||
let hub = EventHub::new();
|
||||
let sub = hub
|
||||
.subscribe(SubscriberConfig {
|
||||
capacity: 1,
|
||||
overflow: SubscriberOverflow::Disconnect,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
hub.publish(event(1));
|
||||
hub.publish(event(2));
|
||||
|
||||
assert_eq!(
|
||||
sub.try_recv().unwrap_err(),
|
||||
EventSubscriptionError::Disconnected
|
||||
);
|
||||
assert_next(&sub, first_expected);
|
||||
assert_next(&sub, second_expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user