2015-03-09 17:20:26 +00:00
|
|
|
package storage
|
2015-02-28 17:47:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-13 18:42:14 +00:00
|
|
|
"fmt"
|
2023-07-20 10:25:32 +00:00
|
|
|
"io"
|
2015-08-13 18:42:14 +00:00
|
|
|
"strings"
|
2015-02-28 17:47:55 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetter(t *testing.T) {
|
|
|
|
fgp := NewBufferFileGetPutter()
|
2015-03-03 19:23:04 +00:00
|
|
|
files := map[string]map[string][]byte{
|
|
|
|
"file1.txt": {"foo": []byte{60, 60, 48, 48, 0, 0, 0, 0}},
|
|
|
|
"file2.txt": {"bar": []byte{45, 196, 22, 240, 0, 0, 0, 0}},
|
2015-02-28 17:47:55 +00:00
|
|
|
}
|
|
|
|
for n, b := range files {
|
2015-03-03 19:23:04 +00:00
|
|
|
for body, sum := range b {
|
2015-03-04 22:14:43 +00:00
|
|
|
_, csum, err := fgp.Put(n, bytes.NewBufferString(body))
|
2015-03-03 19:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(csum, sum) {
|
|
|
|
t.Errorf("checksum: expected 0x%x; got 0x%x", sum, csum)
|
|
|
|
}
|
2015-02-28 17:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for n, b := range files {
|
2015-03-03 19:23:04 +00:00
|
|
|
for body := range b {
|
|
|
|
r, err := fgp.Get(n)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2023-07-20 10:25:32 +00:00
|
|
|
buf, err := io.ReadAll(r)
|
2015-03-03 19:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if body != string(buf) {
|
|
|
|
t.Errorf("expected %q, got %q", body, string(buf))
|
|
|
|
}
|
2015-02-28 17:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 18:42:14 +00:00
|
|
|
|
2015-03-02 20:25:03 +00:00
|
|
|
func TestPutter(t *testing.T) {
|
|
|
|
fp := NewDiscardFilePutter()
|
2015-03-03 19:23:04 +00:00
|
|
|
// map[filename]map[body]crc64sum
|
|
|
|
files := map[string]map[string][]byte{
|
|
|
|
"file1.txt": {"foo": []byte{60, 60, 48, 48, 0, 0, 0, 0}},
|
|
|
|
"file2.txt": {"bar": []byte{45, 196, 22, 240, 0, 0, 0, 0}},
|
|
|
|
"file3.txt": {"baz": []byte{32, 68, 22, 240, 0, 0, 0, 0}},
|
|
|
|
"file4.txt": {"bif": []byte{48, 9, 150, 240, 0, 0, 0, 0}},
|
2015-03-02 20:25:03 +00:00
|
|
|
}
|
|
|
|
for n, b := range files {
|
2015-03-03 19:23:04 +00:00
|
|
|
for body, sum := range b {
|
2015-03-04 22:14:43 +00:00
|
|
|
_, csum, err := fp.Put(n, bytes.NewBufferString(body))
|
2015-03-03 19:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(csum, sum) {
|
|
|
|
t.Errorf("checksum on %q: expected %v; got %v", n, sum, csum)
|
|
|
|
}
|
2015-03-02 20:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 18:42:14 +00:00
|
|
|
|
|
|
|
func BenchmarkPutter(b *testing.B) {
|
|
|
|
files := []string{
|
|
|
|
strings.Repeat("foo", 1000),
|
|
|
|
strings.Repeat("bar", 1000),
|
|
|
|
strings.Repeat("baz", 1000),
|
|
|
|
strings.Repeat("fooz", 1000),
|
|
|
|
strings.Repeat("vbatts", 1000),
|
|
|
|
strings.Repeat("systemd", 1000),
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
fgp := NewBufferFileGetPutter()
|
|
|
|
for n, body := range files {
|
|
|
|
if _, _, err := fgp.Put(fmt.Sprintf("%d", n), bytes.NewBufferString(body)); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|