Skip to content

Commit

Permalink
Merge pull request #45 from jafayer/jafayer/fix-dialer-timeout
Browse files Browse the repository at this point in the history
Fix SetTimeout not changing the timeout in the underlying Dialer
  • Loading branch information
likexian authored Sep 2, 2024
2 parents 80bf76f + a561183 commit 259e6b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type Client struct {
disableReferral bool
}

type hasTimeout struct {
Timeout time.Duration
proxy.Dialer
}

// Version returns package version
func Version() string {
return "1.15.4"
Expand Down Expand Up @@ -91,6 +96,9 @@ func (c *Client) SetDialer(dialer proxy.Dialer) *Client {

// SetTimeout set query timeout
func (c *Client) SetTimeout(timeout time.Duration) *Client {
if d, ok := c.dialer.(*hasTimeout); ok {
d.Timeout = timeout
}
c.timeout = timeout
return c
}
Expand Down
12 changes: 12 additions & 0 deletions whois_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ func TestWhoisFail(t *testing.T) {
assert.NotNil(t, err)
}

func TestWhoisTimeout(t *testing.T) {
client := NewClient()
client.SetTimeout(1 * time.Millisecond)
_, err := client.Whois("google.com")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "timeout")

client.SetTimeout(10 * time.Second)
_, err = client.Whois("google.com")
assert.Nil(t, err)
}

func TestWhois(t *testing.T) {
tests := []string{
"com",
Expand Down

0 comments on commit 259e6b0

Please sign in to comment.