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 8eb1499
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/decision/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ func TestTaggingPeers(t *testing.T) {
}

func TestTaggingUseful(t *testing.T) {
peerSampleInterval := 1 * time.Millisecond
peerSampleInterval := 20 * time.Millisecond

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
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 8eb1499

Please sign in to comment.