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

Method TestBuffer_Split

mem/buffers_test.go:260–300  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

258}
259
260func (s) TestBuffer_Split(t *testing.T) {
261 freed := make(chan struct{})
262 data := []byte{1, 2, 3, 4}
263 buf := mem.NewBuffer(&data, poolFunc(func(*[]byte) {
264 close(freed)
265 }))
266
267 buf, split1 := mem.SplitUnsafe(buf, 2)
268 if !bytes.Equal(buf.ReadOnlyData(), data[:2]) {
269 t.Fatalf("Buffer did not contain expected data. got %v, want %v", buf.ReadOnlyData(), data[:2])
270 }
271 if !bytes.Equal(split1.ReadOnlyData(), data[2:]) {
272 t.Fatalf("Buffer did not contain expected data. got %v, want %v", split1.ReadOnlyData(), data[2:])
273 }
274
275 // Check that splitting the buffer more than once works as intended.
276 split1, split2 := mem.SplitUnsafe(split1, 1)
277 if !bytes.Equal(split1.ReadOnlyData(), data[2:3]) {
278 t.Fatalf("Buffer did not contain expected data. got %v, want %v", split1.ReadOnlyData(), data[2:3])
279 }
280 if !bytes.Equal(split2.ReadOnlyData(), data[3:]) {
281 t.Fatalf("Buffer did not contain expected data. got %v, want %v", split2.ReadOnlyData(), data[3:])
282 }
283
284 // If any of the following frees actually free the buffer, the test will fail.
285 buf.Free()
286 split2.Free()
287 select {
288 case <-freed:
289 t.Fatalf("Freed too early")
290 default:
291 }
292
293 split1.Free()
294
295 select {
296 case <-freed:
297 default:
298 t.Fatalf("Still not freed")
299 }
300}
301
302func (s) TestBuffer_Slice(t *testing.T) {
303 freed := make(chan struct{})

Callers

nothing calls this directly

Calls 7

ReadOnlyDataMethod · 0.95
FreeMethod · 0.95
NewBufferFunction · 0.92
SplitUnsafeFunction · 0.92
poolFuncFuncType · 0.85
EqualMethod · 0.65
FatalfMethod · 0.65

Tested by

no test coverage detected