Skip to content

Commit 7382886

Browse files
authored
fix(common): spurious detection off-by-one (#12262)
fix(common): spurious detection off-by-one and add threshold test
1 parent ad627ae commit 7382886

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/common/src/selectors.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl OpenChainClient {
106106
if is_connectivity_err(err) {
107107
warn!("spurious network detected for OpenChain");
108108
let previous = self.timedout_requests.fetch_add(1, Ordering::SeqCst);
109-
if previous >= self.max_timedout_requests {
109+
if previous + 1 >= self.max_timedout_requests {
110110
self.set_spurious();
111111
}
112112
}
@@ -642,4 +642,21 @@ mod tests {
642642
ParsedSignatures { signatures: Default::default(), ..Default::default() }
643643
);
644644
}
645+
646+
#[tokio::test]
647+
async fn spurious_marked_on_timeout_threshold() {
648+
// Use an unreachable local port to trigger a quick connect error.
649+
let client = OpenChainClient::new().expect("client must build");
650+
let url = "http://127.0.0.1:9"; // Discard port; typically closed and fails fast.
651+
652+
// After MAX_TIMEDOUT_REQ - 1 failures we should NOT be spurious.
653+
for i in 0..(MAX_TIMEDOUT_REQ - 1) {
654+
let _ = client.get_text(url).await; // expect an error and internal counter increment
655+
assert!(!client.is_spurious(), "unexpected spurious after {} failed attempts", i + 1);
656+
}
657+
658+
// The Nth failure (N == MAX_TIMEDOUT_REQ) should flip the spurious flag.
659+
let _ = client.get_text(url).await;
660+
assert!(client.is_spurious(), "expected spurious after threshold failures");
661+
}
645662
}

0 commit comments

Comments
 (0)