Skip to content

Commit dd635f0

Browse files
authored
Merge pull request #5669 from stacks-network/fix/event_observer_skip_retry_tests
fixed race condition in tests assuming TEST_EVENT_OBSERVER_SKIP_RETRY…
2 parents a5a6ce6 + 64733db commit dd635f0

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

Cargo.lock

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testnet/stacks-node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ tiny_http = "0.12.0"
5151
http-types = "2.12"
5252
tempfile = "3.3"
5353
mockito = "1.5"
54+
serial_test = "3.2.0"
5455

5556
[[bin]]
5657
name = "stacks-node"

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use std::collections::hash_map::Entry;
1818
use std::collections::{HashMap, HashSet};
1919
use std::path::PathBuf;
2020
use std::sync::mpsc::{channel, Receiver, Sender};
21+
#[cfg(test)]
22+
use std::sync::LazyLock;
2123
use std::sync::{Arc, Mutex};
2224
use std::thread::sleep;
2325
use std::time::Duration;
@@ -330,7 +332,7 @@ impl RewardSetEventPayload {
330332
}
331333

332334
#[cfg(test)]
333-
static TEST_EVENT_OBSERVER_SKIP_RETRY: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
335+
static TEST_EVENT_OBSERVER_SKIP_RETRY: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
334336

335337
impl EventObserver {
336338
fn init_db(db_path: &str) -> Result<Connection, db_error> {
@@ -440,11 +442,7 @@ impl EventObserver {
440442
Self::send_payload_directly(&payload, &url, timeout);
441443

442444
#[cfg(test)]
443-
if TEST_EVENT_OBSERVER_SKIP_RETRY
444-
.lock()
445-
.unwrap()
446-
.unwrap_or(false)
447-
{
445+
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
448446
warn!("Fault injection: delete_payload");
449447
return;
450448
}
@@ -509,11 +507,7 @@ impl EventObserver {
509507
}
510508

511509
#[cfg(test)]
512-
if TEST_EVENT_OBSERVER_SKIP_RETRY
513-
.lock()
514-
.unwrap()
515-
.unwrap_or(false)
516-
{
510+
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
517511
warn!("Fault injection: skipping retry of payload");
518512
return;
519513
}
@@ -1759,6 +1753,7 @@ mod test {
17591753
use std::time::Instant;
17601754

17611755
use clarity::vm::costs::ExecutionCost;
1756+
use serial_test::serial;
17621757
use stacks::burnchains::{PoxConstants, Txid};
17631758
use stacks::chainstate::nakamoto::{NakamotoBlock, NakamotoBlockHeader};
17641759
use stacks::chainstate::stacks::db::{StacksBlockHeaderTypes, StacksHeaderInfo};
@@ -2042,6 +2037,7 @@ mod test {
20422037
}
20432038

20442039
#[test]
2040+
#[serial]
20452041
fn test_process_pending_payloads() {
20462042
use mockito::Matcher;
20472043

@@ -2065,6 +2061,8 @@ mod test {
20652061

20662062
let url = &format!("{}/api", &server.url());
20672063

2064+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
2065+
20682066
// Insert payload
20692067
EventObserver::insert_payload(&conn, url, &payload, timeout)
20702068
.expect("Failed to insert payload");
@@ -2115,6 +2113,7 @@ mod test {
21152113
}
21162114

21172115
#[test]
2116+
#[serial]
21182117
fn test_send_payload_with_db() {
21192118
use mockito::Matcher;
21202119

@@ -2136,6 +2135,8 @@ mod test {
21362135

21372136
let observer = EventObserver::new(Some(working_dir), endpoint, timeout);
21382137

2138+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
2139+
21392140
// Call send_payload
21402141
observer.send_payload(&payload, "/test");
21412142

@@ -2262,6 +2263,7 @@ mod test {
22622263
}
22632264

22642265
#[test]
2266+
#[serial]
22652267
fn test_send_payload_timeout() {
22662268
let port = get_random_port();
22672269
let timeout = Duration::from_secs(3);
@@ -2324,6 +2326,7 @@ mod test {
23242326
}
23252327

23262328
#[test]
2329+
#[serial]
23272330
fn test_send_payload_with_db_force_restart() {
23282331
let port = get_random_port();
23292332
let timeout = Duration::from_secs(3);
@@ -2391,18 +2394,15 @@ mod test {
23912394

23922395
// Disable retrying so that it sends the payload only once
23932396
// and that payload will be ignored by the test server.
2394-
TEST_EVENT_OBSERVER_SKIP_RETRY.lock().unwrap().replace(true);
2397+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(true);
23952398

23962399
info!("Sending payload 1");
23972400

23982401
// Send the payload
23992402
observer.send_payload(&payload, "/test");
24002403

24012404
// Re-enable retrying
2402-
TEST_EVENT_OBSERVER_SKIP_RETRY
2403-
.lock()
2404-
.unwrap()
2405-
.replace(false);
2405+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
24062406

24072407
info!("Sending payload 2");
24082408

0 commit comments

Comments
 (0)