Make TestTimeout less flaky (#863)
This test currently fails on occasion due to what appears to be goroutine scheduling differences. This should eliminate those failures.
This commit is contained in:
parent
7586a3cbe8
commit
65b2aa0a63
|
@ -485,49 +485,34 @@ func TestTimeout(t *testing.T) {
|
||||||
m := new(Msg)
|
m := new(Msg)
|
||||||
m.SetQuestion("miek.nl.", TypeTXT)
|
m.SetQuestion("miek.nl.", TypeTXT)
|
||||||
|
|
||||||
// Use a channel + timeout to ensure we don't get stuck if the
|
runTest := func(name string, exchange func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error)) {
|
||||||
// Client Timeout is not working properly
|
t.Run(name, func(t *testing.T) {
|
||||||
done := make(chan struct{}, 2)
|
start := time.Now()
|
||||||
|
|
||||||
timeout := time.Millisecond
|
timeout := time.Millisecond
|
||||||
allowable := timeout + 10*time.Millisecond
|
allowable := timeout + 10*time.Millisecond
|
||||||
abortAfter := timeout + 100*time.Millisecond
|
|
||||||
|
|
||||||
start := time.Now()
|
_, _, err := exchange(m, addrstr, timeout)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("no timeout using Client.%s", name)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
length := time.Since(start)
|
||||||
|
if length > allowable {
|
||||||
|
t.Errorf("exchange took longer %v than specified Timeout %v", length, allowable)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
runTest("Exchange", func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error) {
|
||||||
c := &Client{Timeout: timeout}
|
c := &Client{Timeout: timeout}
|
||||||
_, _, err := c.Exchange(m, addrstr)
|
return c.Exchange(m, addr)
|
||||||
if err == nil {
|
})
|
||||||
t.Error("no timeout using Client.Exchange")
|
runTest("ExchangeContext", func(m *Msg, addr string, timeout time.Duration) (*Msg, time.Duration, error) {
|
||||||
}
|
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
c := &Client{}
|
|
||||||
_, _, err := c.ExchangeContext(ctx, m, addrstr)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("no timeout using Client.ExchangeContext")
|
|
||||||
}
|
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Wait for both the Exchange and ExchangeContext tests to be done.
|
return new(Client).ExchangeContext(ctx, m, addrstr)
|
||||||
for i := 0; i < 2; i++ {
|
})
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(abortAfter):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
length := time.Since(start)
|
|
||||||
|
|
||||||
if length > allowable {
|
|
||||||
t.Errorf("exchange took longer %v than specified Timeout %v", length, allowable)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that responses from deduplicated requests aren't shared between callers
|
// Check that responses from deduplicated requests aren't shared between callers
|
||||||
|
|
Loading…
Reference in New Issue