Skip to content

Commit

Permalink
Merge pull request #4561 from filecoin-project/fix/timedbs-flaky
Browse files Browse the repository at this point in the history
Fix flaky TestTimedBSSimple
  • Loading branch information
Jakub Sztandera authored Oct 23, 2020
2 parents e395559 + 9fbd2a5 commit 099bb2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/timedbs/timedbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,16 +25,18 @@ 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 {
return &TimedCacheBS{
active: blockstore.NewTemporary(),
inactive: blockstore.NewTemporary(),
interval: cacheTime,
clock: build.Clock,
}
}

Expand All @@ -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
}
Expand Down
21 changes: 14 additions & 7 deletions lib/timedbs/timedbs_test.go
Original file line number Diff line number Diff line change
@@ -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())
}()
Expand All @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 099bb2c

Please sign in to comment.