]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/net/timeout_test.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / net / timeout_test.go
1 // Copyright 2009 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 "os"
9 "testing"
10 "time"
11 "runtime"
12 )
13
14 func testTimeout(t *testing.T, network, addr string, readFrom bool) {
15 // Timeouts are not implemented on windows.
16 if runtime.GOOS == "windows" {
17 return
18 }
19 fd, err := Dial(network, "", addr)
20 if err != nil {
21 t.Errorf("dial %s %s failed: %v", network, addr, err)
22 return
23 }
24 defer fd.Close()
25 t0 := time.Nanoseconds()
26 fd.SetReadTimeout(1e8) // 100ms
27 var b [100]byte
28 var n int
29 var err1 os.Error
30 if readFrom {
31 n, _, err1 = fd.(PacketConn).ReadFrom(b[0:])
32 } else {
33 n, err1 = fd.Read(b[0:])
34 }
35 t1 := time.Nanoseconds()
36 what := "Read"
37 if readFrom {
38 what = "ReadFrom"
39 }
40 if n != 0 || err1 == nil || !err1.(Error).Timeout() {
41 t.Errorf("fd.%s on %s %s did not return 0, timeout: %v, %v", what, network, addr, n, err1)
42 }
43 if t1-t0 < 0.5e8 || t1-t0 > 1.5e8 {
44 t.Errorf("fd.%s on %s %s took %f seconds, expected 0.1", what, network, addr, float64(t1-t0)/1e9)
45 }
46 }
47
48 func TestTimeoutUDP(t *testing.T) {
49 testTimeout(t, "udp", "127.0.0.1:53", false)
50 testTimeout(t, "udp", "127.0.0.1:53", true)
51 }
52
53 func TestTimeoutTCP(t *testing.T) {
54 // 74.125.19.99 is www.google.com.
55 // could use dns, but dns depends on
56 // timeouts and this is the timeout test.
57 testTimeout(t, "tcp", "74.125.19.99:80", false)
58 }