use super::*;
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
time::Duration,
};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
time::timeout,
};
use zebra_chain::{chain_sync_status::mock::MockSyncStatus, parameters::Network};
use zebra_network::{address_book_peers::MockAddressBookPeers, PeerSocketAddr};
fn config_for(addr: SocketAddr) -> Config {
Config {
listen_addr: Some(addr),
enforce_on_test_networks: true,
..Default::default()
}
}
fn peers_with_count(count: usize) -> MockAddressBookPeers {
let mut peers = MockAddressBookPeers::default();
for _ in 0..count {
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0);
let peer = PeerSocketAddr::from(socket);
peers.add_peer(peer);
}
peers
}
async fn http_get(addr: SocketAddr, path: &str) -> Option<(u16, String)> {
let mut stream = timeout(Duration::from_secs(2), tokio::net::TcpStream::connect(addr))
.await
.expect("connect timeout")
.expect("connect ok");
let request = format!(
"GET {} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
path
);
timeout(Duration::from_secs(2), stream.write_all(request.as_bytes()))
.await
.expect("write timeout")
.ok()?;
let mut buf = Vec::new();
timeout(Duration::from_secs(2), stream.read_to_end(&mut buf))
.await
.expect("read timeout")
.ok()?;
let text = String::from_utf8_lossy(&buf).to_string();
let status = text
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.and_then(|s| s.parse::<u16>().ok())
.unwrap_or(0);
Some((status, text))
}
async fn start_server() -> (JoinHandle<()>, SocketAddr) {
let cfg = config_for(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0));
let mut sync_status = MockSyncStatus::default();
sync_status.set_is_close_to_tip(true);
let (chain_tip_metrics_sender, chain_tip_metrics_receiver) = ChainTipMetrics::channel();
let _ = chain_tip_metrics_sender.send(ChainTipMetrics::new(Instant::now(), Some(0)));
let (task, addr) = init(
cfg,
Network::Mainnet,
chain_tip_metrics_receiver,
sync_status,
peers_with_count(1),
)
.await;
(task, addr.expect("server bound addr"))
}
#[tokio::test]
async fn healthy_and_ready_ok() {
let cfg = config_for(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0));
let mut sync_status = MockSyncStatus::default();
sync_status.set_is_close_to_tip(true);
let (chain_tip_metrics_sender, chain_tip_metrics_receiver) = ChainTipMetrics::channel();
let _ = chain_tip_metrics_sender.send(ChainTipMetrics::new(Instant::now(), Some(0)));
let (task, addr_opt) = init(
cfg,
Network::Mainnet,
chain_tip_metrics_receiver,
sync_status,
peers_with_count(1),
)
.await;
let addr = addr_opt.expect("server bound addr");
let (status_h, body_h) = http_get(addr, "/healthy").await.unwrap();
assert_eq!(status_h, 200, "healthy response: {}", body_h);
let (status_r, body_r) = http_get(addr, "/ready").await.unwrap();
assert_eq!(status_r, 200, "ready response: {}", body_r);
task.abort();
}
#[tokio::test]
async fn not_ready_when_syncing_or_lagging() {
let cfg = config_for(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0));
let sync_status = MockSyncStatus::default();
let (chain_tip_metrics_sender, chain_tip_metrics_receiver) = ChainTipMetrics::channel();
let _ = chain_tip_metrics_sender.send(ChainTipMetrics::new(Instant::now(), Some(0)));
let (task_syncing, addr_syncing) = init(
cfg.clone(),
Network::Mainnet,
chain_tip_metrics_receiver.clone(),
sync_status,
peers_with_count(1),
)
.await;
let addr_syncing = addr_syncing.expect("addr");
let (status_syncing, body_syncing) = http_get(addr_syncing, "/ready").await.unwrap();
assert_eq!(status_syncing, 503, "body: {}", body_syncing);
assert!(body_syncing.contains("syncing"));
task_syncing.abort();
let mut sync_status = MockSyncStatus::default();
sync_status.set_is_close_to_tip(true);
let _ = chain_tip_metrics_sender.send(ChainTipMetrics::new(Instant::now(), Some(5)));
let (task_lagging, addr_lagging) = init(
cfg,
Network::Mainnet,
chain_tip_metrics_receiver,
sync_status,
peers_with_count(1),
)
.await;
let addr_lagging = addr_lagging.expect("addr");
let (status_lagging, body_lagging) = http_get(addr_lagging, "/ready").await.unwrap();
assert_eq!(status_lagging, 503, "body: {}", body_lagging);
assert!(body_lagging.contains("lag=5"));
task_lagging.abort();
}
#[tokio::test]
async fn not_ready_when_tip_is_too_old() {
let mut cfg = config_for(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0));
cfg.ready_max_tip_age = Duration::from_secs(1);
let mut sync_status = MockSyncStatus::default();
sync_status.set_is_close_to_tip(true);
let (chain_tip_metrics_sender, chain_tip_metrics_receiver) = ChainTipMetrics::channel();
let _ = chain_tip_metrics_sender.send(ChainTipMetrics::new(
Instant::now()
.checked_sub(Duration::from_secs(10))
.expect("should not overflow"),
Some(0),
));
let (task, addr_opt) = init(
cfg,
Network::Mainnet,
chain_tip_metrics_receiver,
sync_status,
peers_with_count(1),
)
.await;
let addr = addr_opt.expect("addr");
let (status, body) = http_get(addr, "/ready").await.unwrap();
assert_eq!(status, 503, "body: {}", body);
assert!(body.contains("tip_age"));
task.abort();
}
#[test]
fn rate_limiting_drops_bursts() {
let start = Instant::now();
let mut num_recent_requests = 0;
let mut last_request_count_reset_time = start;
let mut allow_at = |now| {
allow_request(
&mut num_recent_requests,
&mut last_request_count_reset_time,
|| now,
)
};
for window_start in [
start,
start + RECENT_REQUEST_INTERVAL + Duration::from_nanos(1),
] {
for _ in 0..MAX_RECENT_REQUESTS {
assert!(allow_at(window_start));
}
assert!(!allow_at(window_start));
assert!(!allow_at(
window_start + RECENT_REQUEST_INTERVAL - Duration::from_nanos(1)
));
assert!(!allow_at(window_start + RECENT_REQUEST_INTERVAL));
assert!(allow_at(
window_start + RECENT_REQUEST_INTERVAL + Duration::from_nanos(1)
));
}
}
#[tokio::test]
async fn connection_serves_one_request() {
let (task, addr) = start_server().await;
let mut stream = tokio::net::TcpStream::connect(addr)
.await
.expect("connect ok");
stream
.write_all(
"GET /healthy HTTP/1.1\r\nHost: localhost\r\n\r\n"
.repeat(2)
.as_bytes(),
)
.await
.expect("write ok");
let mut buf = Vec::new();
timeout(Duration::from_secs(2), stream.read_to_end(&mut buf))
.await
.expect("the server closes the connection after one response")
.expect("read ok");
let text = String::from_utf8_lossy(&buf);
assert_eq!(
text.matches("HTTP/1.1 200 OK").count(),
1,
"responses: {text}"
);
task.abort();
}
#[tokio::test]
async fn idle_connection_is_closed() {
let (task, addr) = start_server().await;
let mut stream = tokio::net::TcpStream::connect(addr)
.await
.expect("connect ok");
let mut buf = [0; 1];
let read = timeout(HEADER_READ_TIMEOUT * 2, stream.read(&mut buf))
.await
.expect("the server closes idle connections");
assert!(matches!(read, Ok(0) | Err(_)), "unexpected read: {read:?}");
task.abort();
}
#[tokio::test]
async fn connections_past_the_limit_are_dropped() {
let (task, addr) = start_server().await;
let mut idle = Vec::new();
for _ in 0..MAX_CONNECTIONS {
idle.push(
tokio::net::TcpStream::connect(addr)
.await
.expect("connect ok"),
);
}
let mut stream = tokio::net::TcpStream::connect(addr)
.await
.expect("connect ok");
let _ = stream
.write_all(b"GET /healthy HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.await;
let mut buf = Vec::new();
let _ = timeout(Duration::from_secs(2), stream.read_to_end(&mut buf))
.await
.expect("the server closes connections past the limit");
assert!(
!String::from_utf8_lossy(&buf).contains("HTTP/1.1"),
"a connection past the limit was served"
);
drop(idle);
timeout(Duration::from_secs(2), async {
while !matches!(http_get(addr, "/healthy").await, Some((200, _))) {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("closed connections free their slots");
task.abort();
}