mirror of
https://github.com/rustfs/rustfs.git
synced 2026-09-02 10:18:10 +00:00
test(targets,notify,audit): share a builder MockTarget testkit (#6717)
* test(targets): ship a builder MockTarget testkit and retire the in-crate Target mocks Adds crates/targets/src/testkit.rs with a builder-style MockTarget implementing Target<E> for every E: PluginEvent, with orthogonal off-by-default knobs: disabled/active override, health delay plus health-started signal plus a drop-guard counter proving a cancelled probe future was dropped, an init failure budget (usize::MAX = always fail) plus blocking init plus an init counter, a close counter/signal/semaphore gate with a runtime block toggle, a save counter plus save failure budget, caller-supplied store and failed-store handles, and a shared final-failure counter. Clones and clone_dyn share all counters, so an observer clone keeps watching a target after it is boxed into a runtime. The module is gated as #[cfg(any(test, feature = "test-support"))]: in-crate unit tests get it via cfg(test), and downstream test suites opt in through the new off-by-default test-support cargo feature (test-support = [], activating no dependencies). Migrates the five in-crate duplicate mocks onto it: the plugin.rs registry-factory TestTarget, the runtime/adapter.rs lifecycle TestTarget (init/close/store knobs), the runtime/mod.rs TestTarget plus HealthDropGuard (close gating and health-probe tests), the target/mod.rs MoveTestTarget (folded into the test_support helper constructors used by the NATS JetStream failed-store tests), and the target/mod.rs StoreBackedTarget (the default send_from_store purge test; the mock deliberately does not override send_from_store or handle_terminal_failure). The forced init failure now uses TargetError::Initialization instead of the old adapter mock's Configuration; the adapter derives its redacted failure summary from the target id alone, so the migrated assertions are unchanged. Leak guard: testkit unit tests assert the crate manifest still declares default = [] and that test-support = [] stays a pure cfg gate, complementing the compile-level cfg gate that keeps the mock out of production builds. Part of rustfs/backlog#1846 (cluster 3, step 1). * test(notify,audit): migrate the Target mocks onto the shared testkit Retires the hand-written Target mocks in crates/notify and crates/audit in favor of rustfs_targets::testkit::MockTarget: notify's notifier.rs TestTarget/DeferredTestTarget/ClosableTestTarget, lifecycle.rs BlockingInitTarget/RetryInitTarget (rebuilt as observed MockTarget templates cloned by their plugin-descriptor factories, sharing the init signal, close counter, and single-failure init budget across generations), runtime_view.rs TestTarget, runtime_facade.rs TestTarget, and audit's pipeline.rs MockTarget, system.rs TestTarget, registry.rs CloseTestTarget, plus TestTarget and FailingTarget in audit/tests/pipeline_layer_test.rs. Removing the two integration-test mocks also removes their respelled PluginEvent bound (E: Send + Sync + 'static + Clone + Serialize + DeserializeOwned), which the plugin-contract rules require to be spelled only via PluginEvent. lifecycle.rs ReplayTarget stays bespoke on purpose: its generation tags, mpsc observation channels, gated send_raw delivery, and ObservedQueueStore model the replay pipeline itself and would contort a general-purpose mock. The other bespoke mocks named out of scope in PR-3a (ProgrammedTarget, ClassifyingTarget, the ReloadableTargetTls fakes) are likewise untouched. New testkit knobs, each defaulted off and unit-tested: with_id (rename a clone while keeping the shared counters, for factory templates), with_first_save_gate (the first save notifies entered and waits on release; several mocks may share one pair), with_health_gate (is_active waits on a release handle after notifying health_started), with_delivery_snapshot (fixed snapshot overriding the store-derived default), with_close_failures (close-failure budget, default TargetError::Storage) with with_close_failure_error to shape the variant (audit's registry test pins TargetError::Unknown), and an always-on is_enabled call counter exposed as enabled_call_count (notifier's generation tests count dispatcher selections through it). Both crates enable the testkit through a dev-dependency on rustfs-targets with the test-support feature; the feature stays out of default and activates no dependencies, so production builds are unchanged. Part of rustfs/backlog#1846 (cluster 3, step 2).
This commit is contained in:
@@ -441,13 +441,10 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{BuiltinPluginRuntimeAdapter, MAX_PARALLEL_STORE_OPENS, PluginRuntimeAdapter};
|
||||
use crate::PluginEvent;
|
||||
use crate::arn::TargetID;
|
||||
use crate::store::{Key, QueueStore, Store};
|
||||
use crate::target::{EntityTarget, QueuedPayload, QueuedPayloadMeta};
|
||||
use crate::{StoreError, Target, TargetError};
|
||||
use async_trait::async_trait;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use crate::target::QueuedPayload;
|
||||
use crate::testkit::MockTarget;
|
||||
use crate::{StoreError, Target};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::time::Duration;
|
||||
use tempfile::tempdir;
|
||||
@@ -569,96 +566,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TestTarget {
|
||||
close_calls: Arc<AtomicUsize>,
|
||||
id: TargetID,
|
||||
init_calls: Arc<AtomicUsize>,
|
||||
init_entered: Option<Arc<Notify>>,
|
||||
init_fails: bool,
|
||||
store: Option<Arc<TestStore>>,
|
||||
}
|
||||
|
||||
impl TestTarget {
|
||||
fn new(id: &str, name: &str) -> Self {
|
||||
Self {
|
||||
close_calls: Arc::new(AtomicUsize::new(0)),
|
||||
id: TargetID::new(id.to_string(), name.to_string()),
|
||||
init_calls: Arc::new(AtomicUsize::new(0)),
|
||||
init_entered: None,
|
||||
init_fails: false,
|
||||
store: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_failed_init(mut self) -> Self {
|
||||
self.init_fails = true;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_pending_init(mut self, init_entered: Arc<Notify>) -> Self {
|
||||
self.init_entered = Some(init_entered);
|
||||
self
|
||||
}
|
||||
|
||||
fn with_store(mut self) -> Self {
|
||||
let dir = tempdir().expect("tempdir should be created for queue store tests");
|
||||
let store = QueueStore::<QueuedPayload>::new(dir.path(), 16, ".queue");
|
||||
store.open().expect("queue store should open");
|
||||
self.store = Some(Arc::new(store));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<E> Target<E> for TestTarget
|
||||
where
|
||||
E: PluginEvent,
|
||||
{
|
||||
fn id(&self) -> TargetID {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
async fn is_active(&self) -> Result<bool, TargetError> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn save(&self, _event: Arc<EntityTarget<E>>) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_raw_from_store(&self, _key: Key, _body: Vec<u8>, _meta: QueuedPayloadMeta) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), TargetError> {
|
||||
self.close_calls.fetch_add(1, Ordering::SeqCst);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store(&self) -> Option<&(dyn Store<QueuedPayload, Error = StoreError, Key = Key> + Send + Sync)> {
|
||||
self.store.as_deref()
|
||||
}
|
||||
|
||||
fn clone_dyn(&self) -> Box<dyn Target<E> + Send + Sync> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
async fn init(&self) -> Result<(), TargetError> {
|
||||
self.init_calls.fetch_add(1, Ordering::SeqCst);
|
||||
if let Some(init_entered) = &self.init_entered {
|
||||
init_entered.notify_one();
|
||||
return std::future::pending().await;
|
||||
}
|
||||
if self.init_fails {
|
||||
return Err(TargetError::Configuration("forced init failure".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_enabled(&self) -> bool {
|
||||
true
|
||||
}
|
||||
/// Builds the tempdir-backed, already-opened queue store the store-backed mock targets use.
|
||||
/// The tempdir handle is dropped here on purpose, matching the previous in-module mock: these
|
||||
/// tests never write through the store, they only need an openable handle.
|
||||
fn opened_queue_store() -> Arc<TestStore> {
|
||||
let dir = tempdir().expect("tempdir should be created for queue store tests");
|
||||
let store = QueueStore::<QueuedPayload>::new(dir.path(), 16, ".queue");
|
||||
store.open().expect("queue store should open");
|
||||
Arc::new(store)
|
||||
}
|
||||
|
||||
fn builtin_adapter() -> BuiltinPluginRuntimeAdapter<String> {
|
||||
@@ -684,7 +599,7 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn builtin_adapter_skips_non_store_target_when_init_fails() {
|
||||
let adapter = builtin_adapter();
|
||||
let target = TestTarget::new("primary", "webhook").with_failed_init();
|
||||
let target = MockTarget::new("primary", "webhook").with_init_failures(usize::MAX);
|
||||
|
||||
let activation = adapter.activate_with_replay(vec![Box::new(target)]).await;
|
||||
|
||||
@@ -695,7 +610,9 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn builtin_adapter_keeps_store_backed_target_when_init_fails() {
|
||||
let adapter = builtin_adapter();
|
||||
let target = TestTarget::new("primary", "webhook").with_failed_init().with_store();
|
||||
let target = MockTarget::new("primary", "webhook")
|
||||
.with_init_failures(usize::MAX)
|
||||
.with_store(opened_queue_store());
|
||||
|
||||
let activation = adapter.activate_with_replay(vec![Box::new(target)]).await;
|
||||
|
||||
@@ -706,7 +623,9 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn prepared_store_target_reports_init_failure_without_dropping_queue_runtime() {
|
||||
let adapter = builtin_adapter();
|
||||
let target = TestTarget::new("primary", "webhook").with_failed_init().with_store();
|
||||
let target = MockTarget::new("primary", "webhook")
|
||||
.with_init_failures(usize::MAX)
|
||||
.with_store(opened_queue_store());
|
||||
|
||||
let prepared = adapter.prepare_targets(vec![Box::new(target)]).await;
|
||||
assert_eq!(prepared.targets.len(), 1);
|
||||
@@ -729,11 +648,10 @@ mod tests {
|
||||
async fn cancellable_preparation_returns_current_and_remaining_targets_for_shutdown() {
|
||||
let adapter = builtin_adapter();
|
||||
let init_entered = Arc::new(Notify::new());
|
||||
let first = TestTarget::new("first", "webhook").with_pending_init(init_entered.clone());
|
||||
let first_close_calls = first.close_calls.clone();
|
||||
let second = TestTarget::new("second", "webhook");
|
||||
let second_close_calls = second.close_calls.clone();
|
||||
let second_init_calls = second.init_calls.clone();
|
||||
let first = MockTarget::new("first", "webhook").with_blocking_init(init_entered.clone());
|
||||
let first_observer = first.clone();
|
||||
let second = MockTarget::new("second", "webhook");
|
||||
let second_observer = second.clone();
|
||||
let cancellation = CancellationToken::new();
|
||||
let prepare_adapter = adapter.clone();
|
||||
let prepare_cancellation = cancellation.clone();
|
||||
@@ -755,9 +673,9 @@ mod tests {
|
||||
.close_prepared(prepared)
|
||||
.await
|
||||
.expect("cancelled targets should close");
|
||||
assert_eq!(first_close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(second_close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(second_init_calls.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(first_observer.close_call_count(), 1);
|
||||
assert_eq!(second_observer.close_call_count(), 1);
|
||||
assert_eq!(second_observer.init_call_count(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -765,8 +683,11 @@ mod tests {
|
||||
let adapter = builtin_adapter();
|
||||
let dir = tempdir().expect("tempdir should be created");
|
||||
let queue_path = dir.path().join("queue");
|
||||
let mut target = TestTarget::new("primary", "webhook");
|
||||
target.store = Some(Arc::new(QueueStore::<QueuedPayload>::new(&queue_path, 16, ".queue")));
|
||||
let target = MockTarget::new("primary", "webhook").with_store(Arc::new(QueueStore::<QueuedPayload>::new(
|
||||
&queue_path,
|
||||
16,
|
||||
".queue",
|
||||
)));
|
||||
|
||||
let prepared = adapter.prepare_targets(vec![Box::new(target)]).await;
|
||||
assert!(!queue_path.exists(), "dormant preparation must not open the queue store");
|
||||
@@ -789,15 +710,18 @@ mod tests {
|
||||
let dir = tempdir().expect("tempdir should be created");
|
||||
let invalid_base = dir.path().join("not-a-directory");
|
||||
std::fs::write(&invalid_base, b"file").expect("invalid queue base should be created");
|
||||
let mut target = TestTarget::new("primary", "webhook");
|
||||
let close_calls = target.close_calls.clone();
|
||||
target.store = Some(Arc::new(QueueStore::<QueuedPayload>::new(&invalid_base, 16, ".queue")));
|
||||
let target = MockTarget::new("primary", "webhook").with_store(Arc::new(QueueStore::<QueuedPayload>::new(
|
||||
&invalid_base,
|
||||
16,
|
||||
".queue",
|
||||
)));
|
||||
let observer = target.clone();
|
||||
|
||||
let activation = adapter.activate_with_replay(vec![Box::new(target)]).await;
|
||||
|
||||
assert!(activation.targets.is_empty());
|
||||
assert!(activation.replay_workers.is_empty());
|
||||
assert_eq!(close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.close_call_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -810,14 +734,13 @@ mod tests {
|
||||
let mut targets: Vec<Box<dyn Target<String> + Send + Sync>> = Vec::with_capacity(TARGETS);
|
||||
let mut expected_ids = Vec::with_capacity(TARGETS);
|
||||
for index in 0..TARGETS {
|
||||
let mut target = TestTarget::new(&format!("target-{index}"), "webhook");
|
||||
expected_ids.push(target.id.to_string());
|
||||
let open_gate = gate.clone();
|
||||
target.store = Some(Arc::new(TestOpenStore {
|
||||
let target = MockTarget::new(&format!("target-{index}"), "webhook").with_store(Arc::new(TestOpenStore {
|
||||
before_clone: Arc::new(|| {}),
|
||||
before_open: Arc::new(move || open_gate.enter()),
|
||||
store: QueueStore::new(dir.path().join(index.to_string()), 16, ".queue"),
|
||||
}));
|
||||
expected_ids.push(target.target_id().to_string());
|
||||
targets.push(Box::new(target));
|
||||
}
|
||||
|
||||
@@ -848,13 +771,12 @@ mod tests {
|
||||
async fn panicking_store_open_rejects_and_closes_only_that_target() {
|
||||
let adapter = builtin_adapter();
|
||||
let dir = tempdir().expect("tempdir should be created");
|
||||
let mut target = TestTarget::new("panicking", "webhook");
|
||||
let close_calls = target.close_calls.clone();
|
||||
target.store = Some(Arc::new(TestOpenStore {
|
||||
let target = MockTarget::new("panicking", "webhook").with_store(Arc::new(TestOpenStore {
|
||||
before_clone: Arc::new(|| {}),
|
||||
before_open: Arc::new(|| panic!("forced store open panic: do-not-expose-payload")),
|
||||
store: QueueStore::new(dir.path(), 16, ".queue"),
|
||||
}));
|
||||
let observer = target.clone();
|
||||
|
||||
let prepared = adapter.prepare_targets(vec![Box::new(target)]).await;
|
||||
let (opened, rejected) = adapter.open_prepared_stores(prepared);
|
||||
@@ -875,20 +797,19 @@ mod tests {
|
||||
.close_prepared(rejected)
|
||||
.await
|
||||
.expect("a target rejected after a store panic should close");
|
||||
assert_eq!(close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.close_call_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn panicking_store_clone_cannot_publish_target_without_replay_worker() {
|
||||
let adapter = builtin_adapter();
|
||||
let dir = tempdir().expect("tempdir should be created");
|
||||
let mut target = TestTarget::new("panicking-clone", "webhook");
|
||||
let close_calls = target.close_calls.clone();
|
||||
target.store = Some(Arc::new(TestOpenStore {
|
||||
let target = MockTarget::new("panicking-clone", "webhook").with_store(Arc::new(TestOpenStore {
|
||||
before_clone: Arc::new(|| panic!("forced store clone panic: do-not-expose-payload")),
|
||||
before_open: Arc::new(|| {}),
|
||||
store: QueueStore::new(dir.path(), 16, ".queue"),
|
||||
}));
|
||||
let observer = target.clone();
|
||||
|
||||
let prepared = adapter.prepare_targets(vec![Box::new(target)]).await;
|
||||
let (opened, open_rejected) = adapter.open_prepared_stores(prepared);
|
||||
@@ -906,14 +827,14 @@ mod tests {
|
||||
.close_prepared(rejected)
|
||||
.await
|
||||
.expect("a target rejected during replay activation should close");
|
||||
assert_eq!(close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.close_call_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn builtin_adapter_shutdown_clears_runtime_and_replay_workers() {
|
||||
let adapter = builtin_adapter();
|
||||
let target = TestTarget::new("primary", "webhook");
|
||||
let close_calls = Arc::clone(&target.close_calls);
|
||||
let target = MockTarget::new("primary", "webhook");
|
||||
let observer = target.clone();
|
||||
let mut runtime = crate::runtime::TargetRuntimeManager::new();
|
||||
let mut replay_workers = crate::runtime::ReplayWorkerManager::new();
|
||||
|
||||
@@ -933,6 +854,6 @@ mod tests {
|
||||
|
||||
assert!(runtime.is_empty());
|
||||
assert!(replay_workers.is_empty());
|
||||
assert_eq!(close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.close_call_count(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -965,17 +965,12 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{HEALTH_PROBE_CONCURRENCY, TargetRuntimeManager, health_snapshots_for_targets};
|
||||
use crate::PluginEvent;
|
||||
use crate::StoreError;
|
||||
use crate::arn::TargetID;
|
||||
use crate::store::{Key, Store};
|
||||
use crate::target::{EntityTarget, QueuedPayload, QueuedPayloadMeta};
|
||||
use crate::{SharedTarget, Target, TargetError};
|
||||
use async_trait::async_trait;
|
||||
use crate::SharedTarget;
|
||||
use crate::store::Key;
|
||||
use crate::testkit::MockTarget;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::{Notify, Semaphore};
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn seed_interval_start_backdates_by_one_interval() {
|
||||
@@ -1037,108 +1032,11 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TestTarget {
|
||||
id: TargetID,
|
||||
block_on_close: Arc<AtomicBool>,
|
||||
close_gate: Arc<Semaphore>,
|
||||
close_calls: Arc<AtomicUsize>,
|
||||
enabled: bool,
|
||||
health_delay: Duration,
|
||||
health_drops: Arc<AtomicUsize>,
|
||||
health_started: Arc<Notify>,
|
||||
close_started: Arc<Notify>,
|
||||
}
|
||||
|
||||
struct HealthDropGuard(Arc<AtomicUsize>);
|
||||
|
||||
impl Drop for HealthDropGuard {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
impl TestTarget {
|
||||
fn new(id: &str, name: &str) -> Self {
|
||||
Self {
|
||||
id: TargetID::new(id.to_string(), name.to_string()),
|
||||
block_on_close: Arc::new(AtomicBool::new(false)),
|
||||
close_gate: Arc::new(Semaphore::new(0)),
|
||||
close_calls: Arc::new(AtomicUsize::new(0)),
|
||||
enabled: true,
|
||||
health_delay: Duration::ZERO,
|
||||
health_drops: Arc::new(AtomicUsize::new(0)),
|
||||
health_started: Arc::new(Notify::new()),
|
||||
close_started: Arc::new(Notify::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_health_delay(id: &str, delay: Duration) -> Self {
|
||||
Self {
|
||||
health_delay: delay,
|
||||
..Self::new(id, "webhook")
|
||||
}
|
||||
}
|
||||
|
||||
fn disabled(id: &str) -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
..Self::new(id, "webhook")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<E> Target<E> for TestTarget
|
||||
where
|
||||
E: PluginEvent,
|
||||
{
|
||||
fn id(&self) -> TargetID {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
async fn is_active(&self) -> Result<bool, TargetError> {
|
||||
self.health_started.notify_one();
|
||||
let _drop_guard = HealthDropGuard(Arc::clone(&self.health_drops));
|
||||
tokio::time::sleep(self.health_delay).await;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn save(&self, _event: Arc<EntityTarget<E>>) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn send_raw_from_store(&self, _key: Key, _body: Vec<u8>, _meta: QueuedPayloadMeta) -> Result<(), TargetError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), TargetError> {
|
||||
self.close_calls.fetch_add(1, Ordering::SeqCst);
|
||||
self.close_started.notify_one();
|
||||
if self.block_on_close.load(Ordering::SeqCst) {
|
||||
let _permit = self.close_gate.acquire().await.expect("close gate should remain open");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store(&self) -> Option<&(dyn Store<QueuedPayload, Error = StoreError, Key = Key> + Send + Sync)> {
|
||||
None
|
||||
}
|
||||
|
||||
fn clone_dyn(&self) -> Box<dyn Target<E> + Send + Sync> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn is_enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn runtime_manager_removes_and_closes_target() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
let target = TestTarget::new("primary", "webhook");
|
||||
let close_calls = Arc::clone(&target.close_calls);
|
||||
let target = MockTarget::new("primary", "webhook");
|
||||
let observer = target.clone();
|
||||
|
||||
manager.add_boxed(Box::new(target));
|
||||
assert_eq!(manager.len(), 1);
|
||||
@@ -1146,14 +1044,14 @@ mod tests {
|
||||
let removed = manager.remove_and_close("primary:webhook").await;
|
||||
assert!(removed.is_some());
|
||||
assert_eq!(manager.len(), 0);
|
||||
assert_eq!(close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.close_call_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn runtime_manager_starts_all_target_closes_before_waiting_for_completion() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
let first = TestTarget::new("first", "webhook");
|
||||
let second = TestTarget::new("second", "webhook");
|
||||
let first = MockTarget::new("first", "webhook");
|
||||
let second = MockTarget::new("second", "webhook");
|
||||
let first_observer = first.clone();
|
||||
let second_observer = second.clone();
|
||||
manager.add_boxed(Box::new(first));
|
||||
@@ -1164,34 +1062,34 @@ mod tests {
|
||||
.into_iter()
|
||||
.next()
|
||||
.expect("two targets should have a first close key");
|
||||
let (blocked, unblocked) = if first_close_key == first_observer.id.to_string() {
|
||||
let (blocked, unblocked) = if first_close_key == first_observer.target_id().to_string() {
|
||||
(first_observer, second_observer)
|
||||
} else {
|
||||
(second_observer, first_observer)
|
||||
};
|
||||
blocked.block_on_close.store(true, Ordering::SeqCst);
|
||||
blocked.set_block_on_close(true);
|
||||
|
||||
let close_task = tokio::spawn(async move { manager.clear_and_close().await });
|
||||
|
||||
tokio::time::timeout(std::time::Duration::from_secs(1), blocked.close_started.notified())
|
||||
tokio::time::timeout(std::time::Duration::from_secs(1), blocked.close_started().notified())
|
||||
.await
|
||||
.expect("the first target close should start");
|
||||
tokio::time::timeout(std::time::Duration::from_secs(1), unblocked.close_started.notified())
|
||||
tokio::time::timeout(std::time::Duration::from_secs(1), unblocked.close_started().notified())
|
||||
.await
|
||||
.expect("a blocked first close must not prevent the second close from starting");
|
||||
assert!(!close_task.is_finished(), "clear_and_close must still await the blocked target");
|
||||
|
||||
blocked.close_gate.add_permits(1);
|
||||
blocked.close_gate().add_permits(1);
|
||||
let errors = close_task.await.expect("clear_and_close task should join");
|
||||
assert!(errors.is_empty());
|
||||
assert_eq!(blocked.close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(unblocked.close_calls.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(blocked.close_call_count(), 1);
|
||||
assert_eq!(unblocked.close_call_count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_manager_snapshots_targets() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
manager.add_boxed(Box::new(TestTarget::new("primary", "webhook")));
|
||||
manager.add_boxed(Box::new(MockTarget::new("primary", "webhook")));
|
||||
|
||||
let snapshots = manager.snapshots();
|
||||
assert_eq!(snapshots.len(), 1);
|
||||
@@ -1202,7 +1100,7 @@ mod tests {
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn health_snapshot_allows_a_four_second_probe() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
manager.add_boxed(Box::new(TestTarget::with_health_delay("slow", Duration::from_secs(4))));
|
||||
manager.add_boxed(Box::new(MockTarget::new("slow", "webhook").with_health_delay(Duration::from_secs(4))));
|
||||
|
||||
let snapshots = manager.health_snapshots().await;
|
||||
|
||||
@@ -1214,7 +1112,7 @@ mod tests {
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn health_snapshot_times_out_after_five_seconds() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
manager.add_boxed(Box::new(TestTarget::with_health_delay("stalled", Duration::from_secs(6))));
|
||||
manager.add_boxed(Box::new(MockTarget::new("stalled", "webhook").with_health_delay(Duration::from_secs(6))));
|
||||
|
||||
let snapshots = manager.health_snapshots().await;
|
||||
|
||||
@@ -1227,10 +1125,9 @@ mod tests {
|
||||
async fn health_collection_deadline_does_not_scale_with_target_count() {
|
||||
let mut manager = TargetRuntimeManager::<String>::new();
|
||||
for index in 0..24 {
|
||||
manager.add_boxed(Box::new(TestTarget::with_health_delay(
|
||||
&format!("stalled-{index}"),
|
||||
Duration::from_secs(30),
|
||||
)));
|
||||
manager.add_boxed(Box::new(
|
||||
MockTarget::new(&format!("stalled-{index}"), "webhook").with_health_delay(Duration::from_secs(30)),
|
||||
));
|
||||
}
|
||||
let started = tokio::time::Instant::now();
|
||||
|
||||
@@ -1263,11 +1160,11 @@ mod tests {
|
||||
async fn disabled_target_does_not_wait_for_probe_capacity() {
|
||||
let mut targets: Vec<SharedTarget<String>> = (0..HEALTH_PROBE_CONCURRENCY)
|
||||
.map(|index| {
|
||||
Arc::new(TestTarget::with_health_delay(&format!("stalled-{index}"), Duration::from_secs(30)))
|
||||
Arc::new(MockTarget::new(&format!("stalled-{index}"), "webhook").with_health_delay(Duration::from_secs(30)))
|
||||
as SharedTarget<String>
|
||||
})
|
||||
.collect();
|
||||
targets.push(Arc::new(TestTarget::disabled("disabled")));
|
||||
targets.push(Arc::new(MockTarget::new("disabled", "webhook").disabled()));
|
||||
|
||||
let snapshots = health_snapshots_for_targets(targets).await;
|
||||
let disabled = snapshots
|
||||
@@ -1281,20 +1178,19 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancelling_health_collection_drops_in_flight_probe() {
|
||||
let target = TestTarget::with_health_delay("slow", Duration::from_secs(30));
|
||||
let health_drops = Arc::clone(&target.health_drops);
|
||||
let health_started = Arc::clone(&target.health_started);
|
||||
let target = MockTarget::new("slow", "webhook").with_health_delay(Duration::from_secs(30));
|
||||
let observer = target.clone();
|
||||
let targets: Vec<SharedTarget<String>> = vec![Arc::new(target)];
|
||||
let collector = tokio::spawn(health_snapshots_for_targets(targets));
|
||||
|
||||
tokio::time::timeout(Duration::from_secs(1), health_started.notified())
|
||||
tokio::time::timeout(Duration::from_secs(1), observer.health_started().notified())
|
||||
.await
|
||||
.expect("health probe should start");
|
||||
collector.abort();
|
||||
let join_error = collector.await.expect_err("health collector should be cancelled");
|
||||
|
||||
assert!(join_error.is_cancelled());
|
||||
assert_eq!(health_drops.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(observer.health_drop_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user