Merge pull request #4561 from filecoin-project/fix/timedbs-flaky
Fix flaky TestTimedBSSimple
This commit is contained in:
commit
099bb2cbaa
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
blocks "github.com/ipfs/go-block-format"
|
blocks "github.com/ipfs/go-block-format"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
|
"github.com/raulk/clock"
|
||||||
"go.uber.org/multierr"
|
"go.uber.org/multierr"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/build"
|
"github.com/filecoin-project/lotus/build"
|
||||||
@ -24,9 +25,10 @@ import (
|
|||||||
type TimedCacheBS struct {
|
type TimedCacheBS struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
active, inactive blockstore.MemStore
|
active, inactive blockstore.MemStore
|
||||||
|
clock clock.Clock
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
closeCh chan struct{}
|
closeCh chan struct{}
|
||||||
|
doneRotatingCh chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimedCacheBS(cacheTime time.Duration) *TimedCacheBS {
|
func NewTimedCacheBS(cacheTime time.Duration) *TimedCacheBS {
|
||||||
@ -34,6 +36,7 @@ func NewTimedCacheBS(cacheTime time.Duration) *TimedCacheBS {
|
|||||||
active: blockstore.NewTemporary(),
|
active: blockstore.NewTemporary(),
|
||||||
inactive: blockstore.NewTemporary(),
|
inactive: blockstore.NewTemporary(),
|
||||||
interval: cacheTime,
|
interval: cacheTime,
|
||||||
|
clock: build.Clock,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,12 +48,15 @@ func (t *TimedCacheBS) Start(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
t.closeCh = make(chan struct{})
|
t.closeCh = make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
ticker := build.Clock.Ticker(t.interval)
|
ticker := t.clock.Ticker(t.interval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
t.rotate()
|
t.rotate()
|
||||||
|
if t.doneRotatingCh != nil {
|
||||||
|
t.doneRotatingCh <- struct{}{}
|
||||||
|
}
|
||||||
case <-t.closeCh:
|
case <-t.closeCh:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,27 @@
|
|||||||
package timedbs_test
|
package timedbs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/raulk/clock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
blocks "github.com/ipfs/go-block-format"
|
blocks "github.com/ipfs/go-block-format"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/lib/timedbs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTimedBSSimple(t *testing.T) {
|
func TestTimedBSSimple(t *testing.T) {
|
||||||
tc := timedbs.NewTimedCacheBS(10 * time.Millisecond)
|
tc := NewTimedCacheBS(10 * time.Millisecond)
|
||||||
|
mClock := clock.NewMock()
|
||||||
|
mClock.Set(time.Now())
|
||||||
|
tc.clock = mClock
|
||||||
|
tc.doneRotatingCh = make(chan struct{})
|
||||||
|
|
||||||
_ = tc.Start(context.Background())
|
_ = tc.Start(context.Background())
|
||||||
|
mClock.Add(1) // IDK why it is needed but it makes it work
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = tc.Stop(context.Background())
|
_ = tc.Stop(context.Background())
|
||||||
}()
|
}()
|
||||||
@ -36,7 +42,8 @@ func TestTimedBSSimple(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, has)
|
require.True(t, has)
|
||||||
|
|
||||||
time.Sleep(15 * time.Millisecond)
|
mClock.Add(10 * time.Millisecond)
|
||||||
|
<-tc.doneRotatingCh
|
||||||
|
|
||||||
// We should still have everything.
|
// We should still have everything.
|
||||||
has, err = tc.Has(b1.Cid())
|
has, err = tc.Has(b1.Cid())
|
||||||
@ -60,8 +67,8 @@ func TestTimedBSSimple(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.ElementsMatch(t, ks, []cid.Cid{b1.Cid(), b2.Cid(), b3.Cid()})
|
require.ElementsMatch(t, ks, []cid.Cid{b1.Cid(), b2.Cid(), b3.Cid()})
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
mClock.Add(10 * time.Millisecond)
|
||||||
|
<-tc.doneRotatingCh
|
||||||
// should still have b2, and b3, but not b1
|
// should still have b2, and b3, but not b1
|
||||||
|
|
||||||
has, err = tc.Has(b1.Cid())
|
has, err = tc.Has(b1.Cid())
|
||||||
|
Loading…
Reference in New Issue
Block a user