Skip to content

Commit cb3f26d

Browse files
authored
Merge pull request #790 from obeli-sk/tests-deterministic
test: Replace wall-clock with `SimClock` in activity tests
2 parents 7c7e954 + 96a525b commit cb3f26d

12 files changed

Lines changed: 92 additions & 102 deletions

File tree

crates/concepts/src/storage.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,6 @@ mod tests {
28562856
use crate::SupportedFunctionReturnValue;
28572857
use chrono::DateTime;
28582858
use chrono::Datelike;
2859-
use chrono::Utc;
28602859
use insta::assert_snapshot;
28612860
use rstest::rstest;
28622861
use std::time::Duration;
@@ -2900,7 +2899,7 @@ mod tests {
29002899
fn serde_pending_state_finished_should_work(result_kind: PendingStateFinishedResultKind) {
29012900
let expected = PendingStateFinished {
29022901
version: 0,
2903-
finished_at: Utc::now(),
2902+
finished_at: DateTime::UNIX_EPOCH,
29042903
result_kind,
29052904
};
29062905

crates/executor/src/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ mod tests {
11221122
use concepts::storage::{
11231123
ExecutionEvent, ExecutionRequest, HistoryEvent, PendingState, PendingStatePendingAt,
11241124
};
1125-
use concepts::time::{ConstClock, Now};
1125+
use concepts::time::ConstClock;
11261126
use concepts::{
11271127
FunctionMetadata, JoinSetKind, ParameterTypes, Params, RETURN_TYPE_DUMMY,
11281128
SUPPORTED_RETURN_VALUE_OK_EMPTY, StrVariant, SupportedFunctionReturnValue, TrapKind,
@@ -1162,7 +1162,7 @@ mod tests {
11621162
locking_strategy: LockingStrategy,
11631163
) {
11641164
set_up();
1165-
let created_at = Now.now();
1165+
let created_at = DateTime::UNIX_EPOCH;
11661166
let (_guard, db_pool, db_close) = database.set_up().await;
11671167
let db_connection = db_pool.connection_test().await.unwrap();
11681168
execute_simple_lifecycle_tick_based_inner(
@@ -1236,7 +1236,7 @@ mod tests {
12361236
locking_strategy: LockingStrategy,
12371237
) {
12381238
set_up();
1239-
let created_at = Now.now();
1239+
let created_at = DateTime::UNIX_EPOCH;
12401240
let clock_fn = Box::new(ConstClock(created_at));
12411241
let (_guard, db_pool, db_close) = Database::Sqlite.set_up().await;
12421242
let exec_config = ExecConfig {
@@ -1508,7 +1508,7 @@ mod tests {
15081508
locking_strategy: LockingStrategy,
15091509
) {
15101510
set_up();
1511-
let created_at = Now.now();
1511+
let created_at = DateTime::UNIX_EPOCH;
15121512
let clock_fn = Box::new(ConstClock(created_at));
15131513
let (_guard, db_pool, db_close) = Database::Sqlite.set_up().await;
15141514
let exec_config = ExecConfig {

crates/testing/db-tests/tests/diff-tests.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use concepts::storage::Pagination;
1919
use concepts::storage::Version;
2020
use concepts::storage::{AppendRequest, CreateRequest};
2121
use concepts::time::ClockFn as _;
22-
use concepts::time::Now;
2322
use obeli_db_tests::Database;
2423
use obeli_db_tests::SOME_FFQN;
2524
use rstest::rstest;
@@ -46,13 +45,13 @@ async fn diff_proptest_inner(seed: u64) {
4645
let mut unstructured = unstructured_holder.unstructured();
4746
let execution_id = ExecutionId::generate();
4847
let create_req = CreateRequest {
49-
created_at: Now.now(),
48+
created_at: DateTime::UNIX_EPOCH,
5049
execution_id: execution_id.clone(),
5150
ffqn: SOME_FFQN,
5251
params: Params::empty(),
5352
parent: None,
5453
metadata: concepts::ExecutionMetadata::empty(),
55-
scheduled_at: Now.now(),
54+
scheduled_at: DateTime::UNIX_EPOCH,
5655
component_id: ComponentId::dummy_activity(),
5756
deployment_id: DEPLOYMENT_ID_DUMMY,
5857
scheduled_by: None,
@@ -147,7 +146,7 @@ fn arbitrary_valid_append_request(
147146
) -> arbitrary::Result<AppendRequest> {
148147
let mut req = AppendRequest {
149148
event: postgres_compatible(&unstructured.arbitrary()?),
150-
created_at: Now.now(),
149+
created_at: DateTime::UNIX_EPOCH,
151150
};
152151
normalize_timestamps(&mut req, unstructured)?;
153152
Ok(req)

crates/testing/db-tests/tests/lifecycle.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use concepts::storage::{DbErrorWriteNonRetriable, HistoryEvent, ListExecutionsFi
1919
use concepts::storage::{HistoryEventScheduleAt, JoinSetResponseEvent, LogFilter};
2020
use concepts::storage::{LogCursor, LogEntry, LogInfoAppendRow};
2121
use concepts::time::ClockFn;
22-
use concepts::time::Now;
2322
use concepts::{ComponentId, Params, StrVariant, SupportedFunctionReturnValue};
2423
use concepts::{ComponentRetryConfig, JoinSetId, JoinSetKind, SUPPORTED_RETURN_VALUE_OK_EMPTY};
2524
use concepts::{ExecutionId, FunctionFqn, prefixed_ulid::ExecutorId};
@@ -1492,7 +1491,7 @@ async fn get_expired_delay(db_connection: &dyn DbConnection, sim_clock: SimClock
14921491
execution_id.clone(),
14931492
version,
14941493
AppendRequest {
1495-
created_at: Now.now(),
1494+
created_at: sim_clock.now(),
14961495
event: ExecutionRequest::HistoryEvent {
14971496
event: HistoryEvent::JoinSetRequest {
14981497
join_set_id: join_set_id.clone(),
@@ -1701,7 +1700,7 @@ async fn append_same_delay_id_twice_should_fail(
17011700
execution_id.clone(),
17021701
version,
17031702
AppendRequest {
1704-
created_at: Now.now(),
1703+
created_at: sim_clock.now(),
17051704
event: ExecutionRequest::HistoryEvent {
17061705
event: HistoryEvent::JoinSetRequest {
17071706
join_set_id: join_set_id.clone(),
@@ -1723,7 +1722,7 @@ async fn append_same_delay_id_twice_should_fail(
17231722
execution_id.clone(),
17241723
version,
17251724
AppendRequest {
1726-
created_at: Now.now(),
1725+
created_at: sim_clock.now(),
17271726
event: ExecutionRequest::HistoryEvent {
17281727
event: HistoryEvent::JoinSetRequest {
17291728
join_set_id: join_set_id.clone(),
@@ -2041,7 +2040,7 @@ async fn append_response_with_same_id_twice_should_fail(
20412040
execution_id.clone(),
20422041
version,
20432042
AppendRequest {
2044-
created_at: Now.now(),
2043+
created_at: sim_clock.now(),
20452044
event: ExecutionRequest::HistoryEvent {
20462045
event: HistoryEvent::JoinSetRequest {
20472046
join_set_id: join_set_id.clone(),
@@ -2136,7 +2135,7 @@ async fn delay_cancellation_should_be_idempotent(database: Database) {
21362135
execution_id.clone(),
21372136
version,
21382137
AppendRequest {
2139-
created_at: Now.now(),
2138+
created_at: sim_clock.now(),
21402139
event: ExecutionRequest::HistoryEvent {
21412140
event: HistoryEvent::JoinSetRequest {
21422141
join_set_id: join_set_id.clone(),

crates/testing/test-utils/src/sim_clock.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::{DateTime, Utc};
2-
use concepts::time::{ClockFn, Now};
2+
use concepts::time::ClockFn;
33
use std::{sync::Arc, time::Duration};
44
use tracing::info;
55

@@ -9,9 +9,8 @@ pub struct SimClock {
99
}
1010

1111
impl Default for SimClock {
12-
// TODO: Rename to system_time
1312
fn default() -> Self {
14-
Self::new(Now.now())
13+
Self::epoch()
1514
}
1615
}
1716

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,15 @@ mod tests {
300300
use concepts::component_id::COMPONENT_DIGEST_DUMMY;
301301
use concepts::prefixed_ulid::{DEPLOYMENT_ID_DUMMY, ExecutorId, RunId};
302302
use concepts::storage::{Locked, Version};
303-
use concepts::time::{ClockFn, Now, TokioSleep};
303+
use concepts::time::{ClockFn, TokioSleep};
304304
use concepts::{
305305
ComponentRetryConfig, ComponentType, ExecutionId, ExecutionMetadata, StrVariant,
306306
};
307307
use concepts::{SupportedFunctionReturnValue, TypeWrapperTopLevel};
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: SimClock::epoch().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();
@@ -474,7 +481,7 @@ mod tests {
474481
executor_id: ExecutorId::generate(),
475482
deployment_id: DEPLOYMENT_ID_DUMMY,
476483
run_id: RunId::generate(),
477-
lock_expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
484+
lock_expires_at: chrono::DateTime::UNIX_EPOCH + chrono::Duration::seconds(60),
478485
retry_config: ComponentRetryConfig::ZERO,
479486
},
480487
executor_close_watcher,
@@ -510,7 +517,7 @@ mod tests {
510517
executor_id: ExecutorId::generate(),
511518
deployment_id: DEPLOYMENT_ID_DUMMY,
512519
run_id: RunId::generate(),
513-
lock_expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
520+
lock_expires_at: chrono::DateTime::UNIX_EPOCH + chrono::Duration::seconds(60),
514521
retry_config: ComponentRetryConfig::ZERO,
515522
},
516523
executor_close_watcher,
@@ -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,

0 commit comments

Comments
 (0)