-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn_test.go
101 lines (98 loc) · 2.35 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package kpx
import (
"fmt"
"net"
"net/http"
"strings"
"testing"
"time"
)
func TestClosedConnFailsOnWrite(t *testing.T) {
hp := "127.0.0.1:12345"
// create a fake server on random port
l, err := net.Listen("tcp4", hp)
if err != nil {
t.Fatalf("error listen: %v", err)
}
go func() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello, %s!", request.URL.Path[1:])
})
http.Serve(l, nil)
}()
// create a connection to this random port
dialer := new(net.Dialer)
c, err := dialer.Dial("tcp4", hp)
if err != nil {
t.Fatalf("error dial: %v", err)
}
ConfigureConn(c)
// get content
_, err = c.Write([]byte("GET /world HTTP/1.0\n\n"))
if err != nil {
t.Fatalf("error write 1: %v", err)
}
b := make([]byte, 4096)
n, err := c.Read(b)
if err != nil {
t.Fatalf("error read 1: %v", err)
}
if !strings.Contains(string(b[0:n]), "Hello, world!") {
t.Fatalf("error, 'Hello, world!' not found")
}
// restart http server
l.Close()
// get content and check it fails
_, err = c.Write([]byte("G"))
if err != nil {
t.Log("Fail on first write: success")
return
}
_, err = c.Write([]byte("ET / HTTP/1.0\n\n"))
if err != nil {
t.Log("Fail on second write: success")
return
}
n, err = c.Read(b)
if err != nil {
// unfortunately, this still happen from time to time
t.Fatalf("error read 2: %v", err)
}
t.Fatalf("error write and read")
}
func TestLostConnection(t *testing.T) {
hp := "127.0.0.1:12345"
// create a fake server on random port
l, err := net.Listen("tcp4", hp)
if err != nil {
t.Fatalf("error listen: %v", err)
}
go func() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello, %s!", request.URL.Path[1:])
})
http.Serve(l, nil)
}()
// create a connection to this random port
dialer := new(net.Dialer)
c, err := dialer.Dial("tcp4", hp)
if err != nil {
t.Fatalf("error dial: %v", err)
}
// get content
_, err = c.Write([]byte("GET /world HTTP/1.0\n\n"))
if err != nil {
t.Fatalf("error write 1: %v", err)
}
b := make([]byte, 4096)
n, err := c.Read(b)
if err != nil {
t.Fatalf("error read 1: %v", err)
}
if !strings.Contains(string(b[0:n]), "Hello, world!") {
t.Fatalf("error, 'Hello, world!' not found")
}
c.SetDeadline(time.Now().Add(time.Second * 5))
time.Sleep(time.Second * 60)
l.Close()
}