Skip to content

Commit

Permalink
Merge pull request #18 from libp2p/fix-flaky-ttl-test
Browse files Browse the repository at this point in the history
fix flaky TTL test
  • Loading branch information
marten-seemann authored Sep 27, 2021
2 parents f9db021 + afe3b0d commit 25408a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
IPv4AndIPv6 = IPv4 | IPv6 // default option
)

var initialQueryInterval = 4 * time.Second

// Client structure encapsulates both IPv4/IPv6 UDP connections.
type client struct {
ipv4conn *ipv4.PacketConn
Expand Down Expand Up @@ -371,7 +373,7 @@ func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error
}

const maxInterval = 60 * time.Second
interval := 4 * time.Second
interval := initialQueryInterval
timer := time.NewTimer(interval)
defer timer.Stop()
for {
Expand Down
13 changes: 8 additions & 5 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,14 @@ func TestSubtype(t *testing.T) {
t.Run("ttl", func(t *testing.T) {
origTTL := defaultTTL
origCleanupFreq := cleanupFreq
defer func() {
origInitialQueryInterval := initialQueryInterval
t.Cleanup(func() {
defaultTTL = origTTL
cleanupFreq = origCleanupFreq
}()
defaultTTL = 2 // 2 seconds
initialQueryInterval = origInitialQueryInterval
})
defaultTTL = 1 // 1 second
initialQueryInterval = 100 * time.Millisecond
cleanupFreq = 100 * time.Millisecond

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand All @@ -178,8 +181,8 @@ func TestSubtype(t *testing.T) {
}

<-ctx.Done()
if len(entries) != 2 {
t.Fatalf("Expected to have received 2 entries, but got %d", len(entries))
if len(entries) < 2 {
t.Fatalf("Expected to have received at least 2 entries, but got %d", len(entries))
}
res1 := <-entries
res2 := <-entries
Expand Down

0 comments on commit 25408a8

Please sign in to comment.