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

Method TestBuffer_CopyRefAndFree

mem/buffers_test.go:185–214  ·  view source on GitHub ↗

Tests that a buffer created with Copy, on which an additional reference is acquired, which when later freed, returns the underlying byte slice to the buffer pool.

(t *testing.T)

Source from the content-addressed store, hash-verified

183// acquired, which when later freed, returns the underlying byte slice to the
184// buffer pool.
185func (s) TestBuffer_CopyRefAndFree(t *testing.T) {
186 data := []byte("abcd")
187 testPool := &singleBufferPool{
188 t: t,
189 data: &data,
190 }
191
192 buf := mem.Copy(data, testPool)
193 if got := buf.ReadOnlyData(); !bytes.Equal(got, data) {
194 t.Fatalf("Buffer contains data %s, want %s", string(got), string(data))
195 }
196
197 buf.Ref()
198 if got := buf.ReadOnlyData(); !bytes.Equal(got, []byte(data)) {
199 t.Fatalf("New reference to the Buffer contains data %s, want %s", string(got), string(data))
200 }
201
202 // Verify that the free function is not invoked when all references are yet
203 // to be freed.
204 buf.Free()
205 if testPool.data == nil {
206 t.Fatalf("Free function called before all references freed")
207 }
208
209 // Verify that the free function is invoked when all references are freed.
210 buf.Free()
211 if testPool.data != nil {
212 t.Fatalf("Free never called")
213 }
214}
215
216func (s) TestBuffer_ReadOnlyDataAfterFree(t *testing.T) {
217 // Verify that reading before freeing does not panic.

Callers

nothing calls this directly

Calls 6

CopyFunction · 0.92
ReadOnlyDataMethod · 0.65
EqualMethod · 0.65
FatalfMethod · 0.65
RefMethod · 0.65
FreeMethod · 0.65

Tested by

no test coverage detected