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

Method TestBuffer_NewBufferRefAndFree

mem/buffers_test.go:69–101  ·  view source on GitHub ↗

Tests that a buffer created with NewBuffer, on which an additional reference is acquired, which when later freed, invokes the free function with the correct data, but only after all references are released.

(t *testing.T)

Source from the content-addressed store, hash-verified

67// is acquired, which when later freed, invokes the free function with the
68// correct data, but only after all references are released.
69func (s) TestBuffer_NewBufferRefAndFree(t *testing.T) {
70 data := "abcd"
71 freed := false
72 freeF := poolFunc(func(got *[]byte) {
73 if !bytes.Equal(*got, []byte(data)) {
74 t.Fatalf("Free function called with bytes %s, want %s", string(*got), string(data))
75 }
76 freed = true
77 })
78
79 buf := newBuffer([]byte(data), freeF)
80 if got := buf.ReadOnlyData(); !bytes.Equal(got, []byte(data)) {
81 t.Fatalf("Buffer contains data %s, want %s", string(got), string(data))
82 }
83
84 buf.Ref()
85 if got := buf.ReadOnlyData(); !bytes.Equal(got, []byte(data)) {
86 t.Fatalf("New reference to the Buffer contains data %s, want %s", string(got), string(data))
87 }
88
89 // Verify that the free function is not invoked when all references are yet
90 // to be freed.
91 buf.Free()
92 if freed {
93 t.Fatalf("Free function called before all references freed")
94 }
95
96 // Verify that the free function is invoked when all references are freed.
97 buf.Free()
98 if !freed {
99 t.Fatalf("Buffer not freed")
100 }
101}
102
103func (s) TestBuffer_NewBufferHandlesShortBuffers(t *testing.T) {
104 const threshold = 100

Callers

nothing calls this directly

Calls 7

poolFuncFuncType · 0.85
newBufferFunction · 0.70
EqualMethod · 0.65
FatalfMethod · 0.65
ReadOnlyDataMethod · 0.65
RefMethod · 0.65
FreeMethod · 0.65

Tested by

no test coverage detected