diff options
| author | Mikio Hara <mikioh.mikioh@gmail.com> | 2015-03-29 20:19:20 +0900 |
|---|---|---|
| committer | Mikio Hara <mikioh.mikioh@gmail.com> | 2015-04-02 01:04:49 +0000 |
| commit | 8b0e38ffb44cd9a000db38510925a2cada074e26 (patch) | |
| tree | 3d079d1a9953f3691298cd1f02b67f53b4d6c942 /src/net/timeout_test.go | |
| parent | fb4b46738cfc0adf4e02781c25603ca16764cdbf (diff) | |
| download | go-8b0e38ffb44cd9a000db38510925a2cada074e26.tar.xz | |
net: deflake TestDialTimeout
This change makes TestDialTimeout work on almost all the supported
platforms.
Updates #3016.
Updates #3307.
Updates #3867.
Updates #5380.
Updates #5349.
Change-Id: Iacf0ebea23cdd8f6c0333d70c667a5a5f5eb0ed2
Reviewed-on: https://go-review.googlesource.com/8220
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/net/timeout_test.go')
| -rw-r--r-- | src/net/timeout_test.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/net/timeout_test.go b/src/net/timeout_test.go index 9ef0c4d15c..b46321b13b 100644 --- a/src/net/timeout_test.go +++ b/src/net/timeout_test.go @@ -8,11 +8,59 @@ import ( "fmt" "io" "io/ioutil" + "net/internal/socktest" "runtime" "testing" "time" ) +func TestDialTimeout(t *testing.T) { + const T = 100 * time.Millisecond + + switch runtime.GOOS { + case "plan9", "windows": + origTestHookDialChannel := testHookDialChannel + testHookDialChannel = func() { time.Sleep(2 * T) } + defer func() { testHookDialChannel = origTestHookDialChannel }() + if runtime.GOOS == "plan9" { + break + } + fallthrough + default: + sw.Set(socktest.FilterConnect, func(so *socktest.Status) (socktest.AfterFilter, error) { + time.Sleep(2 * T) + return nil, errTimeout + }) + defer sw.Set(socktest.FilterConnect, nil) + } + + ch := make(chan error) + go func() { + // This dial never starts to send any SYN segment + // because of above socket filter and test hook. + c, err := DialTimeout("tcp", "127.0.0.1:0", T) + if err == nil { + err = fmt.Errorf("unexpectedly established: tcp:%s->%s", c.LocalAddr(), c.RemoteAddr()) + c.Close() + } + ch <- err + }() + tmo := time.NewTimer(3 * T) + defer tmo.Stop() + select { + case <-tmo.C: + t.Fatal("dial has not returned") + case err := <-ch: + nerr, ok := err.(Error) + if !ok { + t.Fatalf("got %v; want error implements Error interface", err) + } + if !nerr.Timeout() { + t.Fatalf("got %v; want timeout error", err) + } + } +} + func isTimeout(err error) bool { e, ok := err.(Error) return ok && e.Timeout() |
