2009-03-06 18:51:31 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-11-17 09:39:17 -07:00
|
|
|
"os";
|
2009-03-06 18:51:31 -07:00
|
|
|
"testing";
|
|
|
|
"time";
|
|
|
|
)
|
|
|
|
|
2009-11-17 09:39:17 -07:00
|
|
|
func testTimeout(t *testing.T, network, addr string, readFrom bool) {
|
2009-08-12 14:19:17 -06:00
|
|
|
fd, err := Dial(network, "", addr);
|
2009-03-06 18:51:31 -07:00
|
|
|
defer fd.Close();
|
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("dial %s %s failed: %v", network, addr, err)
|
2009-03-06 18:51:31 -07:00
|
|
|
}
|
|
|
|
t0 := time.Nanoseconds();
|
|
|
|
fd.SetReadTimeout(1e8); // 100ms
|
|
|
|
var b [100]byte;
|
2009-11-17 09:39:17 -07:00
|
|
|
var n int;
|
|
|
|
var err1 os.Error;
|
|
|
|
if readFrom {
|
|
|
|
n, _, err1 = fd.(PacketConn).ReadFrom(&b)
|
|
|
|
} else {
|
|
|
|
n, err1 = fd.Read(&b)
|
|
|
|
}
|
2009-03-06 18:51:31 -07:00
|
|
|
t1 := time.Nanoseconds();
|
2009-11-17 09:39:17 -07:00
|
|
|
what := "Read";
|
|
|
|
if readFrom {
|
|
|
|
what = "ReadFrom"
|
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if n != 0 || !isEAGAIN(err1) {
|
2009-11-17 09:39:17 -07:00
|
|
|
t.Errorf("fd.%s on %s %s did not return 0, EAGAIN: %v, %v", what, network, addr, n, err1)
|
2009-03-06 18:51:31 -07:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
if t1-t0 < 0.5e8 || t1-t0 > 1.5e8 {
|
2009-11-17 09:39:17 -07:00
|
|
|
t.Errorf("fd.%s on %s %s took %f seconds, expected 0.1", what, network, addr, float64(t1-t0)/1e9)
|
2009-03-06 18:51:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 09:39:17 -07:00
|
|
|
func TestTimeoutUDP(t *testing.T) {
|
|
|
|
testTimeout(t, "udp", "127.0.0.1:53", false);
|
|
|
|
testTimeout(t, "udp", "127.0.0.1:53", true);
|
|
|
|
}
|
2009-03-06 18:51:31 -07:00
|
|
|
|
|
|
|
func TestTimeoutTCP(t *testing.T) {
|
|
|
|
// 74.125.19.99 is www.google.com.
|
|
|
|
// could use dns, but dns depends on
|
|
|
|
// timeouts and this is the timeout test.
|
2009-11-17 09:39:17 -07:00
|
|
|
testTimeout(t, "tcp", "74.125.19.99:80", false)
|
2009-03-06 18:51:31 -07:00
|
|
|
}
|