New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/lopatkinevgeniy/clock

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/lopatkinevgeniy/clock

  • v0.0.0-20181129135056-768e3044b657
  • Source
  • Go
  • Socket score

Version published
Created
Source

clock

Build Status GoDoc

Small timer-driven library for mocking time in Go. Clockwork drop in replacement.

Why another one time mocking library?

  • Race free
  • Full featured
  • Redesigned

Example

Suppose we have some type with time-dependent method that we wanna test. Instead of direct use time package we specify the clock field:

const incrementStateDelay = time.Hour

// myType is a type with time-dependent method that we will test.
type myType struct {
	clock clock.Clock
	state int
}

// incrementState increments myType's state with delay.
func (f *myType) incrementState() {
	f.clock.Sleep(incrementStateDelay)
	f.state++
}

Now in tests we just inject FakeClock to the tested struct. This allows us to manipulate time:

func TestExample(t *testing.T) {
	fakeClock := clock.NewFakeClock()

	// create the myType instance with fake clock.
	mt := myType{clock: fakeClock}

	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		mt.incrementState()
		wg.Done()
	}()

	// wait until incrementState is actually sleeping.
	fakeClock.BlockUntil(1)

	// assert state not changed.
	if mt.state != 0 {
		t.Fatalf("Unxepected state, expected=0 actual=%d", mt.state)
	}

	// move time forward and wait for incrementState done.
	fakeClock.Advance(incrementStateDelay)
	wg.Wait()

	// assert state incremented.
	if mt.state != 1 {
		t.Fatalf("Unxepected state, expected=1 actual=%d", mt.state)
	}
}

In production simply inject the real clock instead

mt := myType{clock: clock.NewRealClock()}

Inspired by:

FAQs

Package last updated on 29 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc