Skip to content

Commit b7d2f08

Browse files
committed
comment: Test Double clocks
1 parent 139ed82 commit b7d2f08

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

_posts/2022-06-27-test-double-clocks.html

+59
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,63 @@ <h2 id="comments-header">
378378
</div>
379379
<div class="comment-date">2022-06-30 7:43 UTC</div>
380380
</div>
381+
<div class="comment" id="5aa51b5af530409290c0944b07ce9b10">
382+
<div class="comment-author"><a href="https://github.com/c-vetter">C. Vetter</a> <a href="#5aa51b5af530409290c0944b07ce9b10">#</a></div>
383+
<div class="comment-content">
384+
<figure class="quote">
385+
<blockquote>
386+
Yeah, not really. This isn't worse, but neither is it better. It's the same size of code, and while the dates are now explicit (<a href="https://peps.python.org/pep-0020/">which, ostensibly, is better</a>), the reader now has to deduce the relationship between the clock offset, <code>r1</code>, and <code>r2</code>. I'm not convinced that this is an improvement.
387+
</blockquote>
388+
</figure>
389+
<p>I think, you overlook an important fact here: It depends™.</p>
390+
391+
<p>
392+
As <a href="https://youtu.be/pSOBeD1GC_Y">Obi Wan taught us</a>, the point of view is often quite important.
393+
In this case, yes it's true, the changed code is more explicit, from a certain point of view, because <em>the dates are now explicit</em>.
394+
But: in the previous version, the <em>relationships were explicit</em>, whereas they have been rendered implicit now.
395+
Which is better depends on the context, and in this context, I think the change is for the worse.
396+
</p>
397+
398+
<p>
399+
In this context, I think we care neither about the specific dates nor the relationship between both reservation dates.
400+
All we care about is their relationship to the present and that they are different from each other.
401+
With that in mind, I'd suggest to extend your <code>Some</code> container with more datetimes, in addition to <code>Now</code>,
402+
like <code>FutureDate</code> and <code>OtherFutureDate</code>.
403+
</p>
404+
405+
<p>
406+
How those are constructed is generally of no relevance to the current test.
407+
After all, if we wanted to be 100% sure about every piece,
408+
we'd basically have re-write our entire runtime for each test case, which would just be madness.
409+
And yes, I'd just construct them based on the current system time.
410+
</p>
411+
412+
<p>
413+
Regarding the overall argument, I'll say that dealing with time issues is generally a pain,
414+
but most of the time, we don't really need to deal with what happens at specific times.
415+
In those rare cases, yes, it makes sense to fix the test's time, but I'd leave that as a rare exception.
416+
Partly because such tests tend to require some kind of wide-ranging mock that messes with a lot of things.
417+
</p>
418+
419+
<p>
420+
If we're talking about stuff like Y2k-proofing (if you're too young to remember, look it up, kids),
421+
it bears thinking about actually creating a whole test machine (virtual or physical)
422+
with an appropriate system time and just running your test suite on there.
423+
In times of docker, I'll bet that that will be less pain in many cases than adding time-fixing mock stuff.
424+
</p>
425+
426+
<p>
427+
If passage of time is important, that's another bag of pain right there,
428+
but I'd look into segregating that as much as possible from everything else.
429+
If, for example, you need things to happen after 100 minutes have passed,
430+
I'd prefer having a single time-based event system that
431+
all other code can subscribe to and be called back when the interesting time arrives.
432+
That way, I can test the consumers without actually travelling through time,
433+
while testing the timer service will be reduced to making sure that events are fired at the appropriate times.
434+
<small>The latter could even happen on a persistent test machine that just keeps running,
435+
giving you insight on long-time behavior (just an idea, not a prescription 😉).</small>
436+
</p>
437+
</div>
438+
<div class="comment-date">2024-05-08 11:13 UTC</div>
439+
</div>
381440
</div>

0 commit comments

Comments
 (0)