Skip to content

Commit

Permalink
Merge pull request #185 from gustavosbarreto/first_poll_after_now
Browse files Browse the repository at this point in the history
Prevent first poll timestamp with a timestamp in the future
  • Loading branch information
otavio committed Jan 18, 2018
2 parents 4bd007d + 881f144 commit ec581e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions updatehub/updatehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ func (uh *UpdateHub) Start() {

timeZero := (time.Time{}).UTC()

if uh.Settings.FirstPoll.After(now) && uh.Settings.LastPoll != timeZero {
uh.Settings.FirstPoll = timeZero
}

if uh.Settings.FirstPoll == timeZero {
// Apply an offset in first poll
uh.Settings.FirstPoll = now.Add(time.Duration(rand.Int63n(int64(uh.Settings.PollingInterval))))
Expand Down
15 changes: 15 additions & 0 deletions updatehub/updatehub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,21 @@ func TestStart(t *testing.T) {
false,
},

{
"FirstPollAfterNow",
time.Second,
0,
now.Add(24 * time.Second),
now.Add(-1 * time.Second),
&PollState{},
func(t *testing.T, uh *UpdateHub, state State) {
poll := state.(*PollState)
assert.WithinDuration(t, uh.Settings.FirstPoll, now, uh.Settings.PollingInterval)
assert.Condition(t, func() bool { return poll.ticksCount >= 0 })
},
false,
},

{
"PendingRegularPoll",
time.Second,
Expand Down

0 comments on commit ec581e0

Please sign in to comment.