logical: remove IncrementedLease, simplify ExpirationTime calculation

This commit is contained in:
Armon Dadgar 2015-06-17 13:59:09 -07:00
parent 30de4ea80d
commit ae02203624
2 changed files with 10 additions and 38 deletions

View File

@ -46,22 +46,8 @@ func (l *LeaseOptions) LeaseTotal() time.Duration {
// ExpirationTime computes the time until expiration including the grace period
func (l *LeaseOptions) ExpirationTime() time.Time {
var expireTime time.Time
if !l.LeaseIssue.IsZero() && l.Lease > 0 {
expireTime = l.LeaseIssue.UTC().Add(l.LeaseTotal())
if l.LeaseEnabled() {
expireTime = time.Now().UTC().Add(l.LeaseTotal())
}
return expireTime
}
// IncrementedLease returns the lease duration that would need to set
// in order to increment the _current_ lease by the given duration
// if the auth were re-issued right now.
func (l *LeaseOptions) IncrementedLease(inc time.Duration) time.Duration {
var result time.Duration
expireTime := l.ExpirationTime()
if expireTime.IsZero() {
return result
}
return expireTime.Add(inc).Sub(time.Now().UTC())
}

View File

@ -5,17 +5,6 @@ import (
"time"
)
func TestLeaseOptionsIncrementedLease(t *testing.T) {
var l LeaseOptions
l.Lease = 1 * time.Second
l.LeaseIssue = time.Now().UTC()
actual := l.IncrementedLease(1 * time.Second)
if actual > 3*time.Second || actual < 1*time.Second {
t.Fatalf("bad: %s", actual)
}
}
func TestLeaseOptionsLeaseTotal(t *testing.T) {
var l LeaseOptions
l.Lease = 1 * time.Hour
@ -66,12 +55,11 @@ func TestLeaseOptionsLeaseTotal_negGrace(t *testing.T) {
func TestLeaseOptionsExpirationTime(t *testing.T) {
var l LeaseOptions
l.Lease = 1 * time.Hour
l.LeaseIssue = time.Now().UTC()
actual := l.ExpirationTime()
expected := l.LeaseIssue.Add(l.Lease)
if !actual.Equal(expected) {
t.Fatalf("bad: %s", actual)
limit := time.Now().UTC().Add(time.Hour)
exp := l.ExpirationTime()
if exp.Before(limit) {
t.Fatalf("bad: %s", exp)
}
}
@ -79,11 +67,10 @@ func TestLeaseOptionsExpirationTime_grace(t *testing.T) {
var l LeaseOptions
l.Lease = 1 * time.Hour
l.LeaseGracePeriod = 30 * time.Minute
l.LeaseIssue = time.Now().UTC()
limit := time.Now().UTC().Add(time.Hour + 30*time.Minute)
actual := l.ExpirationTime()
expected := l.LeaseIssue.Add(l.Lease + l.LeaseGracePeriod)
if !actual.Equal(expected) {
if actual.Before(limit) {
t.Fatalf("bad: %s", actual)
}
}
@ -92,11 +79,10 @@ func TestLeaseOptionsExpirationTime_graceNegative(t *testing.T) {
var l LeaseOptions
l.Lease = 1 * time.Hour
l.LeaseGracePeriod = -1 * 30 * time.Minute
l.LeaseIssue = time.Now().UTC()
limit := time.Now().UTC().Add(time.Hour)
actual := l.ExpirationTime()
expected := l.LeaseIssue.Add(l.Lease)
if !actual.Equal(expected) {
if actual.Before(limit) {
t.Fatalf("bad: %s", actual)
}
}