-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinfo_test.go
86 lines (78 loc) · 1.62 KB
/
info_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tcp_test
import (
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"runtime"
"testing"
"time"
"github.com/mikioh/tcp"
)
var (
host = "www.google.com"
url = "https://www.google.com/robots.txt"
tt *testing.T
)
func TestInfoWithGoogle(t *testing.T) {
switch runtime.GOOS {
case "freebsd", "linux":
default:
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
tt = t
tr := &http.Transport{
Dial: dialWithTCPConnMonitor,
TLSClientConfig: &tls.Config{ServerName: host},
}
client := http.Client{Transport: tr}
resp, err := client.Get(url)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
}
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
}
func tcpConnMonitor(c *tcp.Conn) {
tt.Logf("%v -> %v", c.LocalAddr(), c.RemoteAddr())
for {
ti, err := c.Info()
if err != nil {
if runtime.GOOS == "linux" && runtime.GOARCH == "386" {
tt.Log(err)
} else {
tt.Error(err)
}
return
}
text, err := json.Marshal(ti)
if err != nil {
tt.Error(err)
return
}
tt.Log(string(text))
time.Sleep(5 * time.Millisecond)
}
}