Skip to content

Commit 8e1e731

Browse files
committed
test: improve fault injection in event dispatcher
1 parent be32137 commit 8e1e731

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ impl RewardSetEventPayload {
322322
}
323323

324324
#[cfg(test)]
325-
static TEST_EVENT_OBSERVER_SKIP_SEND_PAYLOAD: std::sync::Mutex<Option<bool>> =
326-
std::sync::Mutex::new(None);
325+
static TEST_EVENT_OBSERVER_SKIP_RETRY: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
327326

328327
impl EventObserver {
329328
fn init_db(db_path: &str) -> Result<Connection, db_error> {
@@ -381,16 +380,6 @@ impl EventObserver {
381380
}
382381

383382
fn process_pending_payloads(conn: &Connection) {
384-
#[cfg(test)]
385-
if TEST_EVENT_OBSERVER_SKIP_SEND_PAYLOAD
386-
.lock()
387-
.unwrap()
388-
.unwrap_or(false)
389-
{
390-
warn!("Fault injection: skipping retry of payload");
391-
return;
392-
}
393-
394383
let pending_payloads = match Self::get_pending_payloads(conn) {
395384
Ok(payloads) => payloads,
396385
Err(e) => {
@@ -405,6 +394,17 @@ impl EventObserver {
405394
for (id, url, payload, timeout_ms) in pending_payloads {
406395
let timeout = Duration::from_millis(timeout_ms);
407396
Self::send_payload_directly(&payload, &url, timeout);
397+
398+
#[cfg(test)]
399+
if TEST_EVENT_OBSERVER_SKIP_RETRY
400+
.lock()
401+
.unwrap()
402+
.unwrap_or(false)
403+
{
404+
warn!("Fault injection: delete_payload");
405+
return;
406+
}
407+
408408
if let Err(e) = Self::delete_payload(conn, id) {
409409
error!(
410410
"Event observer: failed to delete pending payload from database";
@@ -459,6 +459,17 @@ impl EventObserver {
459459
);
460460
}
461461
}
462+
463+
#[cfg(test)]
464+
if TEST_EVENT_OBSERVER_SKIP_RETRY
465+
.lock()
466+
.unwrap()
467+
.unwrap_or(false)
468+
{
469+
warn!("Fault injection: skipping retry of payload");
470+
return;
471+
}
472+
462473
sleep(backoff);
463474
backoff *= 2;
464475
}
@@ -2258,18 +2269,15 @@ mod test {
22582269

22592270
// Disable retrying so that it sends the payload only once
22602271
// and that payload will be ignored by the test server.
2261-
TEST_EVENT_OBSERVER_SKIP_SEND_PAYLOAD
2262-
.lock()
2263-
.unwrap()
2264-
.replace(true);
2272+
TEST_EVENT_OBSERVER_SKIP_RETRY.lock().unwrap().replace(true);
22652273

22662274
info!("Sending payload 1");
22672275

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

22712279
// Re-enable retrying
2272-
TEST_EVENT_OBSERVER_SKIP_SEND_PAYLOAD
2280+
TEST_EVENT_OBSERVER_SKIP_RETRY
22732281
.lock()
22742282
.unwrap()
22752283
.replace(false);

0 commit comments

Comments
 (0)