Skip to content

Commit

Permalink
fix flaky TestGCDelay (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 12, 2022
1 parent 9161f4a commit 6d53e63
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions p2p/host/peerstore/pstoreds/addr_book_gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"time"

mockClock "github.com/benbjohnson/clock"
query "github.com/ipfs/go-datastore/query"
"github.com/ipfs/go-datastore/query"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
test "github.com/libp2p/go-libp2p-peerstore/test"
"github.com/libp2p/go-libp2p-peerstore/test"
ma "github.com/multiformats/go-multiaddr"

"github.com/stretchr/testify/require"
)

var lookaheadQuery = query.Query{Prefix: gcLookaheadBase.String(), KeysOnly: true}
Expand All @@ -31,6 +33,7 @@ func (tp *testProbe) countLookaheadEntries() (i int) {
}
return i
}

func (tp *testProbe) clearCache() {
for _, k := range tp.ab.(*dsAddrBook).cache.Keys() {
tp.ab.(*dsAddrBook).cache.Remove(k)
Expand Down Expand Up @@ -155,32 +158,31 @@ func TestGCDelay(t *testing.T) {
ids := test.GeneratePeerIDs(10)
addrs := test.GenerateAddrs(100)

clk := mockClock.NewMock()
opts := DefaultOpts()

opts.GCInitialDelay = 2 * time.Second
opts.GCLookaheadInterval = 1 * time.Minute
opts.GCPurgeInterval = 30 * time.Second
clk := mockClock.NewMock()
opts.Clock = clk

factory := addressBookFactory(t, leveldbStore, opts)
ab, closeFn := factory()
defer closeFn()
// give the background Go routine some time to start
time.Sleep(100 * time.Millisecond)

tp := &testProbe{t, ab}

ab.AddAddrs(ids[0], addrs, 1*time.Second)

// immediately after we should be having no lookahead entries.
if i := tp.countLookaheadEntries(); i != 0 {
t.Errorf("expected no lookahead entries, got: %d", i)
t.Fatalf("expected no lookahead entries, got: %d", i)
}

// after the initial delay has passed.
clk.Add(3 * time.Second)
if i := tp.countLookaheadEntries(); i != 1 {
t.Errorf("expected 1 lookahead entry, got: %d", i)
}
require.Eventually(t, func() bool { return tp.countLookaheadEntries() == 1 }, 3000*time.Millisecond, 10*time.Millisecond, "expected 1 lookahead entry")
}

func TestGCLookaheadDisabled(t *testing.T) {
Expand Down

0 comments on commit 6d53e63

Please sign in to comment.