Skip to content

Commit

Permalink
internal/poller: try test for longer and longer intervals
Browse files Browse the repository at this point in the history
if the first interval doesn't work. This is an attempt to reduce the
flakiness in the test when the timing isn't right.

For golagn/go#61269

Change-Id: I34860135a9802745ab3d73462dbea841964dfa78
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/524800
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
kokoro-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
matloob committed Sep 1, 2023
1 parent 12a45f8 commit 68be0dd
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions internal/poller/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package poller

import (
"context"
"fmt"
"strconv"
"sync"
"testing"
Expand All @@ -19,6 +20,21 @@ type numError struct {
func (e numError) Error() string { return strconv.Itoa(e.num) }

func Test(t *testing.T) {
var err error
// Try the test with longer and longer durations to try to find one that works.
// If not, return an error.
for durationUnit := 10 * time.Millisecond; durationUnit < time.Second; durationUnit *= 2 {
err = doTest(durationUnit)
if err == nil {
break
}
}
if err != nil {
t.Error(err)
}
}

func doTest(durationUnit time.Duration) error {
var (
mu sync.Mutex
goods, bads []int
Expand All @@ -42,20 +58,20 @@ func Test(t *testing.T) {

p := New(cur, getter, onError)
if got, want := p.Current(), cur; got != want {
t.Fatalf("got %v, want %v", got, want)
return fmt.Errorf("got %v, want %v", got, want)
}
ctx, cancel := context.WithCancel(context.Background())
p.Start(ctx, 50*time.Millisecond)
time.Sleep(100 * time.Millisecond) // wait for first poll
p.Start(ctx, 5*durationUnit)
time.Sleep(10 * durationUnit) // wait for first poll
for i := 0; i < 10; i++ {
goods = append(goods, p.Current().(int))
time.Sleep(60 * time.Millisecond)
time.Sleep(6 * durationUnit)
}
cancel()
// Expect goods to be all even and non-decreasing.
for i, g := range goods {
if g%2 != 0 || (i > 0 && goods[i-1] > g) {
t.Errorf("incorrect 'good' value %d", g)
return fmt.Errorf("incorrect 'good' value %d", g)
}
}
// Expect bads to be consecutive odd numbers.
Expand All @@ -64,7 +80,8 @@ func Test(t *testing.T) {
mu.Unlock()
for i, b := range bs {
if b%2 == 0 || (i > 0 && bs[i-1]+2 != b) {
t.Errorf("incorrect 'bad' value %d", b)
return fmt.Errorf("incorrect 'bad' value %d", b)
}
}
return nil
}

0 comments on commit 68be0dd

Please sign in to comment.