Tests that a buffer created with NewBuffer, which when later freed, invokes the free function with the correct data.
(t *testing.T)
| 42 | // Tests that a buffer created with NewBuffer, which when later freed, invokes |
| 43 | // the free function with the correct data. |
| 44 | func (s) TestBuffer_NewBufferAndFree(t *testing.T) { |
| 45 | data := "abcd" |
| 46 | freed := false |
| 47 | freeF := poolFunc(func(got *[]byte) { |
| 48 | if !bytes.Equal(*got, []byte(data)) { |
| 49 | t.Fatalf("Free function called with bytes %s, want %s", string(*got), data) |
| 50 | } |
| 51 | freed = true |
| 52 | }) |
| 53 | |
| 54 | buf := newBuffer([]byte(data), freeF) |
| 55 | if got := buf.ReadOnlyData(); !bytes.Equal(got, []byte(data)) { |
| 56 | t.Fatalf("Buffer contains data %s, want %s", string(got), string(data)) |
| 57 | } |
| 58 | |
| 59 | // Verify that the free function is invoked when all references are freed. |
| 60 | buf.Free() |
| 61 | if !freed { |
| 62 | t.Fatalf("Buffer not freed") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Tests that a buffer created with NewBuffer, on which an additional reference |
| 67 | // is acquired, which when later freed, invokes the free function with the |