OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [dial_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package net
6
 
7
import (
8
        "runtime"
9
        "testing"
10
        "time"
11
)
12
 
13
func newLocalListener(t *testing.T) Listener {
14
        ln, err := Listen("tcp", "127.0.0.1:0")
15
        if err != nil {
16
                ln, err = Listen("tcp6", "[::1]:0")
17
        }
18
        if err != nil {
19
                t.Fatal(err)
20
        }
21
        return ln
22
}
23
 
24
func TestDialTimeout(t *testing.T) {
25
        ln := newLocalListener(t)
26
        defer ln.Close()
27
 
28
        errc := make(chan error)
29
 
30
        const SOMAXCONN = 0x80 // copied from syscall, but not always available
31
        const numConns = SOMAXCONN + 10
32
 
33
        // TODO(bradfitz): It's hard to test this in a portable
34
        // way. This is unforunate, but works for now.
35
        switch runtime.GOOS {
36
        case "linux":
37
                // The kernel will start accepting TCP connections before userspace
38
                // gets a chance to not accept them, so fire off a bunch to fill up
39
                // the kernel's backlog.  Then we test we get a failure after that.
40
                for i := 0; i < numConns; i++ {
41
                        go func() {
42
                                _, err := DialTimeout("tcp", ln.Addr().String(), 200*time.Millisecond)
43
                                errc <- err
44
                        }()
45
                }
46
        case "darwin", "windows":
47
                // At least OS X 10.7 seems to accept any number of
48
                // connections, ignoring listen's backlog, so resort
49
                // to connecting to a hopefully-dead 127/8 address.
50
                // Same for windows.
51
                go func() {
52
                        _, err := DialTimeout("tcp", "127.0.71.111:80", 200*time.Millisecond)
53
                        errc <- err
54
                }()
55
        default:
56
                // TODO(bradfitz):
57
                // OpenBSD may have a reject route to 10/8.
58
                // FreeBSD likely works, but is untested.
59
                t.Logf("skipping test on %q; untested.", runtime.GOOS)
60
                return
61
        }
62
 
63
        connected := 0
64
        for {
65
                select {
66
                case <-time.After(15 * time.Second):
67
                        t.Fatal("too slow")
68
                case err := <-errc:
69
                        if err == nil {
70
                                connected++
71
                                if connected == numConns {
72
                                        t.Fatal("all connections connected; expected some to time out")
73
                                }
74
                        } else {
75
                                terr, ok := err.(timeout)
76
                                if !ok {
77
                                        t.Fatalf("got error %q; want error with timeout interface", err)
78
                                }
79
                                if !terr.Timeout() {
80
                                        t.Fatalf("got error %q; not a timeout", err)
81
                                }
82
                                // Pass. We saw a timeout error.
83
                                return
84
                        }
85
                }
86
        }
87
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.