Skip to content

Commit 96a525b

Browse files
committed
test: Make SimClock deterministic by default
1 parent 87ac52e commit 96a525b

11 files changed

Lines changed: 31 additions & 43 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ 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
};
@@ -342,7 +342,7 @@ mod tests {
342342
},
343343
allowed_hosts: Vec::new(),
344344
logs_storage_config: None,
345-
clock_fn: Now.clone_box(),
345+
clock_fn: SimClock::epoch().clone_box(),
346346
}
347347
}
348348

@@ -481,7 +481,7 @@ mod tests {
481481
executor_id: ExecutorId::generate(),
482482
deployment_id: DEPLOYMENT_ID_DUMMY,
483483
run_id: RunId::generate(),
484-
lock_expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
484+
lock_expires_at: chrono::DateTime::UNIX_EPOCH + chrono::Duration::seconds(60),
485485
retry_config: ComponentRetryConfig::ZERO,
486486
},
487487
executor_close_watcher,
@@ -517,7 +517,7 @@ mod tests {
517517
executor_id: ExecutorId::generate(),
518518
deployment_id: DEPLOYMENT_ID_DUMMY,
519519
run_id: RunId::generate(),
520-
lock_expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
520+
lock_expires_at: chrono::DateTime::UNIX_EPOCH + chrono::Duration::seconds(60),
521521
retry_config: ComponentRetryConfig::ZERO,
522522
},
523523
executor_close_watcher,

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ pub(crate) mod tests {
548548
use concepts::storage::http_client_trace::{RequestTrace, ResponseTrace};
549549
use concepts::storage::{DbPool, TimeoutOutcome};
550550
use concepts::storage::{ExecutionRequest, Version};
551-
use concepts::time::Now;
552551
use concepts::time::TokioSleep;
553552
use concepts::{ComponentRetryConfig, ComponentType};
554553
use concepts::{
@@ -1038,18 +1037,20 @@ pub(crate) mod tests {
10381037
let engine =
10391038
Engines::get_activity_engine_test(EngineConfig::pooling_nocache_testing(pool_opts))
10401039
.unwrap();
1040+
let sim_clock = SimClock::epoch();
10411041

10421042
let (fibo_worker, _) = new_activity_worker(
10431043
test_programs_fibo_activity_builder::TEST_PROGRAMS_FIBO_ACTIVITY,
10441044
engine,
1045-
Now.clone_box(),
1045+
sim_clock.clone_box(),
10461046
TokioSleep,
10471047
)
10481048
.await;
10491049
// create executions
10501050
let join_handles = (0..tasks)
10511051
.map(|_| {
10521052
let fibo_worker = fibo_worker.clone();
1053+
let sim_clock = sim_clock.clone();
10531054
let execution_id = ExecutionId::generate();
10541055
let (executor_close_tx, executor_close_watcher) =
10551056
tokio::sync::watch::channel(false);
@@ -1070,7 +1071,7 @@ pub(crate) mod tests {
10701071
executor_id: ExecutorId::generate(),
10711072
deployment_id: DEPLOYMENT_ID_DUMMY,
10721073
run_id: RunId::generate(),
1073-
lock_expires_at: Now.now() + lock_expiry,
1074+
lock_expires_at: sim_clock.now() + lock_expiry,
10741075
retry_config: ComponentRetryConfig::ZERO,
10751076
},
10761077
executor_close_watcher,
@@ -1386,7 +1387,6 @@ pub(crate) mod tests {
13861387
let created_at = sim_clock.now();
13871388
let db_connection = db_pool.connection_test().await.unwrap();
13881389
info!("Creating execution");
1389-
let stopwatch = std::time::Instant::now();
13901390
db_connection
13911391
.create(CreateRequest {
13921392
created_at,
@@ -1423,8 +1423,6 @@ pub(crate) mod tests {
14231423
.len()
14241424
);
14251425
let exec_log = db_connection.get(&execution_id).await.unwrap();
1426-
let stopwatch = stopwatch.elapsed();
1427-
info!("Finished in {stopwatch:?}");
14281426
let (res, http_client_traces) = assert_matches!(
14291427
exec_log.last_event().event.clone(),
14301428
ExecutionRequest::Finished { retval, http_client_traces: Some(http_client_traces) }
@@ -1518,7 +1516,6 @@ pub(crate) mod tests {
15181516
let created_at = sim_clock.now();
15191517
let db_connection = db_pool.connection_test().await.unwrap();
15201518
info!("Creating execution");
1521-
let stopwatch = std::time::Instant::now();
15221519
db_connection
15231520
.create(CreateRequest {
15241521
created_at,
@@ -1555,8 +1552,6 @@ pub(crate) mod tests {
15551552
.len()
15561553
);
15571554
let exec_log = db_connection.get(&execution_id).await.unwrap();
1558-
let stopwatch = stopwatch.elapsed();
1559-
info!("Finished in {stopwatch:?}");
15601555
let (res, http_client_traces) = assert_matches!(
15611556
exec_log.last_event().event.clone(),
15621557
ExecutionRequest::Finished { retval, http_client_traces: Some(http_client_traces) }

crates/wasm-workers/src/workflow/replay_db_proxy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ mod tests {
737737
use super::*;
738738
use crate::workflow::caching_db_connection::{CachingBuffer, CachingDbConnection};
739739
use crate::workflow::workflow_worker::JoinNextBlockingStrategy;
740-
use chrono::Utc;
741740
use concepts::{FunctionFqn, Params};
742741
use rstest::rstest;
743742

@@ -755,7 +754,7 @@ mod tests {
755754
let real_connection = db_pool.connection().await.unwrap();
756755
let parent_execution_id = ExecutionId::from_parts(0, 0);
757756
let child_execution_id = ExecutionId::from_parts(0, 1);
758-
let created_at = Utc::now();
757+
let created_at = DateTime::UNIX_EPOCH;
759758
let parent_version = real_connection
760759
.create(CreateRequest {
761760
created_at,

crates/wasm-workers/src/workflow/workflow_ctx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ pub(crate) mod tests {
29952995
PendingStateFinishedError, PendingStateFinishedResultKind, PendingStatePendingAt,
29962996
};
29972997
use concepts::storage::{DbPoolCloseable, ExecutionLog};
2998-
use concepts::time::{ClockFn, Now};
2998+
use concepts::time::ClockFn;
29992999
use concepts::{
30003000
ComponentId, ComponentRetryConfig, ExecutionFailureKind, ExecutionMetadata,
30013001
FinishedExecutionFailure, FunctionRegistry, IfcFqnName, JoinSetId, JoinSetKind,
@@ -3655,7 +3655,7 @@ pub(crate) mod tests {
36553655
const SUBMITS: usize = 10;
36563656
test_utils::set_up();
36573657
let (_guard, db_pool, db_close) = Database::Sqlite.set_up().await;
3658-
let sim_clock = SimClock::new(Now.now());
3658+
let sim_clock = SimClock::epoch();
36593659
let db_connection = db_pool.connection_test().await.unwrap();
36603660

36613661
// Create an execution.

crates/wasm-workers/src/workflow/workflow_js_worker.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ mod tests {
606606
PendingStateFinished, PendingStateFinishedError, PendingStateFinishedResultKind,
607607
PendingStatePendingAt, Version,
608608
};
609-
use concepts::time::{ClockFn, Now, TokioSleep};
609+
use concepts::time::{ClockFn, TokioSleep};
610610
use concepts::{
611611
ComponentRetryConfig, ComponentType, ExecutionId, ExecutionMetadata, StrVariant,
612612
TypeWrapperTopLevel,
@@ -750,7 +750,7 @@ mod tests {
750750
) {
751751
let engine = Engines::get_workflow_engine_test(EngineConfig::on_demand_testing()).unwrap();
752752
let cancel_registry = CancelRegistry::new();
753-
let clock_fn: Box<dyn ClockFn> = Now.clone_box();
753+
let clock_fn: Box<dyn ClockFn> = SimClock::epoch().clone_box();
754754

755755
let component_id = concepts::ComponentId::new(
756756
ComponentType::Workflow,
@@ -829,7 +829,7 @@ mod tests {
829829
user_ffqn: &FunctionFqn,
830830
) -> Result<WorkflowJsWorkerLinked, crate::WasmFileError> {
831831
let engine = Engines::get_workflow_engine_test(EngineConfig::on_demand_testing()).unwrap();
832-
let clock_fn: Box<dyn ClockFn> = Now.clone_box();
832+
let clock_fn: Box<dyn ClockFn> = SimClock::epoch().clone_box();
833833

834834
let component_id = concepts::ComponentId::new(
835835
ComponentType::Workflow,
@@ -898,7 +898,7 @@ mod tests {
898898
executor_id: ExecutorId::generate(),
899899
deployment_id: DEPLOYMENT_ID_DUMMY,
900900
run_id: RunId::generate(),
901-
lock_expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
901+
lock_expires_at: chrono::DateTime::UNIX_EPOCH + chrono::Duration::seconds(60),
902902
retry_config: ComponentRetryConfig::WORKFLOW,
903903
},
904904
executor_close_watcher: tokio::sync::watch::channel(false).1,
@@ -1566,7 +1566,6 @@ mod tests {
15661566
);
15671567
}
15681568

1569-
let stopwatch = std::time::Instant::now();
15701569
let (log_sender, mut log_storage_recv) = mpsc::channel(100);
15711570
let replay_worker = build_js_replay_worker(
15721571
DeploymentId::generate(),
@@ -1584,7 +1583,6 @@ mod tests {
15841583
default_return_type(),
15851584
);
15861585
replay_worker.replay(execution_id).await.unwrap();
1587-
info!("Replayed in {:?}", stopwatch.elapsed());
15881586
// Drop the worker so the log_sender is closed; otherwise `recv_many` blocks indefinitely.
15891587
drop(replay_worker);
15901588
let mut buffer = Vec::new();

0 commit comments

Comments
 (0)