MCPcopy
hub / github.com/grpc/grpc-go / TestBufferSlice_Reader

Method TestBufferSlice_Reader

mem/buffer_slice_test.go:131–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

129}
130
131func (s) TestBufferSlice_Reader(t *testing.T) {
132 bs := mem.BufferSlice{
133 newBuffer([]byte("abcd"), nil),
134 newBuffer([]byte("abcd"), nil),
135 newBuffer([]byte("abcd"), nil),
136 }
137 wantData := []byte("abcdabcdabcd")
138
139 reader := bs.Reader()
140 var gotData []byte
141 // Read into a buffer of size 1 until EOF, and verify that the data matches.
142 for {
143 buf := make([]byte, 1)
144 n, err := reader.Read(buf)
145 if n > 0 {
146 gotData = append(gotData, buf[:n]...)
147 }
148 if err == io.EOF {
149 break
150 }
151 if err != nil {
152 t.Fatalf("BufferSlice.Reader() failed unexpectedly: %v", err)
153 }
154 }
155 if !bytes.Equal(gotData, wantData) {
156 t.Errorf("BufferSlice.Reader() returned data %v, want %v", string(gotData), string(wantData))
157 }
158
159 // Reader should have released its references to the underlying buffers, but
160 // bs still holds its reference and it should be able to read data from it.
161 gotData = bs.Materialize()
162 if !bytes.Equal(gotData, wantData) {
163 t.Errorf("BufferSlice.Materialize() = %s, want %s", string(gotData), string(wantData))
164 }
165}
166
167// TestBufferSlice_ReadAll_Reads exercises ReadAll by allowing it to read
168// various combinations of data, empty data, EOF.

Callers

nothing calls this directly

Calls 7

ReaderMethod · 0.95
MaterializeMethod · 0.95
newBufferFunction · 0.70
ReadMethod · 0.65
FatalfMethod · 0.65
EqualMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected