...
1
2
3
4
5 package http2
6
7 import (
8 "bytes"
9 "errors"
10 "io"
11 "testing"
12 )
13
14 func TestPipeClose(t *testing.T) {
15 var p pipe
16 p.b = new(bytes.Buffer)
17 a := errors.New("a")
18 b := errors.New("b")
19 p.CloseWithError(a)
20 p.CloseWithError(b)
21 _, err := p.Read(make([]byte, 1))
22 if err != a {
23 t.Errorf("err = %v want %v", err, a)
24 }
25 }
26
27 func TestPipeDoneChan(t *testing.T) {
28 var p pipe
29 done := p.Done()
30 select {
31 case <-done:
32 t.Fatal("done too soon")
33 default:
34 }
35 p.CloseWithError(io.EOF)
36 select {
37 case <-done:
38 default:
39 t.Fatal("should be done")
40 }
41 }
42
43 func TestPipeDoneChan_ErrFirst(t *testing.T) {
44 var p pipe
45 p.CloseWithError(io.EOF)
46 done := p.Done()
47 select {
48 case <-done:
49 default:
50 t.Fatal("should be done")
51 }
52 }
53
54 func TestPipeDoneChan_Break(t *testing.T) {
55 var p pipe
56 done := p.Done()
57 select {
58 case <-done:
59 t.Fatal("done too soon")
60 default:
61 }
62 p.BreakWithError(io.EOF)
63 select {
64 case <-done:
65 default:
66 t.Fatal("should be done")
67 }
68 }
69
70 func TestPipeDoneChan_Break_ErrFirst(t *testing.T) {
71 var p pipe
72 p.BreakWithError(io.EOF)
73 done := p.Done()
74 select {
75 case <-done:
76 default:
77 t.Fatal("should be done")
78 }
79 }
80
81 func TestPipeCloseWithError(t *testing.T) {
82 p := &pipe{b: new(bytes.Buffer)}
83 const body = "foo"
84 io.WriteString(p, body)
85 a := errors.New("test error")
86 p.CloseWithError(a)
87 all, err := io.ReadAll(p)
88 if string(all) != body {
89 t.Errorf("read bytes = %q; want %q", all, body)
90 }
91 if err != a {
92 t.Logf("read error = %v, %v", err, a)
93 }
94 if p.Len() != 0 {
95 t.Errorf("pipe should have 0 unread bytes")
96 }
97
98 if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
99 t.Errorf("Write(abc) after close\ngot %v, %v\nwant 0, %v", n, err, errClosedPipeWrite)
100 }
101 if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
102 t.Errorf("Read() after close\ngot %v, nil\nwant 0, %v", n, errClosedPipeWrite)
103 }
104 if p.Len() != 0 {
105 t.Errorf("pipe should have 0 unread bytes")
106 }
107 }
108
109 func TestPipeBreakWithError(t *testing.T) {
110 p := &pipe{b: new(bytes.Buffer)}
111 io.WriteString(p, "foo")
112 a := errors.New("test err")
113 p.BreakWithError(a)
114 all, err := io.ReadAll(p)
115 if string(all) != "" {
116 t.Errorf("read bytes = %q; want empty string", all)
117 }
118 if err != a {
119 t.Logf("read error = %v, %v", err, a)
120 }
121 if p.b != nil {
122 t.Errorf("buffer should be nil after BreakWithError")
123 }
124 if p.Len() != 3 {
125 t.Errorf("pipe should have 3 unread bytes")
126 }
127
128 if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
129 t.Errorf("Write(abc) after break\ngot %v, %v\nwant 0, errClosedPipeWrite", n, err)
130 }
131 if p.b != nil {
132 t.Errorf("buffer should be nil after Write")
133 }
134 if p.Len() != 3 {
135 t.Errorf("pipe should have 6 unread bytes")
136 }
137
138 if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
139 t.Errorf("Read() after close\ngot %v, nil\nwant 0, not nil", n)
140 }
141 }
142
View as plain text