Skip to content

Commit 4585774

Browse files
committed
use LazyLock + TestFlag
1 parent e8f003a commit 4585774

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ use stacks_common::util::hash::{bytes_to_hex, Sha512Trunc256Sum};
7373
use stacks_common::util::secp256k1::MessageSignature;
7474
use url::Url;
7575

76+
#[cfg(test)]
77+
use std::sync::LazyLock;
78+
7679
#[cfg(any(test, feature = "testing"))]
7780
lazy_static! {
7881
/// Do not announce a signed/mined block to the network when set to true.
@@ -330,7 +333,7 @@ impl RewardSetEventPayload {
330333
}
331334

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

335338
impl EventObserver {
336339
fn init_db(db_path: &str) -> Result<Connection, db_error> {
@@ -440,11 +443,7 @@ impl EventObserver {
440443
Self::send_payload_directly(&payload, &url, timeout);
441444

442445
#[cfg(test)]
443-
if TEST_EVENT_OBSERVER_SKIP_RETRY
444-
.lock()
445-
.unwrap()
446-
.unwrap_or(false)
447-
{
446+
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
448447
warn!("Fault injection: delete_payload");
449448
return;
450449
}
@@ -509,11 +508,7 @@ impl EventObserver {
509508
}
510509

511510
#[cfg(test)]
512-
if TEST_EVENT_OBSERVER_SKIP_RETRY
513-
.lock()
514-
.unwrap()
515-
.unwrap_or(false)
516-
{
511+
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
517512
warn!("Fault injection: skipping retry of payload");
518513
return;
519514
}
@@ -2058,11 +2053,7 @@ mod test {
20582053

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

2061-
// Ensure retrying is enabled on the test (as other tests will run in parallel)
2062-
TEST_EVENT_OBSERVER_SKIP_RETRY
2063-
.lock()
2064-
.unwrap()
2065-
.replace(false);
2056+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
20662057

20672058
// Insert payload
20682059
EventObserver::insert_payload(&conn, url, &payload, timeout)
@@ -2135,11 +2126,7 @@ mod test {
21352126

21362127
let observer = EventObserver::new(Some(working_dir.clone()), endpoint, timeout);
21372128

2138-
// Ensure retrying is enabled on the test (as other tests will run in parallel)
2139-
TEST_EVENT_OBSERVER_SKIP_RETRY
2140-
.lock()
2141-
.unwrap()
2142-
.replace(false);
2129+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
21432130

21442131
// Call send_payload
21452132
observer.send_payload(&payload, "/test");
@@ -2400,18 +2387,15 @@ mod test {
24002387

24012388
// Disable retrying so that it sends the payload only once
24022389
// and that payload will be ignored by the test server.
2403-
TEST_EVENT_OBSERVER_SKIP_RETRY.lock().unwrap().replace(true);
2390+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(true);
24042391

24052392
info!("Sending payload 1");
24062393

24072394
// Send the payload
24082395
observer.send_payload(&payload, "/test");
24092396

24102397
// Re-enable retrying
2411-
TEST_EVENT_OBSERVER_SKIP_RETRY
2412-
.lock()
2413-
.unwrap()
2414-
.replace(false);
2398+
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);
24152399

24162400
info!("Sending payload 2");
24172401

0 commit comments

Comments
 (0)