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)
| 183 | // acquired, which when later freed, returns the underlying byte slice to the |
| 184 | // buffer pool. |
| 185 | func (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 | |
| 216 | func (s) TestBuffer_ReadOnlyDataAfterFree(t *testing.T) { |
| 217 | // Verify that reading before freeing does not panic. |