Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

fix flaky TestGCDelay #206

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions 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