Skip to content

Commit

Permalink
Avoid use of error string matching in test case
Browse files Browse the repository at this point in the history
  • Loading branch information
AGWA committed Oct 13, 2020
1 parent aad691a commit d049866
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dns
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"strconv"
Expand Down Expand Up @@ -162,6 +163,12 @@ func TestClientTLSSyncV4(t *testing.T) {
}
}

func isNetworkTimeout(err error) bool {
// TODO: when Go 1.14 support is dropped, do this: https://golang.org/doc/go1.15#net
var netError net.Error
return errors.As(err, &netError) && netError.Timeout()
}

func TestClientSyncBadID(t *testing.T) {
HandleFunc("miek.nl.", HelloServerBadID)
defer HandleRemove("miek.nl.")
Expand All @@ -178,11 +185,11 @@ func TestClientSyncBadID(t *testing.T) {
c := &Client{
Timeout: 1 * time.Second,
}
if _, _, err := c.Exchange(m, addrstr); err == nil || !strings.Contains(err.Error(), "timeout") {
if _, _, err := c.Exchange(m, addrstr); err == nil || !isNetworkTimeout(err) {
t.Errorf("query did not time out")
}
// And now with plain Exchange().
if _, err = Exchange(m, addrstr); err == nil || !strings.Contains(err.Error(), "timeout") {
if _, err = Exchange(m, addrstr); err == nil || !isNetworkTimeout(err) {
t.Errorf("query did not time out")
}
}
Expand Down

0 comments on commit d049866

Please sign in to comment.