forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufio_test.go
87 lines (71 loc) · 1.96 KB
/
bufio_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
package resp
import (
"bytes"
"io"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BulkReader", func() {
// var buf = new(bytes.Buffer)
//
// setup := func() *RequestWriter {
// buf.Reset()
// return NewRequestWriter(buf)
// }
It("ReadAll: should report error on closing prematurely", func() {
bioR := &bufioR{
rd: bytes.NewBufferString(strings.Repeat("x", 50)),
buf: make([]byte, 10),
}
r := newBulkReader(bioR, 100)
r.Close()
ret, err := r.ReadAll()
Expect(err).To(Equal(io.EOF))
Expect(len(ret)).To(Equal(0))
})
It("ReadAll: should report error on closing unexpectedly", func() {
bioR := &bufioR{
rd: bytes.NewBufferString(strings.Repeat("x", 50)),
buf: make([]byte, 10),
}
r := newBulkReader(bioR, 100)
ret, err := r.ReadAll()
Expect(err).To(Equal(io.ErrUnexpectedEOF))
Expect(len(ret)).To(Equal(50))
})
It("ReadAll: should not return ErrUnexpectedEOF", func() {
bioR := &bufioR{
rd: bytes.NewReader([]byte("$95\r\n" + strings.Repeat("x", 95) + "\r\n")),
buf: make([]byte, 10),
}
stream, err := bioR.StreamBulk()
Expect(err).To(BeNil())
Expect(stream.Len()).To(Equal(int64(95)))
all, err := stream.ReadAll()
Expect(err).To(BeNil())
Expect(len(all)).To(Equal(95))
Expect(all).To(Equal([]byte(strings.Repeat("x", 95))))
p := make([]byte, 10)
stream.Read(p)
rest, err := stream.Read(p)
Expect(err).To(Equal(io.EOF))
Expect(rest).To(Equal(0))
Expect(p[0:2]).NotTo(Equal([]byte("\r\n")))
})
It("Read: should no CRLF left behind", func() {
bioR := &bufioR{
rd: bytes.NewReader([]byte("$95\r\n" + strings.Repeat("x", 95) + "\r\n")),
buf: make([]byte, 100),
}
stream, err := bioR.StreamBulk()
Expect(err).To(BeNil())
Expect(stream.Len()).To(Equal(int64(95)))
p := make([]byte, 100)
stream.Read(p)
rest, err := stream.Read(p)
Expect(err).To(Equal(io.EOF))
Expect(rest).To(Equal(0))
Expect(p[0:2]).NotTo(Equal([]byte("\r\n")))
})
})