Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ impl TelemetryServer {
Arc::clone(&self.event_counter)
}

pub fn get_store(&self) -> Option<Arc<EventStore>> {
self.store.clone()
}

pub fn get_slot_tracker(&self) -> SlotTracker {
Arc::clone(&self.slot_tracker)
}
Expand Down
41 changes: 41 additions & 0 deletions tests/api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,47 @@ async fn test_node_event_count_after_burst() {
assert_eq!(json["event_count"], 1000);
}

/// Events refer to earlier ones by the id their node gave them (JIP-3): ids start
/// at 0 after NodeInformation, and a Dropped event takes the ids of the events it
/// stands for. Tests build those references from what `send_events` returns.
#[tokio::test]
async fn test_send_events_returns_server_event_ids() {
let (server, telemetry_server, telemetry_port, _) = setup_test_api().await;
let mut node = common::connect_test_node(telemetry_port, 5, &telemetry_server).await;

const SLOT_MICROS: u64 = 6_000_000;
let slot_100_at = common::now_jce_micros() - 10 * SLOT_MICROS;
let event_ids = common::send_events(
&mut node,
&[
common::best_block_event(slot_100_at, 100),
// Best-block changes for slots 101 and 102 did not fit the node's buffer.
Event::Dropped {
timestamp: slot_100_at + SLOT_MICROS,
last_timestamp: slot_100_at + 2 * SLOT_MICROS,
num: 2,
},
common::best_block_event(slot_100_at + 3 * SLOT_MICROS, 103),
],
)
.await;
assert_eq!(event_ids, [0, 1, 3]);
common::flush_all(&telemetry_server).await;

let response = server
.get(&format!("/api/nodes/{}/timeline", common::node_id_hex(5)))
.await;
assert_eq!(response.status_code(), StatusCode::OK);
let json: Value = response.json();
let stored_ids: Vec<u64> = json["events"]
.as_array()
.unwrap()
.iter()
.map(|event| event["event_id"].as_u64().unwrap())
.collect();
assert_eq!(stored_ids, [3, 1, 0], "newest first");
}

#[tokio::test]
async fn test_node_details_not_found() {
let (server, _, _, _) = setup_test_api().await;
Expand Down
23 changes: 18 additions & 5 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct TestNode {
server: Arc<TelemetryServer>,
node_id: String,
events_sent: u64,
next_event_id: u64,
}

/// Connect a fake node with the given id and wait until the server sees it.
Expand All @@ -78,16 +79,24 @@ pub async fn connect_test_node(port: u16, node_id: u8, server: &Arc<TelemetrySer
server: Arc::clone(server),
node_id: node_id_hex(node_id),
events_sent: 0,
next_event_id: 0,
}
}

/// Send events from a fake node and wait until the server has received them all,
/// so a following flush covers them.
/// so a following flush covers them. Returns the JIP-3 event ids the server
/// assigns, for events that refer to earlier ones.
#[allow(dead_code)]
pub async fn send_events(node: &mut TestNode, events: &[Event]) {
pub async fn send_events(node: &mut TestNode, events: &[Event]) -> Vec<u64> {
let mut event_ids = Vec::with_capacity(events.len());
for event in events {
let encoded = encode_message(event).unwrap();
node.stream.write_all(&encoded).await.unwrap();
event_ids.push(node.next_event_id);
node.next_event_id += match event {
Event::Dropped { num, .. } => *num,
_ => 1,
};
}
node.events_sent += events.len() as u64;

Expand All @@ -100,6 +109,7 @@ pub async fn send_events(node: &mut TestNode, events: &[Event]) {
node.events_sent, node.node_id
)
});
event_ids
}

/// Creates a test BlockSummary with reasonable default values
Expand Down Expand Up @@ -212,8 +222,7 @@ pub fn test_node_info(peer_id: [u8; 32]) -> NodeInformation {

/// Force-refresh all continuous aggregates used by grafana endpoints.
/// TimescaleDB continuous aggregates don't auto-refresh fast enough in tests.
#[allow(dead_code)]
pub async fn refresh_aggregates(pool: &sqlx::PgPool) {
async fn refresh_aggregates(pool: &sqlx::PgPool) {
// The refresh window covers a generous range so test data is always included
let aggregates = [
// Non-event aggregates (not affected by migration 020)
Expand Down Expand Up @@ -244,11 +253,15 @@ pub async fn refresh_aggregates(pool: &sqlx::PgPool) {
}
}

/// Flush batch_writer + trackers. Combined helper for grafana tests.
/// Flush batch_writer, trackers and the per-minute aggregates, so everything
/// sent so far is visible through the API.
#[allow(dead_code)]
pub async fn flush_all(server: &Arc<TelemetryServer>) {
server.flush_writes().await.expect("Flush writes failed");
server.flush_trackers_for_test().await;
if let Some(store) = server.get_store() {
refresh_aggregates(store.pool()).await;
}
}

/// Construct a Status event (event_type=10) with the given timestamp.
Expand Down
Loading
Loading