-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timeout on github client (#1290)
* add timeout on github client * implement linter recommendation
- Loading branch information
1 parent
f6adffb
commit e672f4c
Showing
2 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-github/v48/github" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGithubAPITimeout(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(5 * time.Second) // sleeping and doing nothing | ||
})) | ||
defer ts.Close() | ||
githubURL, err := url.Parse(fmt.Sprintf("%s/", ts.URL)) | ||
assert.NoError(t, err) | ||
|
||
githubClient := github.NewClient(&http.Client{Timeout: 1 * time.Second}) // client side timeout should be less than server side sleep defined above | ||
githubClient.BaseURL = githubURL | ||
|
||
start := time.Now() | ||
release, err := getLatestRelease(githubClient, "test", "test") | ||
elapsed := time.Since(start) | ||
// assert time to get a response from the function is only slightly greater than client timeout | ||
assert.GreaterOrEqual(t, elapsed, 1*time.Second) | ||
assert.Less(t, elapsed, 2*time.Second) | ||
// assert error returned is related to client timeout | ||
assert.Nil(t, release) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "context deadline exceeded") | ||
} |