Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed race condition in tests assuming TEST_EVENT_OBSERVER_SKIP_RETRY… #5669

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions testnet/stacks-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async-h1 = { version = "2.3.2", optional = true }
async-std = { version = "1.6", optional = true, features = ["attributes"] }
http-types = { version = "2.12", optional = true }
thiserror = { workspace = true }
serial_test = "3.2.0"
rdeioris marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(not(any(target_os = "macos", target_os="windows", target_arch = "arm")))'.dependencies]
tikv-jemallocator = {workspace = true}
Expand Down
32 changes: 16 additions & 16 deletions testnet/stacks-node/src/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender};
#[cfg(test)]
use std::sync::LazyLock;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
Expand Down Expand Up @@ -330,7 +332,7 @@ impl RewardSetEventPayload {
}

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

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

#[cfg(test)]
if TEST_EVENT_OBSERVER_SKIP_RETRY
.lock()
.unwrap()
.unwrap_or(false)
{
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
warn!("Fault injection: delete_payload");
return;
}
Expand Down Expand Up @@ -509,11 +507,7 @@ impl EventObserver {
}

#[cfg(test)]
if TEST_EVENT_OBSERVER_SKIP_RETRY
.lock()
.unwrap()
.unwrap_or(false)
{
if TEST_EVENT_OBSERVER_SKIP_RETRY.get() {
warn!("Fault injection: skipping retry of payload");
return;
}
Expand Down Expand Up @@ -1759,6 +1753,7 @@ mod test {
use std::time::Instant;

use clarity::vm::costs::ExecutionCost;
use serial_test::serial;
use stacks::burnchains::{PoxConstants, Txid};
use stacks::chainstate::nakamoto::{NakamotoBlock, NakamotoBlockHeader};
use stacks::chainstate::stacks::db::{StacksBlockHeaderTypes, StacksHeaderInfo};
Expand Down Expand Up @@ -2042,6 +2037,7 @@ mod test {
}

#[test]
#[serial]
fn test_process_pending_payloads() {
use mockito::Matcher;

Expand All @@ -2065,6 +2061,8 @@ mod test {

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

TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);

// Insert payload
EventObserver::insert_payload(&conn, url, &payload, timeout)
.expect("Failed to insert payload");
Expand Down Expand Up @@ -2115,6 +2113,7 @@ mod test {
}

#[test]
#[serial]
fn test_send_payload_with_db() {
use mockito::Matcher;

Expand All @@ -2136,6 +2135,8 @@ mod test {

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

TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);

// Call send_payload
observer.send_payload(&payload, "/test");

Expand Down Expand Up @@ -2262,6 +2263,7 @@ mod test {
}

#[test]
#[serial]
fn test_send_payload_timeout() {
let port = get_random_port();
let timeout = Duration::from_secs(3);
Expand Down Expand Up @@ -2324,6 +2326,7 @@ mod test {
}

#[test]
#[serial]
fn test_send_payload_with_db_force_restart() {
let port = get_random_port();
let timeout = Duration::from_secs(3);
Expand Down Expand Up @@ -2395,18 +2398,15 @@ mod test {

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

info!("Sending payload 1");

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

// Re-enable retrying
TEST_EVENT_OBSERVER_SKIP_RETRY
.lock()
.unwrap()
.replace(false);
TEST_EVENT_OBSERVER_SKIP_RETRY.set(false);

info!("Sending payload 2");

Expand Down
Loading