Esempio n. 1
0
func TestReadBuffer(t *testing.T) {
	switch runtime.GOOS {
	case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd":
	default:
		t.Skipf("%s/%s", runtime.GOOS, runtime.GOARCH)
	}

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer ln.Close()

	m := []byte("HELLO-R-U-THERE")
	var wg sync.WaitGroup
	wg.Add(1)

	go func() {
		defer wg.Done()
		if err := ln.(*net.TCPListener).SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
			t.Error(err)
			return
		}
		c, err := ln.Accept()
		if err != nil {
			t.Error(err)
			return
		}
		defer c.Close()
		if err := c.(*net.TCPConn).SetReadBuffer(65535); err != nil {
			t.Error(err)
			return
		}
		tc, err := tcp.NewConn(c)
		if err != nil {
			t.Error(err)
			return
		}
		time.Sleep(100 * time.Millisecond)
		n := tc.ReadBufferLen()
		if n != len(m) {
			t.Errorf("got %v; want %v", n, len(m))
			return
		}
		t.Logf("%v bytes buffered to be read", n)
	}()

	c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()
	if _, err := c.Write(m); err != nil {
		t.Fatal(err)
	}

	wg.Wait()
}
Esempio n. 2
0
func TestWriteBuffer(t *testing.T) {
	switch runtime.GOOS {
	case "freebsd", "linux", "netbsd":
	default:
		t.Skipf("%s/%s", runtime.GOOS, runtime.GOARCH)
	}

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer ln.Close()

	m := []byte("HELLO-R-U-THERE")
	var wg sync.WaitGroup
	wg.Add(1)

	go func() {
		defer wg.Done()
		d := net.Dialer{Timeout: 200 * time.Millisecond}
		c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
		if err != nil {
			t.Error(err)
			return
		}
		defer c.Close()
		if err := c.(*net.TCPConn).SetWriteBuffer(65535); err != nil {
			t.Error(err)
			return
		}
		tc, err := tcp.NewConn(c)
		if err != nil {
			t.Error(err)
			return
		}
		defer tc.Close()
		if _, err := c.Write(m); err != nil {
			t.Error(err)
			return
		}
		n := tc.WriteBufferSpace()
		if n <= 0 {
			t.Errorf("got %v; want >0", n)
			return
		}
		t.Logf("%v bytes write available", n)
	}()

	c, err := ln.Accept()
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()

	wg.Wait()
}
Esempio n. 3
0
func dialWithTCPConnMonitor(network, address string) (net.Conn, error) {
	d := net.Dialer{DualStack: true}
	c, err := d.Dial(network, address)
	if err != nil {
		return nil, err
	}
	tc, err := tcp.NewConn(c)
	if err != nil {
		c.Close()
		return nil, err
	}
	go tcpConnMonitor(tc)
	return &tc.TCPConn, nil
}
Esempio n. 4
0
func TestKeepAlive(t *testing.T) {
	switch runtime.GOOS {
	case "darwin", "dragonfly", "freebsd", "linux", "netbsd", "solaris", "windows":
	default:
		t.Skipf("%s/%s", runtime.GOOS, runtime.GOARCH)
	}

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer ln.Close()

	go func() {
		for {
			c, err := ln.Accept()
			if err != nil {
				break
			}
			defer c.Close()
		}
	}()

	c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()
	tc, err := tcp.NewConn(c)
	if err != nil {
		t.Fatal(err)
	}
	defer tc.Close()
	if err := tc.SetKeepAlive(true); err != nil {
		t.Error(err)
	}
	if err := tc.SetKeepAliveIdleInterval(10 * time.Second); err != nil { // solaris requires 10 seconds as the lowest value
		t.Error(err)
	}
	if err := tc.SetKeepAliveProbeInterval(time.Second); err != nil {
		t.Error(err)
	}
	if err := tc.SetKeepAliveProbes(1); err != nil {
		t.Error(err)
	}
}
Esempio n. 5
0
func TestCorkAndUncork(t *testing.T) {
	switch runtime.GOOS {
	case "freebsd", "linux":
	default:
		t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
	}

	const N = 1280
	const M = N / 10

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	defer ln.Close()

	var wg sync.WaitGroup
	wg.Add(1)

	go func() {
		defer wg.Done()
		if err := ln.(*net.TCPListener).SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
			t.Error(err)
			return
		}
		c, err := ln.Accept()
		if err != nil {
			t.Error(err)
			return
		}
		defer c.Close()
		b := make([]byte, N)
		n, err := c.Read(b)
		if err != nil {
			t.Error(err)
			return
		}
		if n != N {
			t.Errorf("got %v; want %v", n, N)
			return
		}
	}()

	c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()
	tc, err := tcp.NewConn(c)
	if err != nil {
		t.Fatal(err)
	}
	defer tc.Close()
	if err := tc.Cork(); err != nil {
		t.Fatal(err)
	}
	b := make([]byte, N)
	for i := 0; i+M <= N; i += M {
		if _, err := tc.Write(b[i : i+M]); err != nil {
			t.Fatal(err)
		}
		time.Sleep(5 * time.Millisecond)
	}
	if err := tc.Uncork(); err != nil {
		t.Fatal(err)
	}

	wg.Wait()
}