79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package timedbs_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/filecoin-project/lotus/lib/timedbs"
|
|
)
|
|
|
|
func TestTimedBSSimple(t *testing.T) {
|
|
tc := timedbs.NewTimedCacheBS(3 * time.Millisecond)
|
|
_ = tc.Start(context.Background())
|
|
defer func() {
|
|
_ = tc.Stop(context.Background())
|
|
}()
|
|
|
|
b1 := blocks.NewBlock([]byte("foo"))
|
|
require.NoError(t, tc.Put(b1))
|
|
|
|
b2 := blocks.NewBlock([]byte("bar"))
|
|
require.NoError(t, tc.Put(b2))
|
|
|
|
b3 := blocks.NewBlock([]byte("baz"))
|
|
|
|
b1out, err := tc.Get(b1.Cid())
|
|
require.NoError(t, err)
|
|
require.Equal(t, b1.RawData(), b1out.RawData())
|
|
|
|
has, err := tc.Has(b1.Cid())
|
|
require.NoError(t, err)
|
|
require.True(t, has)
|
|
|
|
time.Sleep(4 * time.Millisecond)
|
|
|
|
// We should still have everything.
|
|
has, err = tc.Has(b1.Cid())
|
|
require.NoError(t, err)
|
|
require.True(t, has)
|
|
|
|
has, err = tc.Has(b2.Cid())
|
|
require.NoError(t, err)
|
|
require.True(t, has)
|
|
|
|
// extend b2, add b3.
|
|
require.NoError(t, tc.Put(b2))
|
|
require.NoError(t, tc.Put(b3))
|
|
|
|
// 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()})
|
|
|
|
time.Sleep(4 * time.Millisecond)
|
|
|
|
// should still have b2, and b3, but not b1
|
|
|
|
has, err = tc.Has(b1.Cid())
|
|
require.NoError(t, err)
|
|
require.False(t, has)
|
|
|
|
has, err = tc.Has(b2.Cid())
|
|
require.NoError(t, err)
|
|
require.True(t, has)
|
|
|
|
has, err = tc.Has(b3.Cid())
|
|
require.NoError(t, err)
|
|
require.True(t, has)
|
|
}
|