Twenty 垃圾清理提早一天刪除紀錄,作者示範如何用 ChronoStack 在 2 分鐘內測試
A trash cleanup that deleted records a day early, and how I tested it in 2 minutes
開源 CRM Twenty 的垃圾保留期限計算錯誤,令紀錄提早刪除;作者用 ChronoStack 在不到 2 分鐘內重現並測試修正。錯誤源於截止日期計算多加一天,並混用 UTC 與本地時間。ChronoStack 是版本為 0.1.0-alpha.8 的開源 alpha 工具,可推進整個 Docker Compose 堆疊的虛擬時鐘,並在時間推進過程中執行定時工作。
Twenty, the open-source CRM, keeps deleted records in the trash for 14 days before deleting them for good. At least, that's what it says. A record deleted at 23:50 was actually gone after 13 days and 20 minutes. With retention set to 1 day, a record deleted just before midnight UTC disappeared 11 minutes later.
The code looked reasonable:
private calculateCutoffDate(trashRetentionDays: number): Date {
const cutoffDate = new Date();
cutoffDate.setUTCHours(0, 0, 0, 0);
cutoffDate.setDate(cutoffDate.getDate() - trashRetentionDays + 1);
return cutoffDate;
}
A nightly job at 00:10 deletes everything with deletedAt < cutoff. The + 1 moves the cutoff forward a whole day, so anything deleted during day 0 is removed at 00:10 on day 14, whatever time it was deleted. There's a quieter second problem too: setUTCHours is mixed with the local-time setDate/getDate, so the result also depends on the server's time zone.
The fix was small: drop the + 1, and stay in UTC. Twenty merged it.
Why nobody caught it
This bug only shows up if you wait two weeks, delete records at the right time of day, and check after a real cron run. Unit tests usually mock new Date(), so they check the arithmetic, not what happens when Postgres's now(), the cron schedule and the job all run together.
So most of us test time-based features by:
- editing timestamps in the database by hand,
- adding a "make the trial 2 minutes long" flag, or
- not testing them at all.
What I built
I got tired of doing the first one, so I built ChronoStack. It moves the clock forward for a whole Docker Compose stack at once (Postgres now(), Redis TTLs and your services) with no code changes. It runs a separate copy of your stack, so your real data isn't touched.
chronostack up --wait # a copy of your compose stack on a virtual clock
chronostack advance 15d # 15 days later everywhere; your nightly jobs ran on the way
chronostack run scenarios/trial.yml # a scripted, repeatable test with a report
For the Twenty bug, a scenario moves a record to the trash at 23:50, steps forward a day at a time, and checks the record is still there just before the 14 days are up. Then it sets retention to 1 day and does the same at 23:59. On Twenty's published image it fails. With the fix, it passes. A full run takes under 2 minutes instead of two weeks.
The hard part wasn't the fake clock
Faking the time is an old trick (libfaketime has done it for years). What took most of the work was everything that breaks once you do it:
- Timers don't fire when the wall clock jumps. setTimeout and most in-process schedulers run on the monotonic clock, which ChronoStack deliberately leaves alone. So advance stops at each job's scheduled time and runs it.
- Some waits hang forever. pthread_cond_timedwait and friends take a wall-clock deadline. Once virtual time is ahead, those deadlines are wrong, so the shim translates them back to real time. That's checked every night in C, Python, Node, the JVM, Ruby, PHP and .NET.
- pg_cron skips jobs after a jump of more than 3 hours, so the clock stops on each job's minute.
- BullMQ runs a job twice if you jump while it's running, because its lock expires. So advance waits for the queues to drain first.
Users, too
Real bugs often need real behaviour: people signing up over a week, coming back days later, some of them paying. So a scenario can bring simulated users along, and they act on the virtual clock:
cohorts:
- { name: trialists, users: 20, arrive: { over: 7d }, journey: trial }
journeys:
trial:
- http: { method: POST, url: "${api}/signup?email=u${user}-${run_id}@example.com" } save: { token: /token }
- think: { between: [1h, 20h] }
- http: { url: "${api}/me", headers: { x-session: "${token}" } } expect: { status: 200 }
In the demo, 20 trialists over 30 virtual days, with the partner's webhook endpoint down for 4 hours on day 15, take about 3 minutes. Every random choice comes from a seed, so the same seed gives the same timeline of who did what when. chronostack report diff then tells you what changed between two runs, for example before and after a fix.
Other bugs it found
I ran it against a few open-source projects' published Docker images. Besides Twenty, it found date bugs in Saleor, Chatwoot, Formbricks, tududi and Worklenz; Worklenz has merged the fix too. The full list, with issues and fix PRs, is in the README (https://github.com/Asmodeus14/ChronoStack#found-in-real-projects).
Try it
It's an alpha (0.1.0-alpha.8), open source under MIT/Apache-2.0. It works on Linux, Windows and Intel Macs; Apple-silicon Macs are untested so far (if you have one, issue #5 (https://github.com/Asmodeus14/ChronoStack/issues/5) is a 5-minute test).
brew install asmodeus14/tap/chronostack # macOS, Linux
scoop bucket add chronostack https://github.com/Asmodeus14/scoop-bucket # Windows
scoop install chronostack/chronostack
cargo install chronostack@0.1.0-alpha.8 # anywhere with Rust
- Repo: https://github.com/Asmodeus14/ChronoStack
- Site, with an interactive demo: https://chronostack-dev.vercel.app
If you try it on your own stack, I'd love to hear what breaks, especially the output of chronostack doctor.
And yes i used AI specifically Claude Code but only for testing the codes , pipelines and finding bugs in chronostack and fixing them .
來源:DEV Community · dev.to