mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-24 09:39:39 +00:00
net/tcpinfo: add package to allow fetching TCP information
This package contains platform-independent abstractions for fetching information about an open TCP connection. Updates #8413 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I236657b1060d7e6a45efc7a2f6aacf474547a2fe
This commit is contained in:
64
net/tcpinfo/tcpinfo_test.go
Normal file
64
net/tcpinfo/tcpinfo_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package tcpinfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRTT(t *testing.T) {
|
||||
switch runtime.GOOS {
|
||||
case "linux", "darwin":
|
||||
default:
|
||||
t.Skipf("not currently supported on %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp4", "localhost:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
c, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
t.Cleanup(func() { c.Close() })
|
||||
|
||||
// Copy from the client to nowhere
|
||||
go io.Copy(io.Discard, c)
|
||||
}
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("tcp4", ln.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write a bunch of data to the conn to force TCP session establishment
|
||||
// and a few packets.
|
||||
junkData := bytes.Repeat([]byte("hello world\n"), 1024*1024)
|
||||
for i := 0; i < 10; i++ {
|
||||
if _, err := conn.Write(junkData); err != nil {
|
||||
t.Fatalf("error writing junk data [%d]: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Get the RTT now
|
||||
rtt, err := RTT(conn)
|
||||
if err != nil {
|
||||
t.Fatalf("error getting RTT: %v", err)
|
||||
}
|
||||
if rtt == 0 {
|
||||
t.Errorf("expected RTT > 0")
|
||||
}
|
||||
|
||||
t.Logf("TCP rtt: %v", rtt)
|
||||
}
|
||||
Reference in New Issue
Block a user