Skip to content

Commit dd8107a

Browse files
authored
chore(memory-connection-limits): fix tests
while addressing the flakiness of the tests I noticed the memory limit was not accurate according to the documentation, documentation mentions the limit [_is reached_](https://github.com/libp2p/rust-libp2p/blob/master/misc/memory-connection-limits/src/lib.rs#L85) not greater than. This PR fixes that. To address the flakiness due to now connections being probably a little bit smaller in memory footprint I gave room for a connection threshold in the tests. Pull-Request: #6134.
1 parent d0a6509 commit dd8107a

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

misc/memory-connection-limits/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const MAX_STALE_DURATION: Duration = Duration::from_millis(100);
8282
impl Behaviour {
8383
/// Sets the process memory usage threshold in absolute bytes.
8484
///
85-
/// New inbound and outbound connections will be denied when the threshold is reached.
85+
/// New inbound and outbound connections will be denied when the threshold is exceeded.
8686
pub fn with_max_bytes(max_allowed_bytes: usize) -> Self {
8787
Self {
8888
max_allowed_bytes,
@@ -95,7 +95,7 @@ impl Behaviour {
9595

9696
/// Sets the process memory usage threshold in the percentage of the total physical memory.
9797
///
98-
/// New inbound and outbound connections will be denied when the threshold is reached.
98+
/// New inbound and outbound connections will be denied when the threshold is exceeded.
9999
pub fn with_max_percentage(percentage: f64) -> Self {
100100
use sysinfo::{RefreshKind, System};
101101

misc/memory-connection-limits/tests/max_bytes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ use util::*;
3131

3232
#[tokio::test]
3333
async fn max_bytes() {
34+
// These tests use connections as unit to test the memory limit.
35+
// Each connection consumes approximately 1MB of memory, so we give
36+
// one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35.
3437
const CONNECTION_LIMIT: usize = 20;
35-
let max_allowed_bytes = CONNECTION_LIMIT * 1024 * 1024;
38+
let max_allowed_bytes = (CONNECTION_LIMIT - 1) * 1024 * 1024;
3639

3740
let mut network = Swarm::new_ephemeral_tokio(|_| TestBehaviour {
3841
connection_limits: Behaviour::with_max_bytes(max_allowed_bytes),

misc/memory-connection-limits/tests/max_percentage.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ async fn max_percentage() {
5959

6060
// Adds current mem usage to the limit and update
6161
let current_mem = memory_stats::memory_stats().unwrap().physical_mem;
62-
let max_allowed_bytes = current_mem + CONNECTION_LIMIT * 1024 * 1024;
62+
// These tests use connections as unit to test the memory limit.
63+
// Each connection consumes approximately 1MB of memory, so we give
64+
// one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35.
65+
let max_allowed_bytes = current_mem + (CONNECTION_LIMIT - 1) * 1024 * 1024;
6366
network.behaviour_mut().connection_limits = Behaviour::with_max_percentage(
6467
max_allowed_bytes as f64 / system_info.total_memory() as f64,
6568
);

0 commit comments

Comments
 (0)