Fix flaky TestTimedBSSimple

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-10-23 20:46:06 +02:00
parent 8124fe98c7
commit ea69386519
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 25 additions and 12 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.Start(context.Background())
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())