Implement unified time testing with controllable app_now() function#329
Open
tonyalaribe wants to merge 2 commits intomasterfrom
Open
Implement unified time testing with controllable app_now() function#329tonyalaribe wants to merge 2 commits intomasterfrom
tonyalaribe wants to merge 2 commits intomasterfrom
Conversation
…nction
Implements a hybrid approach to unify time management across the Haskell
app layer and PostgreSQL database in tests, enabling time fast-forwarding
for timeseries data and background job testing.
Three components:
1. Mutable TestClock (src/Pkg/TestClock.hs):
- IORef-backed clock with advanceTime/setTestTime/getTestTime
- runMutableTime: custom Time effect interpreter reading from IORef
- runWithTimeSyncedPool: pool wrapper that sets DB GUC on each checkout
2. app_now() PostgreSQL function (migration 0033):
- Reads app.current_time GUC, falls back to NOW() in production
- Updated triggers: set_updated_at, new_anomaly_proc,
check_triggered_query_monitors, check_tests_to_trigger
3. NOW() replaced with ? parameters in 16 source files:
- All SQL queries now receive time from Time.currentTime effect
- Production uses real time; tests use mutable TestClock time
Test infrastructure updates:
- TestResources gains trTestClock field
- All test runners thread TestClock through effect stacks
- Replaced getCurrentTime with getTestTime in all test specs
- Added advanceTestTime/advanceMinutes/advanceHours/advanceDays helpers
https://claude.ai/code/session_019Mn7XLed4oq6j7dWqciekH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a unified time testing system that allows tests to control time across both Haskell effects and PostgreSQL triggers/defaults. It replaces the frozen time approach with a mutable, advanceable test clock that syncs with PostgreSQL's
app_now()function via a custom GUC variable.Key Changes
Database Migration
static/migrations/0033_app_now_function.sqlwhich:app_now()function that reads fromapp.current_timeGUC variable, falling back toNOW()in productionset_updated_at()trigger to useapp_now()new_anomaly_proc(),check_triggered_query_monitors(), andcheck_tests_to_trigger()procedures to useapp_now()for background job schedulingNew Test Clock Module
src/Pkg/TestClock.hsproviding:TestClocktype backed by anIORef UTCTimeadvanceTimeandsetTestTimefor controlling the test clockrunMutableTimeto interpret theTimeeffect from a mutable clocksyncConnectionTimeto sync the test clock to PostgreSQL's GUC on each connection checkoutrunWithTimeSyncedPoolto wrap connection pools with automatic time synchronizationTest Infrastructure Updates
Pkg.TestUtilsto:TestClockparameter in background job runnersadvanceTestTime,advanceMinutes,advanceHours,advanceDays)TestClockinTestResourcesrunMutableTimeandrunWithTimeSyncedPoolinstead ofrunFrozenTimeQuery Updates for Time Consistency
Time :> esconstraint and useTime.currentTimeinstead ofNOW():Models.Apis.RequestDumps:getRequestDumpForReports,getRequestDumpsForPreviousReportPeriodModels.Telemetry.Telemetry:getDataPointsData,getMetricChartListDataModels.Apis.Anomalies:countAnomalies,acknowledgeAnomaliesModels.Apis.Issues:updateIssueWithNewAnomaly,updateIssueEnhancementModels.Apis.LogPatterns:acknowledgeLogPatternsModels.Apis.Monitors:updateQMonitorTriggeredStateModels.Projects.Projects:projectCacheByIdModels.Projects.ProjectMembersandModels.Projects.ProjectApiKeys: Similar updatesTest Updates
TestClockto background job runners and response handlersgetTestTimeinstead ofgetCurrentTimefor test assertionsrunFrozenTimein favor of mutable clockImplementation Details
set_config('app.current_time', ?, true)which makes the setting transaction-scoped and auto-resets when connections return to the poolNOW()callshttps://claude.ai/code/session_019Mn7XLed4oq6j7dWqciekH