-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhalfclose_test.go
78 lines (69 loc) · 1.22 KB
/
halfclose_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
package netutil
import (
"bytes"
"fmt"
"io"
"net"
"testing"
"time"
)
func TestHalfCloser(t *testing.T) {
t.Parallel()
l, err := net.Listen("tcp", "localhost:15346")
if err != nil {
t.Skip(err)
}
errCh := make(chan error, 1)
done := make(chan struct{})
go func() {
defer close(done)
c, err := l.Accept()
if err != nil {
errCh <- err
return
}
defer c.Close()
hc, ok := c.(HalfCloser)
if !ok {
t.Error("c is not a HalfCloser")
}
c.Write([]byte("test"))
err = hc.CloseWrite()
if err != nil {
errCh <- err
return
}
data, err := io.ReadAll(c)
if err != nil {
errCh <- err
} else if !bytes.Equal(data, []byte("ack")) {
errCh <- fmt.Errorf(`!bytes.Equal(data, []byte("ack")), data=%s`, data)
}
}()
c, err := net.DialTimeout("tcp", "localhost:15346", 1*time.Second)
if err != nil {
t.Fatal(err)
}
defer c.Close()
data, err := io.ReadAll(c)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, []byte("test")) {
t.Error(`!bytes.Equal(data, []byte("test"))`)
}
_, err = c.Write([]byte("ack"))
if err != nil {
t.Error(err)
}
err = c.(HalfCloser).CloseWrite()
if err != nil {
t.Error(err)
}
<-done
select {
case err2 := <-errCh:
t.Error(err2)
default:
}
}