From 1e0aa9a49456c8c05cb80e099cf3d7cf5cc21b93 Mon Sep 17 00:00:00 2001 From: Yutaka Takeda Date: Fri, 24 May 2019 17:59:21 -0700 Subject: [PATCH] Fix the test incorrectly failing Resolves #49 --- transport_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/transport_test.go b/transport_test.go index 4759f7b..27f40a8 100644 --- a/transport_test.go +++ b/transport_test.go @@ -23,15 +23,18 @@ func TestStressDuplex(t *testing.T) { func testTimeout(t *testing.T, c *Conn, timeout time.Duration) { const pollrate = 100 * time.Millisecond + const margin = 20 * time.Millisecond // allow 20msec error in time statechan := make(chan ConnectionState) ticker := time.NewTicker(pollrate) + startedAt := time.Now() + for cnt := time.Duration(0); cnt <= timeout+defaultTaskLoopInterval; cnt += pollrate { <-ticker.C + err := c.agent.run(func(agent *Agent) { statechan <- agent.connectionState }) - if err != nil { // we should never get here. panic(err) @@ -39,9 +42,11 @@ func testTimeout(t *testing.T, c *Conn, timeout time.Duration) { cs := <-statechan if cs != ConnectionStateConnected { - if cnt < timeout { - t.Fatalf("Connection timed out early. (after %d ms)", cnt/time.Millisecond) + elapsed := time.Since(startedAt) + if elapsed+margin < timeout { + t.Fatalf("Connection timed out %f msec early", elapsed.Seconds()*1000) } else { + t.Logf("Connection timed out in %f msec", elapsed.Seconds()*1000) return } }