2021-03-02 17:03:11 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b0 = blocks.NewBlock([]byte("abc"))
|
|
|
|
b1 = blocks.NewBlock([]byte("foo"))
|
|
|
|
b2 = blocks.NewBlock([]byte("bar"))
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnionBlockstore_Get(t *testing.T) {
|
2021-11-19 01:50:25 +00:00
|
|
|
ctx := context.Background()
|
2021-03-02 17:03:11 +00:00
|
|
|
m1 := NewMemory()
|
|
|
|
m2 := NewMemory()
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
_ = m1.Put(ctx, b1)
|
|
|
|
_ = m2.Put(ctx, b2)
|
2021-03-02 17:03:11 +00:00
|
|
|
|
|
|
|
u := Union(m1, m2)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
v1, err := u.Get(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.RawData(), v1.RawData())
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
v2, err := u.Get(ctx, b2.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b2.RawData(), v2.RawData())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnionBlockstore_Put_PutMany_Delete_AllKeysChan(t *testing.T) {
|
2021-11-19 01:50:25 +00:00
|
|
|
ctx := context.Background()
|
2021-03-02 17:03:11 +00:00
|
|
|
m1 := NewMemory()
|
|
|
|
m2 := NewMemory()
|
|
|
|
|
|
|
|
u := Union(m1, m2)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
err := u.Put(ctx, b0)
|
2021-03-02 17:03:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var has bool
|
|
|
|
|
|
|
|
// write was broadcasted to all stores.
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m1.Has(ctx, b0.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m2.Has(ctx, b0.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = u.Has(ctx, b0.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
|
|
|
// put many.
|
2021-11-19 01:50:25 +00:00
|
|
|
err = u.PutMany(ctx, []blocks.Block{b1, b2})
|
2021-03-02 17:03:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// write was broadcasted to all stores.
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m1.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m1.Has(ctx, b2.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m2.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m2.Has(ctx, b2.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
|
|
|
// also in the union store.
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = u.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = u.Has(ctx, b2.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.True(t, has)
|
|
|
|
|
|
|
|
// deleted from all stores.
|
2021-11-19 01:50:25 +00:00
|
|
|
err = u.DeleteBlock(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = u.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.False(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m1.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.False(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, _ = m2.Has(ctx, b1.Cid())
|
2021-03-02 17:03:11 +00:00
|
|
|
require.False(t, has)
|
|
|
|
|
|
|
|
// check that AllKeysChan returns b0 and b2, twice (once per backing store)
|
|
|
|
ch, err := u.AllKeysChan(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var i int
|
|
|
|
for range ch {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
require.Equal(t, 4, i)
|
|
|
|
}
|