Merge pull request #4561 from filecoin-project/fix/timedbs-flaky

Fix flaky TestTimedBSSimple
This commit is contained in:
Jakub Sztandera 2020-10-23 22:14:32 +02:00 committed by GitHub
commit 099bb2cbaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import (
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/raulk/clock"
"go.uber.org/multierr"
"github.com/filecoin-project/lotus/build"
@ -24,9 +25,10 @@ import (
type TimedCacheBS struct {
mu sync.RWMutex
active, inactive blockstore.MemStore
interval time.Duration
closeCh chan struct{}
clock clock.Clock
interval time.Duration
closeCh chan struct{}
doneRotatingCh chan struct{}
}
func NewTimedCacheBS(cacheTime time.Duration) *TimedCacheBS {
@ -34,6 +36,7 @@ func NewTimedCacheBS(cacheTime time.Duration) *TimedCacheBS {
active: blockstore.NewTemporary(),
inactive: blockstore.NewTemporary(),
interval: cacheTime,
clock: build.Clock,
}
}
@ -45,12 +48,15 @@ func (t *TimedCacheBS) Start(ctx context.Context) error {
}
t.closeCh = make(chan struct{})
go func() {
ticker := build.Clock.Ticker(t.interval)
ticker := t.clock.Ticker(t.interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.rotate()
if t.doneRotatingCh != nil {
t.doneRotatingCh <- struct{}{}
}
case <-t.closeCh:
return
}

View File

@ -1,21 +1,27 @@
package timedbs_test
package timedbs
import (
"context"
"testing"
"time"
"github.com/raulk/clock"
"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(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())
mClock.Add(1) // IDK why it is needed but it makes it work
defer func() {
_ = tc.Stop(context.Background())
}()
@ -36,7 +42,8 @@ func TestTimedBSSimple(t *testing.T) {
require.NoError(t, err)
require.True(t, has)
time.Sleep(15 * time.Millisecond)
mClock.Add(10 * time.Millisecond)
<-tc.doneRotatingCh
// We should still have everything.
has, err = tc.Has(b1.Cid())
@ -60,8 +67,8 @@ func TestTimedBSSimple(t *testing.T) {
require.NoError(t, err)
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
has, err = tc.Has(b1.Cid())