TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quartz: A Deterministic Time Testing Library for Go

133 pointsby ammario10 months ago

9 comments

neild10 months ago
Of possible interest to anyone testing concurrent Go code in general and time in particular, a proposed standard library package: <a href="https:&#x2F;&#x2F;go.dev&#x2F;issue&#x2F;67434" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;issue&#x2F;67434</a>
评论 #40976379 未加载
评论 #40975751 未加载
评论 #40974109 未加载
评论 #40973205 未加载
评论 #40972566 未加载
评论 #40971719 未加载
playingalong10 months ago
It feels like every second, sorry every other time-related library is called Quartz.
评论 #40974659 未加载
评论 #40977025 未加载
elchief10 months ago
not a great name considering Quartz schedular for java...
评论 #40973759 未加载
评论 #40972274 未加载
评论 #40971644 未加载
jamesrr3910 months ago
At the risk of appearing low-tech, a much more simple, Goroutine-safe solution for dealing with &quot;now-dependent&quot; code:<p>type NowFunc func() time.Time<p>func getGreeting(nowFunc NowFunc) string {<p><pre><code> now := nowFunc() if now.Hour() &lt; 12 { return &quot;Good Morning&quot; } return &quot;Good day&quot; </code></pre> }<p>And just pass in `time.Now` in for live code, and your own inline function for simulating a time in tests.
评论 #40982794 未加载
alpb10 months ago
I think <a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;k8s.io&#x2F;utils&#x2F;clock" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;k8s.io&#x2F;utils&#x2F;clock</a> and <a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;k8s.io&#x2F;utils&#x2F;clock&#x2F;testing" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;k8s.io&#x2F;utils&#x2F;clock&#x2F;testing</a> is already doing these?
评论 #40973124 未加载
nsguy10 months ago
I have a fair bit of experience writing tests for concurrent code that uses timers on Go. We started with an open source test library ( <a href="https:&#x2F;&#x2F;pkg.go.dev&#x2F;github.com&#x2F;benbjohnson&#x2F;clock" rel="nofollow">https:&#x2F;&#x2F;pkg.go.dev&#x2F;github.com&#x2F;benbjohnson&#x2F;clock</a> ). It had a lot of problems. This was many years ago, looks like it&#x27;s seen some maintenance since so maybe it&#x27;s better? Then we tried to fix it. Fixed all the obvious bugs but still had a lot of problems using it in practice. It&#x27;s not enough to just handle the calls without context of who is calling them in concurrent code. Then we switched to using gomock which ended up also being hard to use.<p>It&#x27;s quite tricky is sort of the bottom line. It&#x27;s not enough to just create fake time there&#x27;s a lot more to it.
评论 #40973221 未加载
rmetzler10 months ago
For linux, there is faketime, which will set a specific date and time for all child processes. This enables you to test software you don&#x27;t even compile for time-based problems, e.g. logic around Feb 29th or daylight saving time.
评论 #40975667 未加载
评论 #40974639 未加载
wolfspaw10 months ago
Nice, Golang is Awesome!
评论 #40971960 未加载
wizzard010 months ago
Very cool! Anything similar for .net?
评论 #40971544 未加载