2019-10-03 19:36:35 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCachingBlobStore_Put(t *testing.T) {
|
2019-10-03 22:47:23 +02:00
|
|
|
origin := NewMemoryBlobStore()
|
|
|
|
cache := NewMemoryBlobStore()
|
2019-10-03 19:36:35 +02:00
|
|
|
s := NewCachingBlobStore(origin, cache)
|
|
|
|
|
|
|
|
b := []byte("this is a blob of stuff")
|
|
|
|
hash := "hash"
|
|
|
|
|
|
|
|
err := s.Put(hash, b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err := origin.Has(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Errorf("failed to store blob in origin")
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err = cache.Has(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Errorf("failed to store blob in cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCachingBlobStore_CacheMiss(t *testing.T) {
|
2019-10-03 22:47:23 +02:00
|
|
|
origin := NewMemoryBlobStore()
|
|
|
|
cache := NewMemoryBlobStore()
|
2019-10-03 19:36:35 +02:00
|
|
|
s := NewCachingBlobStore(origin, cache)
|
|
|
|
|
|
|
|
b := []byte("this is a blob of stuff")
|
|
|
|
hash := "hash"
|
|
|
|
err := origin.Put(hash, b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := s.Get(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b, res) {
|
|
|
|
t.Errorf("expected Get() to return %s, got %s", string(b), string(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err := cache.Has(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Errorf("Get() did not copy blob to cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = cache.Get(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b, res) {
|
|
|
|
t.Errorf("expected cached Get() to return %s, got %s", string(b), string(res))
|
|
|
|
}
|
|
|
|
}
|