Skip to content

Commit

Permalink
Add Context
Browse files Browse the repository at this point in the history
  • Loading branch information
k2wanko committed Jun 19, 2016
1 parent 8635ae8 commit 101c0f4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
21 changes: 20 additions & 1 deletion linebot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"net/http"
"net/url"
"strconv"
"sync"

"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
)

// errors
Expand All @@ -25,6 +29,8 @@ type Client struct {
mid string
endpointBase string // default APIEndpointBaseTrial
httpClient *http.Client // default http.DefaultClient
mu sync.Mutex
ctx context.Context
}

// ClientOption type
Expand Down Expand Up @@ -64,6 +70,15 @@ func WithEndpointBase(endpointBase string) ClientOption {
}
}

// Context function which carried deadlines and cancel signal between processes.
// See http://blog.golang.org/context
func (client *Client) Context(ctx context.Context) *Client {
client.mu.Lock()
client.ctx = ctx
client.mu.Unlock()
return client
}

func (client *Client) sendSingleMessage(to []string, content SingleMessageContent) (result *ResponseContent, err error) {
message := SingleMessage{
To: to,
Expand Down Expand Up @@ -144,7 +159,11 @@ func (client *Client) do(req *http.Request) (res *http.Response, err error) {
req.Header.Set("X-Line-ChannelID", strconv.FormatInt(client.channelID, 10))
req.Header.Set("X-Line-ChannelSecret", client.channelSecret)
req.Header.Set("X-Line-Trusted-User-With-ACL", client.mid)
res, err = client.httpClient.Do(req)
if ctx := client.ctx; ctx == nil {
res, err = client.httpClient.Do(req)
} else {
res, err = ctxhttp.Do(ctx, client.httpClient, req)
}
return
}

Expand Down
27 changes: 27 additions & 0 deletions linebot/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"crypto/tls"
"net/http"
"net/http/httptest"
"testing"
"time"

"golang.org/x/net/context"
)

func mockClient(server *httptest.Server) (*Client, error) {
Expand All @@ -25,3 +29,26 @@ func mockClient(server *httptest.Server) (*Client, error) {
}
return client, nil
}

func TestRequestTimeout(t *testing.T) {
requestDuration := 100 * time.Millisecond
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(requestDuration)
w.Write([]byte("{}"))
}))
defer srv.Close()
c, err := mockClient(srv)
if err != nil {
t.Fatalf("mockClient error: %v", err)
}

ctx, _ := context.WithTimeout(context.Background(), requestDuration/2)

res, err := c.Context(ctx).SendText([]string{"DUMMY_MID"}, "hello!")
if res != nil || err == nil {
t.Fatalf("expected error, didn't get one. res: %v", res)
}
if err != ctx.Err() {
t.Fatalf("expected error from context bud got: %v", err)
}
}

0 comments on commit 101c0f4

Please sign in to comment.