News for hackers. Stuff that matters.
Re:10 years ago, someone wrote a test for servo that include... by vocx2tx on Monday April 20, 2026 @11:50AM (#47836075)
But still a kludge. Better: use something equivalent to Go's testing/synctest[0] package, which lets you write tests that run in a bubble where time is fixed and deterministic.

[0] https://pkg.go.dev/testing/synctest

Re:10 years ago, someone wrote a test for servo that include... by dathinab on Monday April 20, 2026 @12:02PM (#47836233)
in general

- generating test data in a realistic way is often better then hard coding it (also makes it easier to add prop testing or similar)

- make the current time an input to you functions (i.e. the whole old prefer pure functions discussion). This isn't just making things more testable it also can matter to make sure: 1. one unit of logic sees the same time 2. avoid unneeded calls to `now()` (only rarely matters, but can matter)

Re:10 years ago, someone wrote a test for servo that include... by WorldMaker on Monday April 20, 2026 @12:32PM (#47836682)
Similarly, I like .NET's TimeProvider abstraction [1]. You pass a TimeProvider to your functions. At runtime you can provide the default TimeProvider.System. When testing FakeTimeProvider has a lot of handy tools to do deterministic testing.

One of the further benefits of .NET's TimeProvider is that it can also be provided to low level async methods like `await Task.Delay(time, timeProvider, cancellationToken)` which also increases the testability of general asynchronous code in a deterministic sandbox once you learn to pass TimeProvider to even low level calls that take an optional one.

[1] https://learn.microsoft.com/en-us/dotnet/standard/datetime/t...

Re:10 years ago, someone wrote a test for servo that include... by rzzzt on Monday April 20, 2026 @04:07PM (#47839833)
Java has an interface named InstantSource for this purpose: https://docs.oracle.com/en/java/javase/17/docs/api/java.base...
Re:10 years ago, someone wrote a test for servo that include... by layer8 on Monday April 20, 2026 @04:30PM (#47840096)
Paradoxically, InstantSource may have a delay. ;)
Re:10 years ago, someone wrote a test for servo that include... by 0x457 on Monday April 20, 2026 @12:45PM (#47836918)
Also, if you do use `now()` in this case you can always do `now() + SomeDistantDuration`
Re:10 years ago, someone wrote a test for servo that include... by bombcar on Monday April 20, 2026 @01:46PM (#47837960)
This can cause other types of bugs to go unnoticed, such as leap year fun (if you handle 100 years, did you handle the 400th year?).
Re:10 years ago, someone wrote a test for servo that include... by Bratmon on Monday April 20, 2026 @02:24PM (#47838521)
Doesn't that just turn bugs in test in n years into bugs in prod in n years?

That seems like a downgrade to me!

Re:10 years ago, someone wrote a test for servo that include... by loeg on Monday April 20, 2026 @02:52PM (#47838885)
No, because prod doesn't have hardcoded cookies baked into it?
Re:10 years ago, someone wrote a test for servo that include... by AndrewDucker on Monday April 20, 2026 @03:22PM (#47839262)
If you always test with a date of 1/1/2000 then you don't know that your choice fails in 2039.
Re:10 years ago, someone wrote a test for servo that include... by vocx2tx on Monday April 20, 2026 @04:15PM (#47839906)
These fake-time environments let you set the time, so you can test how the code will behave in 2039 without waiting for 13 years. For Go's synctest, 1-1-2000 is just the default initial value for now().
Re:10 years ago, someone wrote a test for servo that include... by jcul on Monday April 20, 2026 @03:34PM (#47839432)
libfaketime is cool for testing this kind of thing too.

Not as convenient for unit tests cause you have to run the test with LD_PRELOAD.

Re:10 years ago, someone wrote a test for servo that include... by rtpg on Monday April 20, 2026 @04:44PM (#47840293)
I’ve used freezetime (Python) a decent amount and have experienced some very very very funny flakes due to it.

- Sometimes your test code expects time to be moving forward

- sometimes your code might store classes into a hashmap for caching, and the cache might be built before the freeze time class override kicks in

- sometimes it happens after you have patched the classes and now your cache is weirdly poisoned

- sometimes some serialization code really cares about the exact class used

- sometimes test code acts really weird if time stops moving forward (when people use freezetime frozen=true). Selenium timeouts never clearing was funny

- sometimes your code gets a hold of the unpatched date clsss through silliness but only in one spot

Fun times.

The nicest thing is being able to just pass in a “now” parameter in things that care about time.

Re:10 years ago, someone wrote a test for servo that include... by latchkey on Monday April 20, 2026 @08:58PM (#47843274)
Re:10 years ago, someone wrote a test for servo that include... by pmontra on Monday April 20, 2026 @11:48PM (#47844303)
Ruby's timecop is great for those test scenarios https://github.com/travisjeffery/timecop