Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clock: Move to zapcore #950

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 0 additions & 43 deletions clock.go

This file was deleted.

13 changes: 0 additions & 13 deletions clock_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
}
}