from Hacker News

Quartz: A Deterministic Time Testing Library for Go

by ammario on 7/15/24, 6:30 PM with 46 comments

  • by neild on 7/15/24, 9:05 PM

    Of possible interest to anyone testing concurrent Go code in general and time in particular, a proposed standard library package: https://go.dev/issue/67434
  • by playingalong on 7/15/24, 8:26 PM

    It feels like every second, sorry every other time-related library is called Quartz.
  • by elchief on 7/15/24, 8:26 PM

    not a great name considering Quartz schedular for java...
  • by jamesrr39 on 7/16/24, 5:41 PM

    At the risk of appearing low-tech, a much more simple, Goroutine-safe solution for dealing with "now-dependent" code:

    type NowFunc func() time.Time

    func getGreeting(nowFunc NowFunc) string {

      now := nowFunc()
    
      if now.Hour() < 12 {
    
        return "Good Morning"
    
      }
    
      return "Good day"
    
    }

    And just pass in `time.Now` in for live code, and your own inline function for simulating a time in tests.

  • by alpb on 7/16/24, 1:58 AM

  • by nsguy on 7/15/24, 10:05 PM

    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 ( https://pkg.go.dev/github.com/benbjohnson/clock ). It had a lot of problems. This was many years ago, looks like it's seen some maintenance since so maybe it'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'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.

    It's quite tricky is sort of the bottom line. It's not enough to just create fake time there's a lot more to it.

  • by rmetzler on 7/16/24, 7:03 AM

    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't even compile for time-based problems, e.g. logic around Feb 29th or daylight saving time.
  • by wolfspaw on 7/15/24, 9:23 PM

    Nice, Golang is Awesome!
  • by wizzard0 on 7/15/24, 7:46 PM

    Very cool! Anything similar for .net?