Files
kopia/internal/gather/gather_write_buffer_chunk_test.go
Jarek Kowalski 35d0f31c0d huge: replaced the use of allocated byte slices with populating gather.WriteBuffer in the repository (#1244)
This helps recycle buffers more efficiently during snapshots.
Also, improved memory tracking, enabled profiling flags and added pprof
by default.
2021-08-20 08:45:10 -07:00

62 lines
1.6 KiB
Go

package gather
import (
"bytes"
"testing"
)
func TestWriteBufferChunk(t *testing.T) {
// reset for testing
all := &chunkAllocator{
chunkSize: 100,
maxFreeListSize: 10,
}
// reset for testing
chunk1 := all.allocChunk()
_ = append(chunk1, []byte("chunk1")...)
if got, want := len(chunk1), 0; got != want {
t.Errorf("invalid chunk len: %v, want %v", got, want)
}
if got, want := cap(chunk1), all.chunkSize; got != want {
t.Errorf("invalid chunk cap: %v, want %v", got, want)
}
if got, want := all.freeListHighWaterMark, 0; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
chunk2 := all.allocChunk()
_ = append(chunk2, []byte("chunk2")...)
if got, want := all.freeListHighWaterMark, 0; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
all.releaseChunk(chunk2)
if got, want := all.freeListHighWaterMark, 1; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
all.releaseChunk(chunk1)
if got, want := all.freeListHighWaterMark, 2; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
// allocate chunk3 - make sure we got the same slice as chunk1 (LIFO)
chunk3 := all.allocChunk()
if got, want := chunk3[0:6], []byte("chunk1"); !bytes.Equal(got, want) {
t.Errorf("got wrong chunk data %q, want %q", string(got), string(want))
}
// allocate chunk4 - make sure we got the same slice as chunk1 (LIFO)
chunk4 := all.allocChunk()
if got, want := chunk4[0:6], []byte("chunk2"); !bytes.Equal(got, want) {
t.Errorf("got wrong chunk data %q, want %q", string(got), string(want))
}
}