Alerting: Fix test fails in some environments (#58251)

This commit is contained in:
George Robinson 2022-11-07 16:34:37 +00:00 committed by GitHub
parent 480277f612
commit 8353f307aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View File

@ -27,7 +27,7 @@ func (i *Image) ExtendDuration(d time.Duration) {
// HasExpired returns true if the image has expired.
func (i *Image) HasExpired() bool {
return time.Now().After(i.ExpiresAt)
return timeNow().After(i.ExpiresAt)
}
// HasPath returns true if the image has a path on disk.

View File

@ -4,6 +4,7 @@ import (
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/stretchr/testify/assert"
)
@ -20,12 +21,18 @@ func TestImage_ExtendDuration(t *testing.T) {
}
func TestImage_HasExpired(t *testing.T) {
oldTimeNow := timeNow
timeNow = clock.NewMock().Now
t.Cleanup(func() {
timeNow = oldTimeNow
})
var i Image
i.ExpiresAt = time.Now().Add(time.Minute)
i.ExpiresAt = timeNow().Add(time.Minute)
assert.False(t, i.HasExpired())
i.ExpiresAt = time.Now()
assert.True(t, i.HasExpired())
i.ExpiresAt = time.Now().Add(-time.Minute)
i.ExpiresAt = timeNow()
assert.False(t, i.HasExpired())
i.ExpiresAt = timeNow().Add(-time.Minute)
assert.True(t, i.HasExpired())
}

View File

@ -0,0 +1,8 @@
package models
import "time"
var (
// timeNow is an equivalent time.Now() that can be replaced in tests
timeNow = time.Now
)