Skip to content

Commit d5e7a01

Browse files
committed
test: Replace wall-clock with SimClock in activity tests
1 parent 7c7e954 commit d5e7a01

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

crates/wasm-workers/src/activity/activity_js_worker.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ mod tests {
308308
use executor::worker::{WorkerContext, WorkerError, WorkerResultOk};
309309
use rstest::rstest;
310310
use serde_json::json;
311+
use test_utils::sim_clock::SimClock;
311312
use tokio::sync::mpsc;
312313
use tracing::info_span;
313314
use val_json::wast_val::WastVal;
@@ -319,6 +320,7 @@ mod tests {
319320
user_return_type: ReturnTypeExtendable,
320321
allowed_hosts: Vec<crate::http_request_policy::AllowedHostConfig>,
321322
logs_storage_config: Option<crate::component_logger::LogStrageConfig>,
323+
clock_fn: Box<dyn ClockFn>,
322324
}
323325

324326
impl JsWorkerBuilder {
@@ -340,6 +342,7 @@ mod tests {
340342
},
341343
allowed_hosts: Vec::new(),
342344
logs_storage_config: None,
345+
clock_fn: Now.clone_box(),
343346
}
344347
}
345348

@@ -371,12 +374,16 @@ mod tests {
371374
self
372375
}
373376

377+
fn with_clock_fn(mut self, clock_fn: Box<dyn ClockFn>) -> Self {
378+
self.clock_fn = clock_fn;
379+
self
380+
}
381+
374382
async fn build(self) -> Arc<dyn Worker> {
375383
let engine =
376384
Engines::get_activity_engine_test(EngineConfig::on_demand_testing()).unwrap();
377385
let cancel_registry = CancelRegistry::new();
378386
let (db_forwarder_sender, _) = mpsc::channel(1);
379-
let clock_fn: Box<dyn ClockFn> = Now.clone_box();
380387

381388
let component_id = concepts::ComponentId::new(
382389
ComponentType::Activity,
@@ -406,7 +413,7 @@ mod tests {
406413
wasm_component,
407414
config,
408415
engine,
409-
clock_fn,
416+
self.clock_fn,
410417
std::sync::Arc::new(TokioSleep),
411418
)
412419
.unwrap();
@@ -1207,6 +1214,7 @@ mod tests {
12071214
js_source: &str,
12081215
user_ffqn: FunctionFqn,
12091216
listener: &std::net::TcpListener,
1217+
clock_fn: Box<dyn ClockFn>,
12101218
) -> Arc<dyn Worker> {
12111219
let server_address = listener
12121220
.local_addr()
@@ -1222,6 +1230,7 @@ mod tests {
12221230
JsWorkerBuilder::new(js_source, user_ffqn)
12231231
.with_params(user_params)
12241232
.with_allowed_host(&allowed_host)
1233+
.with_clock_fn(clock_fn)
12251234
.build()
12261235
.await
12271236
}
@@ -1255,12 +1264,19 @@ mod tests {
12551264
"#;
12561265

12571266
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1258-
let worker =
1259-
new_js_activity_worker_for_retry_test(js_source, ffqn.clone(), &listener).await;
1267+
let sim_clock = SimClock::epoch();
1268+
let worker = new_js_activity_worker_for_retry_test(
1269+
js_source,
1270+
ffqn.clone(),
1271+
&listener,
1272+
sim_clock.clone_box(),
1273+
)
1274+
.await;
12601275

12611276
run_http_get_retry_test(
12621277
listener,
12631278
worker,
1279+
sim_clock,
12641280
ffqn,
12651281
|uri| Params::from_json_values_test(vec![json!(format!("{uri}/"))]),
12661282
locking_strategy,

crates/wasm-workers/src/activity/activity_worker.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,11 @@ pub(crate) mod tests {
741741
/// 1. An activity that returns an error (from a 500 response) triggers a temporary failure with backoff
742742
/// 2. After the backoff expires, the activity is retried
743743
/// 3. On retry, either succeeds (if `succeed_eventually` is true) or fails permanently
744+
#[expect(clippy::too_many_arguments)]
744745
pub(crate) async fn run_http_get_retry_test(
745746
listener: std::net::TcpListener,
746747
worker: Arc<dyn Worker>,
748+
sim_clock: SimClock,
747749
ffqn: FunctionFqn,
748750
make_params: impl FnOnce(&str) -> Params,
749751
locking_strategy: LockingStrategy,
@@ -759,7 +761,6 @@ pub(crate) mod tests {
759761
const BODY: &str = "ok";
760762
const RETRY_EXP_BACKOFF: Duration = Duration::from_millis(10);
761763

762-
let sim_clock = SimClock::default();
763764
let (_guard, db_pool, db_close) = Database::Sqlite.set_up().await;
764765

765766
let server_address = listener
@@ -935,25 +936,20 @@ pub(crate) mod tests {
935936
pub(crate) async fn create_activity_worker_with_allowed_host(
936937
wasm_path: &str,
937938
listener: &std::net::TcpListener,
939+
clock_fn: Box<dyn ClockFn>,
938940
) -> Arc<dyn Worker> {
939941
let engine = Engines::get_activity_engine_test(EngineConfig::on_demand_testing()).unwrap();
940-
let sim_clock = SimClock::default();
941942
let server_address = listener
942943
.local_addr()
943944
.expect("Failed to get server address.");
944945
let uri = format!("http://127.0.0.1:{port}", port = server_address.port());
945946

946-
let (worker, _) = new_activity_worker_with_config(
947-
wasm_path,
948-
engine,
949-
sim_clock.clone_box(),
950-
TokioSleep,
951-
{
947+
let (worker, _) =
948+
new_activity_worker_with_config(wasm_path, engine, clock_fn, TokioSleep, {
952949
let uri = uri.clone();
953950
move |component_id| activity_config_allowed_host(component_id, &uri)
954-
},
955-
)
956-
.await;
951+
})
952+
.await;
957953
worker
958954
}
959955

@@ -1611,15 +1607,18 @@ pub(crate) mod tests {
16111607
test_utils::set_up();
16121608

16131609
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1610+
let sim_clock = SimClock::epoch();
16141611
let worker = create_activity_worker_with_allowed_host(
16151612
test_programs_http_get_activity_builder::TEST_PROGRAMS_HTTP_GET_ACTIVITY,
16161613
&listener,
1614+
sim_clock.clone_box(),
16171615
)
16181616
.await;
16191617

16201618
run_http_get_retry_test(
16211619
listener,
16221620
worker,
1621+
sim_clock,
16231622
HTTP_GET_SUCCESSFUL_ACTIVITY,
16241623
|uri| Params::from_json_values_test(vec![json!(uri)]),
16251624
locking_strategy,

0 commit comments

Comments
 (0)