10000 fix: Input get request timeout issue by obalunenko · Pull Request #212 · obalunenko/advent-of-code · GitHub
[go: up one dir, main page]

Skip to content

fix: Input get request timeout issue #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"time"

"github.com/obalunenko/advent-of-code/internal/puzzles"
"github.com/obalunenko/advent-of-code/internal/puzzles/input"
)

// Run runs puzzle solving for passed year/day date.
func Run(ctx context.Context, year, day string) (puzzles.Result, error) {
const timeout = time.Second * 30

cli := input.NewFetcher(http.DefaultClient, timeout)

return run(ctx, cli, year, day)
}

func run(ctx context.Context, cli input.Fetcher, year, day string) (puzzles.Result, error) {
s, err := puzzles.GetSolver(year, day)
if err != nil {
return puzzles.Result{}, fmt.Errorf("failed to get solver: %w", err)
Expand All @@ -22,7 +32,7 @@ func Run(ctx context.Context, year, day string) (puzzles.Result, error) {
return puzzles.Result{}, fmt.Errorf("failed to make full name: %w", err)
}

asset, err := input.Get(ctx, input.Date{
asset, err := cli.Fetch(ctx, input.Date{
Year: year,
Day: day,
}, SessionFromContext(ctx))
Expand Down
8 changes: 4 additions & 4 deletions internal/command/command_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command_test
package command

import (
"context"
Expand All @@ -7,10 +7,10 @@ import (
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/obalunenko/advent-of-code/internal/command"
"github.com/obalunenko/advent-of-code/internal/puzzles"
"github.com/obalunenko/advent-of-code/internal/puzzles/input"
)
Expand Down Expand Up @@ -182,9 +182,9 @@ func TestRun(t *testing.T) {
tt := tests[i]

t.Run(tt.name, func(t *testing.T) {
input.Client = newMockHTTPClient(tt.returnParams)
cli := input.NewFetcher(newMockHTTPClient(tt.returnParams), time.Second*5)

got, err := command.Run(ctx, year, day)
got, err := run(ctx, cli, year, day)
if !tt.expected.wantErr(t, err) {
return
}
Expand Down
42 changes: 27 additions & 15 deletions internal/puzzles/input/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,46 @@ func (d Date) String() string {
return path.Join(d.Year, d.Day)
}

// ClientDo provides the interface for custom HTTP client implementations.
type ClientDo interface {
// IHTTPClient provides the interface for custom HTTP client implementations.
type IHTTPClient interface {
Do(*http.Request) (*http.Response, error)
}

// Client is the default Client and is used by Get, Head, and Post.
var Client ClientDo = http.DefaultClient
// Fetcher is an input get client.
type Fetcher interface {
Fetch(ctx context.Context, d Date, session string) ([]byte, error)
}

type client struct {
cli IHTTPClient
timeout time.Duration
}

// NewFetcher constructor for Fetcher.
func NewFetcher(c IHTTPClient, timeout time.Duration) Fetcher {
return &client{
cli: c,
timeout: timeout,
}
}

// Get returns puzzle input.
func Get(ctx context.Context, d Date, session string) ([]byte, error) {
// Fetch returns puzzle input.
func (c *client) Fetch(ctx context.Context, d Date, session string) ([]byte, error) {
req, err := createInputReq(ctx, d, session)
if err != nil {
return nil, fmt.Errorf("create input request: %w", err)
}

const (
timeoutSecs = 5
)
var cancel context.CancelFunc

ctx, cancel := context.WithTimeout(ctx, time.Second*timeoutSecs)
defer cancel()
if c.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, c.timeout)
defer cancel()
}

req = req.Clone(ctx)

resp, err := Client.Do(req)
resp, err := c.cli.Do(req)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
Expand Down Expand Up @@ -124,8 +139,5 @@ func createInputReq(ctx context.Context, d Date, sessionID string) (*http.Reques
Unparsed: nil,
})

req.Header.Set("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36")

return req, nil
}
27 changes: 11 additions & 16 deletions internal/puzzles/input/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"
"testing/iotest"
"time"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -56,14 +57,8 @@ func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
}

func TestGet(t *testing.T) {
prevCli := input.Client

t.Cleanup(func() {
input.Client = prevCli
})

type client struct {
input.ClientDo
input.IHTTPClient
}

type args struct {
Expand All @@ -82,7 +77,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusOK,
body: io.NopCloser(strings.NewReader("test")),
}),
Expand All @@ -101,7 +96,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusOK,
body: io.NopCloser(strings.NewReader("")),
}),
Expand All @@ -120,7 +115,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusNotFound,
body: http.NoBody,
}),
Expand All @@ -139,7 +134,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusBadRequest,
body: io.NopCloser(strings.NewReader("no session")),
}),
Expand All @@ -158,7 +153,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusInternalServerError,
body: io.NopCloser(strings.NewReader("no session")),
}),
Expand All @@ -177,7 +172,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: &mockHTTPClient{
IHTTPClient: &mockHTTPClient{
MockDo: func(req *http.Request) (*http.Response, error) {
return &http.Response{}, errors.New("error in test")
},
Expand All @@ -197,7 +192,7 @@ func TestGet(t *testing.T) {
{
name: "",
client: client{
ClientDo: newMockHTTPClient(returnParams{
IHTTPClient: newMockHTTPClient(returnParams{
status: http.StatusOK,
body: io.NopCloser(iotest.ErrReader(errors.New("custom error"))),
}),
Expand All @@ -217,9 +212,9 @@ func TestGet(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input.Client = tt.client
cli := input.NewFetcher(tt.client, time.Second*5)

got, err := input.Get(tt.args.ctx, tt.args.d, tt.args.session)
got, err := cli.Fetch(tt.args.ctx, tt.args.d, tt.args.session)
if !tt.wantErr(t, err) {
return
}
Expand Down
0