Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky TTL test #18

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
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
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