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

Finish replacing benbjohnson/clock with coder/quartz #4002

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/aws/aws-sdk-go v1.55.5
github.com/benbjohnson/clock v1.3.5
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cespare/xxhash/v2 v2.3.0
github.com/coder/quartz v0.1.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:W
github.com/aws/aws-sdk-go v1.38.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
8 changes: 4 additions & 4 deletions nflog/nflog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sync"
"time"

"github.com/benbjohnson/clock"
"github.com/coder/quartz"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/matttproud/golang_protobuf_extensions/pbutil"
Expand Down Expand Up @@ -76,7 +76,7 @@ func QGroupKey(gk string) QueryParam {

// Log holds the notification log state for alerts that have been notified.
type Log struct {
clock clock.Clock
clock quartz.Clock

logger log.Logger
metrics *metrics
Expand Down Expand Up @@ -259,7 +259,7 @@ func New(o Options) (*Log, error) {
}

l := &Log{
clock: clock.New(),
clock: quartz.NewReal(),
retention: o.Retention,
logger: log.NewNopLogger(),
st: state{},
Expand Down Expand Up @@ -305,7 +305,7 @@ func (l *Log) Maintenance(interval time.Duration, snapf string, stopc <-chan str
level.Error(l.logger).Log("msg", "interval or stop signal are missing - not running maintenance")
return
}
t := l.clock.Ticker(interval)
t := l.clock.NewTicker(interval)
defer t.Stop()

var doMaintenance MaintenanceFunc
Expand Down
18 changes: 9 additions & 9 deletions nflog/nflog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (

pb "github.com/prometheus/alertmanager/nflog/nflogpb"

"github.com/benbjohnson/clock"
"github.com/coder/quartz"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)

func TestLogGC(t *testing.T) {
mockClock := clock.NewMock()
mockClock := quartz.NewMock(t)
now := mockClock.Now()
// We only care about key names and expiration timestamps.
newEntry := func(ts time.Time) *pb.MeshEntry {
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLogGC(t *testing.T) {

func TestLogSnapshot(t *testing.T) {
// Check whether storing and loading the snapshot is symmetric.
mockClock := clock.NewMock()
mockClock := quartz.NewMock(t)
now := mockClock.Now().UTC()

cases := []struct {
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestWithMaintenance_SupportsCustomCallback(t *testing.T) {
}

l, err := New(opts)
clock := clock.NewMock()
clock := quartz.NewMock(t)
l.clock = clock
require.NoError(t, err)

Expand All @@ -160,12 +160,12 @@ func TestWithMaintenance_SupportsCustomCallback(t *testing.T) {
gosched()

// Before the first tick, no maintenance executed.
clock.Add(99 * time.Millisecond)
clock.Advance(99 * time.Millisecond)
require.EqualValues(t, 0, calls.Load())

// Tick once.
clock.Add(1 * time.Millisecond)
require.EqualValues(t, 1, calls.Load())
clock.Advance(1 * time.Millisecond)
require.Eventually(t, func() bool { return calls.Load() == 1 }, 5*time.Second, time.Second)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now required as otherwise the require.EqualValues is run before the goroutine. This didn't happen with benbjohnson/clock as it included additional synchronization code to ensure all timers fired before clock.Add returned. This made tests easier to write, but is not how the time package behaves in real life.


// Stop the maintenance loop. We should get exactly one more execution of the maintenance func.
close(stopc)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestReplaceFile(t *testing.T) {
}

func TestStateMerge(t *testing.T) {
mockClock := clock.NewMock()
mockClock := quartz.NewMock(t)
now := mockClock.Now()

// We only care about key names and timestamps for the
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestStateMerge(t *testing.T) {

func TestStateDataCoding(t *testing.T) {
// Check whether encoding and decoding the data is symmetric.
mockClock := clock.NewMock()
mockClock := quartz.NewMock(t)
now := mockClock.Now().UTC()

cases := []struct {
Expand Down
Loading