MCPcopy
hub / github.com/segmentio/kafka-go / TestReadNewBytes

Function TestReadNewBytes

read_test.go:119–175  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

117}
118
119func TestReadNewBytes(t *testing.T) {
120
121 t.Run("reads new bytes", func(t *testing.T) {
122 r := bufio.NewReader(bytes.NewReader([]byte("foobar")))
123
124 b, remain, err := readNewBytes(r, 6, 3)
125 if string(b) != "foo" {
126 t.Error("should have returned 3 bytes")
127 }
128 if remain != 3 {
129 t.Error("should have calculated remaining correctly")
130 }
131 if err != nil {
132 t.Error("should not have errored")
133 }
134
135 b, remain, err = readNewBytes(r, remain, 3)
136 if string(b) != "bar" {
137 t.Error("should have returned 3 bytes")
138 }
139 if remain != 0 {
140 t.Error("should have calculated remaining correctly")
141 }
142 if err != nil {
143 t.Error("should not have errored")
144 }
145
146 b, err = r.Peek(0)
147 if len(b) > 0 {
148 t.Error("not all bytes were consumed")
149 }
150 if err != nil {
151 t.Error("should not have errored during peek")
152 }
153 })
154
155 t.Run("discards bytes when insufficient", func(t *testing.T) {
156 r := bufio.NewReader(bytes.NewReader([]byte("foo")))
157 b, remain, err := readNewBytes(bufio.NewReader(r), 3, 4)
158 if string(b) != "foo" {
159 t.Error("should have returned available bytes")
160 }
161 if remain != 0 {
162 t.Error("all bytes should have been consumed")
163 }
164 if !errors.Is(err, errShortRead) {
165 t.Error("should have returned errShortRead")
166 }
167 b, err = r.Peek(0)
168 if len(b) > 0 {
169 t.Error("not all bytes were consumed")
170 }
171 if err != nil {
172 t.Error("should not have errored during peek")
173 }
174 })
175}
176

Callers

nothing calls this directly

Calls 4

readNewBytesFunction · 0.85
NewReaderMethod · 0.65
PeekMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected