Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix(decision): fix flaky TestTaggingUseful test
Browse files Browse the repository at this point in the history
rather than use a ticker, use an actual timer that only resets after
sampling is completed. This doesn't affect behavior in real world
operations significantly but makes the tests much more reliable
  • Loading branch information
hannahhoward committed Jun 3, 2021
1 parent 0b39c44 commit 219f8c3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/decision/scoreledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ type DefaultScoreLedger struct {
// adjust it ±25% based on our debt ratio. Peers that have historically been
// more useful to us than we are to them get the highest score.
func (dsl *DefaultScoreLedger) scoreWorker() {
ticker := time.NewTicker(dsl.peerSampleInterval)
defer ticker.Stop()
timer := time.NewTimer(dsl.peerSampleInterval)

type update struct {
peer peer.ID
Expand All @@ -149,8 +148,11 @@ func (dsl *DefaultScoreLedger) scoreWorker() {
for i := 0; ; i = (i + 1) % longTermRatio {
var now time.Time
select {
case now = <-ticker.C:
case now = <-timer.C:
case <-dsl.closing:
if !timer.Stop() {
<-timer.C
}
return
}

Expand Down Expand Up @@ -218,6 +220,7 @@ func (dsl *DefaultScoreLedger) scoreWorker() {
if dsl.sampleCh != nil {
dsl.sampleCh <- struct{}{}
}
timer.Reset(dsl.peerSampleInterval)
}
}

Expand Down

0 comments on commit 219f8c3

Please sign in to comment.