Files
kopia/internal/gather/gather_write_buffer_chunk_test.go
Jarek Kowalski 60977812f0 Support for gather writes (#373)
, where blob.Storage.PutBlob gets a list of slices and writes them sequentially 
* performance: added gather.Bytes and gather.WriteBuffer

They are similar to bytes.Buffer but instead of managing a single
byte slice, they maintain a list of slices that and when they run out of
space they allocate new fixed-size slice from a free list.

This helps keep memory allocations completely under control regardless
of the size of data written.

* switch from byte slices and bytes.Buffer to gather.Bytes.

This is mostly mechanical, the only cases where it's not involve blob
storage providers, where we leverage the fact that we don't need to
ever concatenate the slices into one and instead we can do gather
writes.

* PR feedback
2020-03-24 15:05:52 -07:00

59 lines
1.5 KiB
Go

package gather
import (
"bytes"
"testing"
)
func TestWriteBufferChunk(t *testing.T) {
// reset for testing
freeList = nil
freeListHighWaterMark = 0
chunk1 := 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), chunkSize; got != want {
t.Errorf("invalid chunk cap: %v, want %v", got, want)
}
if got, want := freeListHighWaterMark, 0; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
chunk2 := allocChunk()
_ = append(chunk2, []byte("chunk2")...)
if got, want := freeListHighWaterMark, 0; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
releaseChunk(chunk2)
if got, want := freeListHighWaterMark, 1; got != want {
t.Errorf("unexpected high water mark %v, want %v", got, want)
}
releaseChunk(chunk1)
if got, want := 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 := 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 := 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))
}
}