From 410f1a71ec61c3f22d4200415aeffe39fe00d990 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 21:27:16 +0000 Subject: [PATCH] Bump github.com/benbjohnson/clock from 1.3.0 to 1.3.5 (#504) Bumps [github.com/benbjohnson/clock](https://github.com/benbjohnson/clock) from 1.3.0 to 1.3.5. - [Release notes](https://github.com/benbjohnson/clock/releases) - [Commits](https://github.com/benbjohnson/clock/compare/v1.3.0...v1.3.5) --- updated-dependencies: - dependency-name: github.com/benbjohnson/clock dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/benbjohnson/clock/clock.go | 72 ++++++++++++++++---- vendor/modules.txt | 2 +- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 03f617892..35f446b59 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible - github.com/benbjohnson/clock v1.3.0 + github.com/benbjohnson/clock v1.3.5 github.com/go-kit/kit v0.12.0 github.com/golang/snappy v0.0.4 github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb diff --git a/go.sum b/go.sum index 6eb19ee02..4e629d20e 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,8 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.35.5/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= -github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +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= diff --git a/vendor/github.com/benbjohnson/clock/clock.go b/vendor/github.com/benbjohnson/clock/clock.go index 40555b303..14ddc0795 100644 --- a/vendor/github.com/benbjohnson/clock/clock.go +++ b/vendor/github.com/benbjohnson/clock/clock.go @@ -74,7 +74,10 @@ func (c *clock) WithTimeout(parent context.Context, t time.Duration) (context.Co // Mock represents a mock clock that only moves forward programmically. // It can be preferable to a real-time clock when testing time-based functionality. type Mock struct { - mu sync.Mutex + // mu protects all other fields in this struct, and the data that they + // point to. + mu sync.Mutex + now time.Time // current time timers clockTimers // tickers & timers } @@ -89,7 +92,9 @@ func NewMock() *Mock { // This should only be called from a single goroutine at a time. func (m *Mock) Add(d time.Duration) { // Calculate the final current time. + m.mu.Lock() t := m.now.Add(d) + m.mu.Unlock() // Continue to execute timers until there are no more before the new time. for { @@ -126,6 +131,23 @@ func (m *Mock) Set(t time.Time) { gosched() } +// WaitForAllTimers sets the clock until all timers are expired +func (m *Mock) WaitForAllTimers() time.Time { + // Continue to execute timers until there are no more + for { + m.mu.Lock() + if len(m.timers) == 0 { + m.mu.Unlock() + return m.Now() + } + + sort.Sort(m.timers) + next := m.timers[len(m.timers)-1].Next() + m.mu.Unlock() + m.Set(next) + } +} + // runNextTimer executes the next timer in chronological order and moves the // current time to the timer's next tick time. The next time is not executed if // its next time is after the max time. Returns true if a timer was executed. @@ -150,10 +172,11 @@ func (m *Mock) runNextTimer(max time.Time) bool { // Move "now" forward and unlock clock. m.now = t.Next() + now := m.now m.mu.Unlock() // Execute timer. - t.Tick(m.now) + t.Tick(now) return true } @@ -162,12 +185,20 @@ func (m *Mock) After(d time.Duration) <-chan time.Time { return m.Timer(d).C } -// AfterFunc waits for the duration to elapse and then executes a function. +// AfterFunc waits for the duration to elapse and then executes a function in its own goroutine. // A Timer is returned that can be stopped. func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer { - t := m.Timer(d) - t.C = nil - t.fn = f + m.mu.Lock() + defer m.mu.Unlock() + ch := make(chan time.Time, 1) + t := &Timer{ + c: ch, + fn: f, + mock: m, + next: m.now.Add(d), + stopped: false, + } + m.timers = append(m.timers, (*internalTimer)(t)) return t } @@ -219,7 +250,6 @@ func (m *Mock) Ticker(d time.Duration) *Ticker { // Timer creates a new instance of Timer. func (m *Mock) Timer(d time.Duration) *Timer { m.mu.Lock() - defer m.mu.Unlock() ch := make(chan time.Time, 1) t := &Timer{ C: ch, @@ -229,9 +259,14 @@ func (m *Mock) Timer(d time.Duration) *Timer { stopped: false, } m.timers = append(m.timers, (*internalTimer)(t)) + now := m.now + m.mu.Unlock() + m.runNextTimer(now) return t } +// removeClockTimer removes a timer from m.timers. m.mu MUST be held +// when this method is called. func (m *Mock) removeClockTimer(t clockTimer) { for i, timer := range m.timers { if timer == t { @@ -313,7 +348,7 @@ func (t *internalTimer) Tick(now time.Time) { t.mock.mu.Lock() if t.fn != nil { // defer function execution until the lock is released, and - defer t.fn() + defer func() { go t.fn() }() } else { t.c <- now } @@ -324,12 +359,13 @@ func (t *internalTimer) Tick(now time.Time) { // Ticker holds a channel that receives "ticks" at regular intervals. type Ticker struct { - C <-chan time.Time - c chan time.Time - ticker *time.Ticker // realtime impl, if set - next time.Time // next tick time - mock *Mock // mock clock, if set - d time.Duration // time between ticks + C <-chan time.Time + c chan time.Time + ticker *time.Ticker // realtime impl, if set + next time.Time // next tick time + mock *Mock // mock clock, if set + d time.Duration // time between ticks + stopped bool // True if stopped, false if running } // Stop turns off the ticker. @@ -339,6 +375,7 @@ func (t *Ticker) Stop() { } else { t.mock.mu.Lock() t.mock.removeClockTimer((*internalTicker)(t)) + t.stopped = true t.mock.mu.Unlock() } } @@ -353,6 +390,11 @@ func (t *Ticker) Reset(dur time.Duration) { t.mock.mu.Lock() defer t.mock.mu.Unlock() + if t.stopped { + t.mock.timers = append(t.mock.timers, (*internalTicker)(t)) + t.stopped = false + } + t.d = dur t.next = t.mock.now.Add(dur) } @@ -365,7 +407,9 @@ func (t *internalTicker) Tick(now time.Time) { case t.c <- now: default: } + t.mock.mu.Lock() t.next = now.Add(t.d) + t.mock.mu.Unlock() gosched() } diff --git a/vendor/modules.txt b/vendor/modules.txt index a5149dfa9..56342ca84 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible ## explicit github.com/Knetic/govaluate -# github.com/benbjohnson/clock v1.3.0 +# github.com/benbjohnson/clock v1.3.5 ## explicit; go 1.15 github.com/benbjohnson/clock # github.com/beorn7/perks v1.0.1