Skip to content

Commit

Permalink
simplify TimeClock
Browse files Browse the repository at this point in the history
  • Loading branch information
skandragon committed Feb 7, 2022
1 parent 5325e69 commit 2188a64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
10 changes: 2 additions & 8 deletions timeclock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ import "time"

// TimeClock implements the jwt.Clock interface, allowing control over
// the interpretation of 'current time' used during validating and signing.
// If NowTime is let unset (0), time.Now() return value will be used.
// Unix time (in seconds).
//
// This is included to help test expiration and use before inception.
type TimeClock struct {
NowTime int64
}

// Now returns either the specific time used at context creation, or
// time.Now().
// Now returns the point in time stored in NowTime.
func (tc *TimeClock) Now() time.Time {
if tc.NowTime != 0 {
return time.Unix(tc.NowTime, 0)
}
return time.Now()
return time.Unix(tc.NowTime, 0)
}
12 changes: 4 additions & 8 deletions timeclock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ package jwtregistry
import (
"testing"
"time"
)

func timeEqualsEpislion(want time.Time, got time.Time, fudge time.Duration) bool {
return got.Unix() >= want.Add(-fudge).Unix() && got.Unix() <= want.Add(fudge).Unix()
}
"github.com/stretchr/testify/assert"
)

func TestTimeClock_Now(t *testing.T) {
type fields struct {
Expand All @@ -37,7 +35,7 @@ func TestTimeClock_Now(t *testing.T) {
{
"default",
fields{},
time.Now(),
time.Unix(0, 0),
},
{
"locked clock to 50",
Expand All @@ -51,9 +49,7 @@ func TestTimeClock_Now(t *testing.T) {
NowTime: tt.fields.NowTime,
}
got := tc.Now()
if !timeEqualsEpislion(tt.want, got, 1*time.Second) {
t.Errorf("TimeClock.Now() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 2188a64

Please sign in to comment.