Skip to content

Provides a "Clock" interface for facilitating dependency injection in time-related Go code.

License

Notifications You must be signed in to change notification settings

karagog/clock-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clock-go

Provides a "Clock" interface for facilitating dependency injection in time-related Go code.

Why?

Golang's "time" package does not facilitate dependency injection by itself, because it relies on package-level functions (e.g. time.Now()). It can be very tricky to properly unit test code like that, because it may require carefully choreographed sleep statements and may end up non-deterministic/flaky. Using a dependency-injected clock allows you to use a real clock in production and a deterministic simulated clock in unit tests.

Example

package main

import (
  "github.com/karagog/clock-go"
  "github.com/karagog/clock-go/real"
  "github.com/karagog/clock-go/simulated"
)

// Given a function that needs the current time, inject a clock
// object instead of calling "time.Now()".
func DoIt(c clock.Clock) {
  fmt.Printf("The time is %v", c.Now())
}

func main() {
  // You can inject a real clock, which delegates to time.Now().
  DoIt(&real.Clock{})

  // ...or you can use a simulated clock, whose output is
  // deterministic and controlled by you.
  //
  // For example, this simulated clock is one hour in the future:
  s := simulated.NewClock(time.Now().Add(time.Hour))
  DoIt(s)

  // You can advance the time in any increments you want.
  s.Advance(time.Second)
  DoIt(s)
}

About

Provides a "Clock" interface for facilitating dependency injection in time-related Go code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages