2021-01-29 20:01:00 +00:00
|
|
|
package blockstore
|
2020-10-14 23:17:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-23 18:46:06 +00:00
|
|
|
"github.com/raulk/clock"
|
2020-10-14 23:17:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func TestTimedCacheBlockstoreSimple(t *testing.T) {
|
|
|
|
tc := NewTimedCacheBlockstore(10 * time.Millisecond)
|
2020-10-23 18:46:06 +00:00
|
|
|
mClock := clock.NewMock()
|
|
|
|
mClock.Set(time.Now())
|
|
|
|
tc.clock = mClock
|
|
|
|
tc.doneRotatingCh = make(chan struct{})
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-10-23 18:50:17 +00:00
|
|
|
_ = tc.Start(context.Background())
|
2020-10-23 18:46:06 +00:00
|
|
|
mClock.Add(1) // IDK why it is needed but it makes it work
|
|
|
|
|
2020-10-14 23:17:55 +00:00
|
|
|
defer func() {
|
|
|
|
_ = tc.Stop(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
b1 := blocks.NewBlock([]byte("foo"))
|
2021-11-19 01:50:25 +00:00
|
|
|
require.NoError(t, tc.Put(ctx, b1))
|
2020-10-14 23:17:55 +00:00
|
|
|
|
|
|
|
b2 := blocks.NewBlock([]byte("bar"))
|
2021-11-19 01:50:25 +00:00
|
|
|
require.NoError(t, tc.Put(ctx, b2))
|
2020-10-14 23:17:55 +00:00
|
|
|
|
|
|
|
b3 := blocks.NewBlock([]byte("baz"))
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
b1out, err := tc.Get(ctx, b1.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, b1.RawData(), b1out.RawData())
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err := tc.Has(ctx, b1.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
|
2020-10-23 18:46:06 +00:00
|
|
|
mClock.Add(10 * time.Millisecond)
|
|
|
|
<-tc.doneRotatingCh
|
2020-10-14 23:17:55 +00:00
|
|
|
|
|
|
|
// We should still have everything.
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err = tc.Has(ctx, b1.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err = tc.Has(ctx, b2.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
|
|
|
|
// extend b2, add b3.
|
2021-11-19 01:50:25 +00:00
|
|
|
require.NoError(t, tc.Put(ctx, b2))
|
|
|
|
require.NoError(t, tc.Put(ctx, b3))
|
2020-10-14 23:17:55 +00:00
|
|
|
|
|
|
|
// all keys once.
|
|
|
|
allKeys, err := tc.AllKeysChan(context.Background())
|
|
|
|
var ks []cid.Cid
|
|
|
|
for k := range allKeys {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, ks, []cid.Cid{b1.Cid(), b2.Cid(), b3.Cid()})
|
|
|
|
|
2020-10-23 18:46:06 +00:00
|
|
|
mClock.Add(10 * time.Millisecond)
|
|
|
|
<-tc.doneRotatingCh
|
2020-10-14 23:17:55 +00:00
|
|
|
// should still have b2, and b3, but not b1
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err = tc.Has(ctx, b1.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err = tc.Has(ctx, b2.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
has, err = tc.Has(ctx, b3.Cid())
|
2020-10-14 23:17:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, has)
|
|
|
|
}
|