Skip to content

Commit

Permalink
Finish replacing benbjohnson/clock with coder/quartz
Browse files Browse the repository at this point in the history
This commit finishes replacing benbjohnson/clock with coder/quartz
and removes it from go.mod.

Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Aug 27, 2024
1 parent ab43965 commit 182a9ff
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
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
25 changes: 9 additions & 16 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 @@ -157,15 +157,14 @@ func TestWithMaintenance_SupportsCustomCallback(t *testing.T) {
return 0, nil
})
}()
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)

// Stop the maintenance loop. We should get exactly one more execution of the maintenance func.
close(stopc)
Expand Down Expand Up @@ -212,7 +211,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 +273,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 Expand Up @@ -375,9 +374,3 @@ func TestStateDecodingError(t *testing.T) {
_, err = decodeState(bytes.NewReader(msg))
require.Equal(t, ErrInvalidState, err)
}

// runtime.Gosched() does not "suspend" the current goroutine so there's no guarantee that the main goroutine won't
// be able to continue. For more see https://pkg.go.dev/runtime#Gosched.
func gosched() {
time.Sleep(1 * time.Millisecond)
}

0 comments on commit 182a9ff

Please sign in to comment.