Skip to content

Commit

Permalink
fix flaky TTL test
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 22, 2021
1 parent 9847be4 commit b91e04c
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 @@ -143,11 +143,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 @@ -160,8 +163,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 b91e04c

Please sign in to comment.