TestPing needs a different timer resolution on windows. Fixes #88.

This commit is contained in:
Alex Bramley 2016-06-10 19:37:51 +01:00
parent aad5dbe821
commit 20e4ea9b00
1 changed files with 27 additions and 15 deletions

View File

@ -1,11 +1,13 @@
package client package client
import ( import (
"github.com/fluffle/goirc/state" "runtime"
"github.com/golang/mock/gomock"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/fluffle/goirc/state"
"github.com/golang/mock/gomock"
) )
type checker struct { type checker struct {
@ -377,13 +379,23 @@ func TestPing(t *testing.T) {
c, s := setUp(t, false) c, s := setUp(t, false)
defer s.tearDown() defer s.tearDown()
res := time.Millisecond
// Windows has a timer resolution of 15.625ms by default.
// This means the test will be slower on windows, but
// should at least stop most of the flakiness...
// https://github.com/fluffle/goirc/issues/88
if runtime.GOOS == "windows" {
res = 15625 * time.Microsecond
}
// Set a low ping frequency for testing. // Set a low ping frequency for testing.
c.cfg.PingFreq = 50 * time.Millisecond c.cfg.PingFreq = 10 * res
// reader is a helper to do a "non-blocking" read of c.out // reader is a helper to do a "non-blocking" read of c.out
reader := func() string { reader := func() string {
select { select {
case <-time.After(time.Millisecond): case <-time.After(res):
case s := <-c.out: case s := <-c.out:
return s return s
} }
@ -402,28 +414,28 @@ func TestPing(t *testing.T) {
exited.call() exited.call()
}() }()
// The first ping should be after 50ms, // The first ping should be after 10*res ms,
// so we don't expect anything now on c.in // so we don't expect anything now on c.in
if s := reader(); s != "" { if s := reader(); s != "" {
t.Errorf("Line output directly after ping started.") t.Errorf("Line output directly after ping started.")
} }
<-time.After(50 * time.Millisecond) <-time.After(c.cfg.PingFreq)
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") { if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
t.Errorf("Line not output after 50ms.") t.Errorf("Line not output after %s.", c.cfg.PingFreq)
} }
// Reader waits for 1ms and we call it a few times above. // Reader waits for res ms and we call it a few times above.
<-time.After(45 * time.Millisecond) <-time.After(7 * res)
if s := reader(); s != "" { if s := reader(); s != "" {
t.Errorf("Line output under 50ms after last ping.") t.Errorf("Line output <%s after last ping.", 7*res)
} }
// This is a short window (49-51ms) in which the ping should happen // This is a short window in which the ping should happen
// This may result in flaky tests; sorry (and file a bug) if so. // This may result in flaky tests; sorry (and file a bug) if so.
<-time.After(2 * time.Millisecond) <-time.After(2 * res)
if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") { if s := reader(); s == "" || !strings.HasPrefix(s, "PING :") {
t.Errorf("Line not output after another 2ms.") t.Errorf("Line not output after another %s.", 2*res)
} }
// Now kill the ping loop. // Now kill the ping loop.
@ -434,8 +446,8 @@ func TestPing(t *testing.T) {
exited.assertNotCalled("Exited before signal sent.") exited.assertNotCalled("Exited before signal sent.")
c.die <- struct{}{} c.die <- struct{}{}
exited.assertWasCalled("Didn't exit after signal.") exited.assertWasCalled("Didn't exit after signal.")
// Make sure we're no longer pinging by waiting ~2x PingFreq // Make sure we're no longer pinging by waiting >2x PingFreq
<-time.After(105 * time.Millisecond) <-time.After(2*c.cfg.PingFreq + res)
if s := reader(); s != "" { if s := reader(); s != "" {
t.Errorf("Line output after ping stopped.") t.Errorf("Line output after ping stopped.")
} }