Debian Codegraph @

nio_test.go

raw symbols SCIP
1 package nio
2
3 import (
4 "bufio"
5 "bytes"
6 "errors"
7 "io"
8 "testing"
9
10 "io/ioutil"
11
12 "time"
13
14 "github.com/djherbis/buffer"
15 )
16
17 func TestCopy(t *testing.T) {
18 buf := buffer.New(1024)
19 input := bytes.NewBuffer(nil)
20 output := bytes.NewBuffer(nil)
21
22 input.Write([]byte("hello world"))
23 n, err := Copy(output, input, buf)
24 if err != nil {
25 t.Errorf(err.Error())
26 }
27 if int(n) != len("hello world") {
28 t.Errorf("wrote wrong # of bytes")
29 }
30
31 if !bytes.Equal(output.Bytes(), []byte("hello world")) {
32 t.Errorf("output didn't match: %s", output.Bytes())
33 }
34 }
35
36 func TestBigWriteSmallBuf(t *testing.T) {
37 buf := buffer.New(5)
38 r, w := Pipe(buf)
39 defer r.Close()
40
41 go func() {
42 defer w.Close()
43 n, err := w.Write([]byte("hello world"))
44 if err != nil {
45 t.Error(err.Error())
46 }
47 if int(n) != len("hello world") {
48 t.Errorf("wrote wrong # of bytes")
49 }
50 }()
51
52 output := bytes.NewBuffer(nil)
53 _, err := io.Copy(output, r)
54 if err != nil {
55 t.Error(err.Error())
56 }
57 if !bytes.Equal(output.Bytes(), []byte("hello world")) {
58 t.Errorf("unexpected output %s", output.Bytes())
59 }
60 }
61
62 func TestPipeCloseEarly(t *testing.T) {
63 buf := buffer.New(1024)
64 r, w := Pipe(buf)
65 r.Close()
66
67 _, err := w.Write([]byte("hello world"))
68 if err != io.ErrClosedPipe {
69 t.Errorf("expected closed pipe")
70 }
71
72 _, err = io.Copy(ioutil.Discard, r)
73 if err != io.ErrClosedPipe {
74 t.Errorf("expected closed pipe")
75 }
76 }
77
78 func TestPipe(t *testing.T) {
79 buf := buffer.New(1024)
80 r, w := Pipe(buf)
81 defer r.Close()
82
83 data := []byte("the quick brown fox jumps over the lazy dog")
84 if _, err := w.Write(data); err != nil {
85 t.Error(err.Error())
86 return
87 }
88 w.Close()
89
90 result := make([]byte, 1024)
91 n, err := r.Read(result)
92 if err != nil && err != io.EOF {
93 t.Error(err.Error())
94 return
95 }
96 result = result[:n]
97
98 if !bytes.Equal(data, result) {
99 t.Errorf("exp [%s]\ngot[%s]", string(data), string(result))
100 }
101 }
102
103 func TestEarlyCloseWrite(t *testing.T) {
104 buf := buffer.New(1)
105 r, w := Pipe(buf)
106
107 testerr := errors.New("test err")
108
109 w.CloseWithError(testerr)
110
111 _, err := w.Write([]byte("ab")) // too big for buffer
112
113 if err != io.ErrClosedPipe {
114 t.Errorf("expected %s but got %s.", testerr, err)
115 }
116
117 _, err = io.Copy(ioutil.Discard, r)
118 if err != testerr {
119 t.Errorf("expected %s but got %s.", testerr, err)
120 }
121 }
122
123 func TestUnblockWrite(t *testing.T) {
124 buf := buffer.New(1)
125 r, w := Pipe(buf)
126
127 testerr := errors.New("test err")
128
129 go func() {
130 <-time.After(100 * time.Millisecond)
131 if er := w.CloseWithError(testerr); er != nil {
132 t.Error(er)
133 }
134 }()
135
136 _, err := w.Write([]byte("ab")) // too big for buffer
137
138 if err != io.ErrClosedPipe {
139 t.Errorf("expected %s but got %s.", testerr, err)
140 }
141
142 _, err = io.Copy(ioutil.Discard, r)
143 if err != testerr {
144 t.Errorf("expected %s but got %s.", testerr, err)
145 }
146 }
147
148 type badBuffer struct{}
149
150 func (badBuffer) Len() int64 { return 3 }
151 func (badBuffer) Cap() int64 { return 6 }
152 func (badBuffer) Write(p []byte) (int, error) { return len(p), nil }
153 func (badBuffer) Read(p []byte) (int, error) { return 0, io.EOF }
154
155 func TestEmpty(t *testing.T) {
156 r, w := Pipe(badBuffer{})
157 n, err := w.Write([]byte("any"))
158
159 if err != nil {
160 t.Error(err)
161 }
162
163 if n != 3 {
164 t.Errorf("wrote wrong # of bytes %d", n)
165 }
166
167 n, err = r.Read(nil)
168
169 if err != nil {
170 t.Error(err)
171 }
172
173 if n != 0 {
174 t.Errorf("wrote wrong # of bytes %d", n)
175 }
176 }
177
178 func BenchmarkPipe(b *testing.B) {
179 r, w := Pipe(buffer.New(1024))
180 benchPipe(r, w, b)
181 }
182
183 func BenchmarkIOPipe(b *testing.B) {
184 r, w := io.Pipe()
185 benchPipe(r, w, b)
186 }
187
188 func benchPipe(r io.Reader, w io.WriteCloser, b *testing.B) {
189 b.ReportAllocs()
190 f, err := ioutil.TempFile("", "benchPipe")
191 if err != nil {
192 b.Error(err)
193 }
194
195 go func() {
196 defer f.Close()
197 io.Copy(bufio.NewWriter(f), r)
198 }()
199
200 defer w.Close()
201
202 for i := 0; i < b.N; i++ {
203 w.Write([]byte("hello world"))
204 }
205 }