2014-12-05 20:45:49 +00:00
|
|
|
package restic_test
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"io"
|
|
|
|
mrand "math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
2015-04-26 12:46:15 +00:00
|
|
|
"github.com/restic/restic/server"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2014-11-21 20:21:44 +00:00
|
|
|
)
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
var maxWorkers = flag.Uint("workers", 20, "number of workers to test Map concurrent access against")
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
func randomID() []byte {
|
2014-11-23 15:48:00 +00:00
|
|
|
buf := make([]byte, backend.IDSize)
|
2014-11-21 20:21:44 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
func newBlob() server.Blob {
|
|
|
|
return server.Blob{
|
2015-01-10 22:40:10 +00:00
|
|
|
ID: randomID(),
|
|
|
|
Size: uint64(mrand.Uint32()),
|
|
|
|
Storage: randomID(),
|
|
|
|
StorageSize: uint64(mrand.Uint32()),
|
|
|
|
}
|
2014-11-21 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test basic functionality
|
2015-01-10 22:40:10 +00:00
|
|
|
func TestMap(t *testing.T) {
|
|
|
|
bl := restic.NewMap()
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
b := newBlob()
|
|
|
|
bl.Insert(b)
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
bl.Insert(newBlob())
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
b2, err := bl.Find(server.Blob{ID: b.ID, Size: b.Size})
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, b2.Compare(b) == 0, "items are not equal: want %v, got %v", b, b2)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
b2, err = bl.FindID(b.ID)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, b2.Compare(b) == 0, "items are not equal: want %v, got %v", b, b2)
|
2014-11-21 20:21:44 +00:00
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
bl2 := restic.NewMap()
|
2014-11-21 20:21:44 +00:00
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
bl.Insert(newBlob())
|
|
|
|
}
|
|
|
|
|
|
|
|
b2, err = bl2.Find(b)
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, err != nil, "found ID in restic that was never inserted: %v", b2)
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
bl2.Merge(bl)
|
|
|
|
|
|
|
|
b2, err = bl2.Find(b)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Compare(b2) != 0 {
|
|
|
|
t.Fatalf("items are not equal: want %v, got %v", b, b2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test JSON encode/decode
|
2015-01-10 22:40:10 +00:00
|
|
|
func TestMapJSON(t *testing.T) {
|
|
|
|
bl := restic.NewMap()
|
2015-04-26 12:46:15 +00:00
|
|
|
b := server.Blob{ID: randomID()}
|
2014-11-21 20:21:44 +00:00
|
|
|
bl.Insert(b)
|
|
|
|
|
|
|
|
b2, err := bl.Find(b)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, b2.Compare(b) == 0, "items are not equal: want %v, got %v", b, b2)
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
buf, err := json.Marshal(bl)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2014-11-21 20:21:44 +00:00
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
bl2 := restic.Map{}
|
2014-11-21 20:21:44 +00:00
|
|
|
json.Unmarshal(buf, &bl2)
|
|
|
|
|
|
|
|
b2, err = bl2.Find(b)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
|
|
|
Assert(t, b2.Compare(b) == 0, "items are not equal: want %v, got %v", b, b2)
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
buf, err = json.Marshal(bl2)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2014-11-21 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// random insert/find access by several goroutines
|
2015-01-10 22:40:10 +00:00
|
|
|
func TestMapRandom(t *testing.T) {
|
2014-11-21 20:21:44 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
worker := func(bl *restic.Map) {
|
2014-11-21 20:21:44 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
b := newBlob()
|
|
|
|
bl.Insert(b)
|
|
|
|
|
|
|
|
for i := 0; i < 200; i++ {
|
|
|
|
bl.Insert(newBlob())
|
|
|
|
}
|
|
|
|
|
|
|
|
d := time.Duration(mrand.Intn(10)*100) * time.Millisecond
|
|
|
|
time.Sleep(d)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
b2, err := bl.Find(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Compare(b2) != 0 {
|
|
|
|
t.Fatalf("items are not equal: want %v, got %v", b, b2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
bl2 := restic.NewMap()
|
2014-11-21 20:21:44 +00:00
|
|
|
for i := 0; i < 200; i++ {
|
|
|
|
bl2.Insert(newBlob())
|
|
|
|
}
|
|
|
|
|
|
|
|
bl2.Merge(bl)
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
bl := restic.NewMap()
|
2014-11-21 20:21:44 +00:00
|
|
|
|
|
|
|
for i := 0; uint(i) < *maxWorkers; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go worker(bl)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|