Skip to content

Commit

Permalink
avoid using duration.Seconds()
Browse files Browse the repository at this point in the history
  • Loading branch information
uniquefine committed Jul 21, 2023
1 parent d4be115 commit 8986359
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion control/beaconing/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *DefaultExtender) Extend(
expTime := s.MaxExpTime()
if ts.Add(path.ExpTimeToDuration(expTime)).After(signExp) {
var err error
expTime, err = path.ExpTimeFromDuration(signExp.Sub(ts))
expTime, err = path.ExpTimeFromSeconds(signExp.Sub(ts).Seconds())
if err != nil {
return serrors.WrapStr(
"calculating expiry time from signer expiration time", err,
Expand Down
12 changes: 6 additions & 6 deletions pkg/slayers/path/hopfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func ExpTimeToDuration(expTime uint8) time.Duration {
// fits into the provided duration. The returned value is the ExpTime that
// can be used in a HopField. The returned value is guaranteed to be >= 1.
// For durations that are out of range, an error is returned.
func ExpTimeFromDuration(d time.Duration) (uint8, error) {
if d.Seconds() < expTimeUnit {
return 0, serrors.New("duration too small", "duration", d)
func ExpTimeFromSeconds(seconds float64) (uint8, error) {
if seconds < expTimeUnit {
return 0, serrors.New("duration too small", "seconds", seconds)
}
if d.Seconds() > MaxTTL {
return 0, serrors.New("duration too large", "duration", d)
if seconds > MaxTTL {
return 0, serrors.New("duration too large", "seconds", seconds)
}
return uint8(d.Seconds()*256/MaxTTL - 1), nil
return uint8(seconds*256/MaxTTL - 1), nil
}
45 changes: 22 additions & 23 deletions pkg/slayers/path/hopfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package path_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -42,44 +41,44 @@ func TestHopSerializeDecode(t *testing.T) {

func TestExpTimeFromDuration(t *testing.T) {
tests := map[string]struct {
Duration time.Duration
ExpTime uint8
ErrorF assert.ErrorAssertionFunc
Seconds float64
ExpTime uint8
ErrorF assert.ErrorAssertionFunc
}{
"Zero": {
Duration: 0,
ExpTime: 0,
ErrorF: assert.Error,
Seconds: 0,
ExpTime: 0,
ErrorF: assert.Error,
},
"Max": {
Duration: path.MaxTTL * time.Second,
ExpTime: 255,
ErrorF: assert.NoError,
Seconds: path.MaxTTL,
ExpTime: 255,
ErrorF: assert.NoError,
},
"Overflow": {
Duration: (path.MaxTTL + 1) * time.Second,
ExpTime: 0,
ErrorF: assert.Error,
Seconds: (path.MaxTTL + 1),
ExpTime: 0,
ErrorF: assert.Error,
},
"Underflow": {
Duration: -1 * time.Second,
ExpTime: 0,
ErrorF: assert.Error,
Seconds: -1,
ExpTime: 0,
ErrorF: assert.Error,
},
"Max-1": {
Duration: (path.MaxTTL - 1) * time.Second,
ExpTime: 254,
ErrorF: assert.NoError,
Seconds: (path.MaxTTL - 1),
ExpTime: 254,
ErrorF: assert.NoError,
},
"Half": {
Duration: (path.MaxTTL / 2) * time.Second,
ExpTime: 127,
ErrorF: assert.NoError,
Seconds: (path.MaxTTL / 2),
ExpTime: 127,
ErrorF: assert.NoError,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
expTime, err := path.ExpTimeFromDuration(test.Duration)
expTime, err := path.ExpTimeFromSeconds(test.Seconds)
test.ErrorF(t, err)
assert.Equal(t, test.ExpTime, expTime)
})
Expand Down

0 comments on commit 8986359

Please sign in to comment.