-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathconn_test.go
73 lines (66 loc) · 1.42 KB
/
conn_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
package http
import (
"testing"
)
var _ Conn = new(conn)
var _ Dialer = new(dialer)
type countingDialer struct {
Dialer
count int
}
func (c *countingDialer) Dial(nw, addr string) (Conn, error) {
c.count++
return c.Dialer.Dial(nw, addr)
}
var dialCountTests = []struct {
f func(*testing.T, *server, Dialer)
expected int // expected dial counts
}{
{func(t *testing.T, s *server, d Dialer) {
conn, err := d.Dial("tcp", s.Addr().String())
if err != nil {
t.Fatal(err)
}
conn.Close()
}, 1},
{func(t *testing.T, s *server, d Dialer) {
conn, err := d.Dial("tcp", s.Addr().String())
if err != nil {
t.Fatal(err)
}
conn.Close()
conn, err = d.Dial("tcp", s.Addr().String())
if err != nil {
t.Fatal(err)
}
conn.Close()
}, 2},
{func(t *testing.T, s *server, d Dialer) {
conn, err := d.Dial("tcp", s.Addr().String())
if err != nil {
t.Fatal(err)
}
c1 := conn
conn.Release()
conn, err = d.Dial("tcp", s.Addr().String())
if err != nil {
t.Fatal(err)
}
c2 := conn
if c1 != c2 {
t.Errorf("expected %v == %v", c1, c2)
}
conn.Release()
}, 2}, // should be 1
}
func TestDialCounts(t *testing.T) {
s := newServer(t, stdmux())
defer s.Shutdown()
for i, tt := range dialCountTests {
d := countingDialer{Dialer: new(dialer)}
tt.f(t, s, &d)
if actual := d.count; actual != tt.expected {
t.Errorf("TestDialCounts %d: expected %d, got %d", i, tt.expected, actual)
}
}
}