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)
| 67 | // is acquired, which when later freed, invokes the free function with the |
| 68 | // correct data, but only after all references are released. |
| 69 | func (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 | |
| 103 | func (s) TestBuffer_NewBufferHandlesShortBuffers(t *testing.T) { |
| 104 | const threshold = 100 |