Skip to content

Commit

Permalink
clock: Move to zapcore (#950)
Browse files Browse the repository at this point in the history
We don't need to export the Clock interface from the top-level Zap
package. This is a low-level API so we can keep it in zapcore only.
  • Loading branch information
abhinav committed May 25, 2021
1 parent dedcdad commit 7699673
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 60 deletions.
43 changes: 0 additions & 43 deletions clock.go

This file was deleted.

13 changes: 0 additions & 13 deletions clock_test.go
Expand Up @@ -45,16 +45,3 @@ func TestWithClock(t *testing.T) {
assert.Equal(t, date, logs.All()[0].Entry.Time, "Unexpected entry time.")
})
}

func TestSystemClockNewTicker(t *testing.T) {
want := 3

var n int
timer := _systemClock.NewTicker(time.Millisecond)
for range timer.C {
n++
if n == want {
return
}
}
}
4 changes: 2 additions & 2 deletions logger.go
Expand Up @@ -51,7 +51,7 @@ type Logger struct {

callerSkip int

clock Clock
clock zapcore.Clock
}

// New constructs a new Logger from the provided zapcore.Core and Options. If
Expand All @@ -72,7 +72,7 @@ func New(core zapcore.Core, options ...Option) *Logger {
core: core,
errorOutput: zapcore.Lock(os.Stderr),
addStack: zapcore.FatalLevel + 1,
clock: _systemClock,
clock: zapcore.DefaultClock,
}
return log.WithOptions(options...)
}
Expand Down
2 changes: 1 addition & 1 deletion options.go
Expand Up @@ -141,7 +141,7 @@ func OnFatal(action zapcore.CheckWriteAction) Option {

// WithClock specifies the clock used by the logger to determine the current
// time for logged entries. Defaults to the system clock with time.Now.
func WithClock(clock Clock) Option {
func WithClock(clock zapcore.Clock) Option {
return optionFunc(func(log *Logger) {
log.clock = clock
})
Expand Down
16 changes: 16 additions & 0 deletions zapcore/clock.go
Expand Up @@ -24,11 +24,27 @@ import (
"time"
)

// DefaultClock is the default clock used by Zap in operations that require
// time. This clock uses the system clock for all operations.
var DefaultClock = systemClock{}

// Clock is a source of time for logged entries.
type Clock interface {
// Now returns the current local time.
Now() time.Time

// NewTicker returns *time.Ticker that holds a channel
// that delivers "ticks" of a clock.
NewTicker(time.Duration) *time.Ticker
}

// systemClock implements default Clock that uses system time.
type systemClock struct{}

func (systemClock) Now() time.Time {
return time.Now()
}

func (systemClock) NewTicker(duration time.Duration) *time.Ticker {
return time.NewTicker(duration)
}
17 changes: 16 additions & 1 deletion zapcore/clock_test.go
Expand Up @@ -29,16 +29,18 @@ import (
"go.uber.org/atomic"
)

// controlledClock provides control over the time via a mock clock.
type controlledClock struct{ *clock.Mock }

func newControlledClock() *controlledClock {
return &controlledClock{clock.NewMock()}
}

func (c *controlledClock) NewTicker(d time.Duration) *time.Ticker {
return &time.Ticker{C: c.Ticker(d).C}
}

func TestMockClock(t *testing.T) {
func TestControlledClock_NewTicker(t *testing.T) {
var n atomic.Int32
ctrlMock := newControlledClock()

Expand All @@ -65,3 +67,16 @@ func TestMockClock(t *testing.T) {
assert.Equal(t, int32(2), n.Load())
close(quit)
}

func TestSystemClock_NewTicker(t *testing.T) {
want := 3

var n int
timer := DefaultClock.NewTicker(time.Millisecond)
for range timer.C {
n++
if n == want {
return
}
}
}

0 comments on commit 7699673

Please sign in to comment.