Skip to content

Commit afe3b0d

Browse files
fix flaky TTL test
1 parent a3c4995 commit afe3b0d

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const (
2626
IPv4AndIPv6 = IPv4 | IPv6 // default option
2727
)
2828

29+
var initialQueryInterval = 4 * time.Second
30+
2931
// Client structure encapsulates both IPv4/IPv6 UDP connections.
3032
type client struct {
3133
ipv4conn *ipv4.PacketConn
@@ -371,7 +373,7 @@ func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error
371373
}
372374

373375
const maxInterval = 60 * time.Second
374-
interval := 4 * time.Second
376+
interval := initialQueryInterval
375377
timer := time.NewTimer(interval)
376378
defer timer.Stop()
377379
for {

service_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ func TestSubtype(t *testing.T) {
143143
t.Run("ttl", func(t *testing.T) {
144144
origTTL := defaultTTL
145145
origCleanupFreq := cleanupFreq
146-
defer func() {
146+
origInitialQueryInterval := initialQueryInterval
147+
t.Cleanup(func() {
147148
defaultTTL = origTTL
148149
cleanupFreq = origCleanupFreq
149-
}()
150-
defaultTTL = 2 // 2 seconds
150+
initialQueryInterval = origInitialQueryInterval
151+
})
152+
defaultTTL = 1 // 1 second
153+
initialQueryInterval = 100 * time.Millisecond
151154
cleanupFreq = 100 * time.Millisecond
152155

153156
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@@ -160,8 +163,8 @@ func TestSubtype(t *testing.T) {
160163
}
161164

162165
<-ctx.Done()
163-
if len(entries) != 2 {
164-
t.Fatalf("Expected to have received 2 entries, but got %d", len(entries))
166+
if len(entries) < 2 {
167+
t.Fatalf("Expected to have received at least 2 entries, but got %d", len(entries))
165168
}
166169
res1 := <-entries
167170
res2 := <-entries

0 commit comments

Comments
 (0)