Merge pull request #50 from 178inaba/streaming

Optimize streaming
pull/55/head
mattn 2017-05-01 09:12:15 +09:00 committed by GitHub
commit 99692bee8a
3 changed files with 281 additions and 42 deletions

View File

@ -80,7 +80,6 @@ func cmdStream(c *cli.Context) error {
go func() { go func() {
<-sc <-sc
cancel() cancel()
close(q)
}() }()
c.App.Metadata["signal"] = sc c.App.Metadata["signal"] = sc

View File

@ -8,8 +8,8 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"strconv"
"strings" "strings"
"time"
) )
// UpdateEvent is struct for passing status event to app. // UpdateEvent is struct for passing status event to app.
@ -42,8 +42,8 @@ type Event interface {
event() event()
} }
func handleReader(ctx context.Context, q chan Event, r io.Reader) error { func handleReader(q chan Event, r io.Reader) error {
name := "" var name string
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
for s.Scan() { for s.Scan() {
line := s.Text() line := s.Text()
@ -55,30 +55,33 @@ func handleReader(ctx context.Context, q chan Event, r io.Reader) error {
case "event": case "event":
name = strings.TrimSpace(token[1]) name = strings.TrimSpace(token[1])
case "data": case "data":
var err error
switch name { switch name {
case "update": case "update":
var status Status var status Status
err := json.Unmarshal([]byte(token[1]), &status) err = json.Unmarshal([]byte(token[1]), &status)
if err == nil { if err == nil {
q <- &UpdateEvent{&status} q <- &UpdateEvent{&status}
} }
case "notification": case "notification":
var notification Notification var notification Notification
err := json.Unmarshal([]byte(token[1]), &notification) err = json.Unmarshal([]byte(token[1]), &notification)
if err == nil { if err == nil {
q <- &NotificationEvent{&notification} q <- &NotificationEvent{&notification}
} }
case "delete": case "delete":
var id int64 var id int64
err := json.Unmarshal([]byte(token[1]), &id) id, err = strconv.ParseInt(strings.TrimSpace(token[1]), 10, 64)
if err == nil { if err == nil {
q <- &DeleteEvent{id} q <- &DeleteEvent{id}
} }
} }
default: if err != nil {
q <- &ErrorEvent{err}
} }
} }
return ctx.Err() }
return s.Err()
} }
func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) { func (c *Client) streaming(ctx context.Context, p string, params url.Values) (chan Event, error) {
@ -96,38 +99,42 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
req = req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
var resp *http.Response q := make(chan Event)
q := make(chan Event, 10)
go func() { go func() {
defer ctx.Done() defer close(q)
for { for {
resp, err = c.Do(req) select {
if resp != nil && resp.StatusCode != http.StatusOK { case <-ctx.Done():
err = parseAPIError("bad request", resp) q <- &ErrorEvent{ctx.Err()}
return
default:
} }
if err == nil {
err = handleReader(ctx, q, resp.Body) c.doStreaming(req, q)
if err == nil {
break
}
} else {
q <- &ErrorEvent{err}
}
resp.Body.Close()
time.Sleep(3 * time.Second)
}
}()
go func() {
<-ctx.Done()
if resp != nil && resp.Body != nil {
resp.Body.Close()
} }
}() }()
return q, nil return q, nil
} }
func (c *Client) doStreaming(req *http.Request, q chan Event) {
resp, err := c.Do(req)
if err != nil {
q <- &ErrorEvent{err}
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
q <- &ErrorEvent{parseAPIError("bad request", resp)}
return
}
err = handleReader(q, resp.Body)
if err != nil {
q <- &ErrorEvent{err}
}
}
// StreamingUser return channel to read events on home. // StreamingUser return channel to read events on home.
func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) { func (c *Client) StreamingUser(ctx context.Context) (chan Event, error) {
return c.streaming(ctx, "user", nil) return c.streaming(ctx, "user", nil)

View File

@ -5,13 +5,207 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"time" "time"
) )
func TestStreamingPublic(t *testing.T) { func TestHandleReader(t *testing.T) {
q := make(chan Event)
r := strings.NewReader(`
event: update
data: {content: error}
event: update
data: {"content": "foo"}
event: notification
data: {"type": "mention"}
event: delete
data: 1234567
:thump
`)
go func() {
defer close(q)
err := handleReader(q, r)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
var passUpdate, passNotification, passDelete, passError bool
for e := range q {
switch event := e.(type) {
case *UpdateEvent:
passUpdate = true
if event.Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", event.Status.Content)
}
case *NotificationEvent:
passNotification = true
if event.Notification.Type != "mention" {
t.Fatalf("want %q but %q", "mention", event.Notification.Type)
}
case *DeleteEvent:
passDelete = true
if event.ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, event.ID)
}
case *ErrorEvent:
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
}
}
if !passUpdate || !passNotification || !passDelete || !passError {
t.Fatalf("have not passed through somewhere: "+
"update %t, notification %t, delete %t, error %t",
passUpdate, passNotification, passDelete, passError)
}
}
func TestStreaming(t *testing.T) {
var isEnd bool
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/streaming/public" { if isEnd {
return
} else if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
f := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
c := NewClient(&Config{Server: ":"})
_, err := c.streaming(context.Background(), "", nil)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
c = NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := c.streaming(ctx, "", nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
var cnt int
var passError, passUpdate bool
for e := range q {
switch event := e.(type) {
case *ErrorEvent:
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
case *UpdateEvent:
cnt++
passUpdate = true
if event.Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", event.Status.Content)
}
}
}
if cnt != 1 {
t.Fatalf("result should be one: %d", cnt)
}
if !passError || !passUpdate {
t.Fatalf("have not passed through somewhere: error %t, update %t", passError, passUpdate)
}
}
func TestDoStreaming(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.(http.Flusher).Flush()
time.Sleep(time.Second)
}))
defer ts.Close()
c := NewClient(&Config{Server: ts.URL})
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Millisecond, cancel)
req = req.WithContext(ctx)
q := make(chan Event)
go func() {
defer close(q)
c.doStreaming(req, q)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
var passError bool
for e := range q {
if event, ok := e.(*ErrorEvent); ok {
passError = true
if event.err == nil {
t.Fatalf("should be fail: %v", event.err)
}
}
}
if !passError {
t.Fatalf("have not passed through: error %t", passError)
}
}
func TestStreamingUser(t *testing.T) {
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/user" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
c := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := c.StreamingUser(ctx)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
}
func TestStreamingPublic(t *testing.T) {
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/public/local" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} }
@ -27,7 +221,7 @@ event: update
data: {"content": "bar"} data: {"content": "bar"}
`) `)
f.Flush() f.Flush()
return isEnd = true
})) }))
defer ts.Close() defer ts.Close()
@ -38,18 +232,17 @@ data: {"content": "bar"}
AccessToken: "zoo", AccessToken: "zoo",
}) })
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
q, err := client.StreamingPublic(ctx, false) q, err := client.StreamingPublic(ctx, true)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
time.AfterFunc(3*time.Second, func() { time.AfterFunc(time.Second, cancel)
cancel()
close(q)
})
events := []Event{} events := []Event{}
for e := range q { for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e) events = append(events, e)
} }
}
if len(events) != 2 { if len(events) != 2 {
t.Fatalf("result should be two: %d", len(events)) t.Fatalf("result should be two: %d", len(events))
} }
@ -60,3 +253,43 @@ data: {"content": "bar"}
t.Fatalf("want %q but %q", "bar", events[1].(*UpdateEvent).Status.Content) t.Fatalf("want %q but %q", "bar", events[1].(*UpdateEvent).Status.Content)
} }
} }
func TestStreamingHashtag(t *testing.T) {
var isEnd bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isEnd {
return
} else if r.URL.Path != "/api/v1/streaming/hashtag/local" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
f, _ := w.(http.Flusher)
fmt.Fprintln(w, `
event: update
data: {"content": "foo"}
`)
f.Flush()
isEnd = true
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)
q, err := client.StreamingHashtag(ctx, "hashtag", true)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
events := []Event{}
for e := range q {
if _, ok := e.(*ErrorEvent); !ok {
events = append(events, e)
}
}
if len(events) != 1 {
t.Fatalf("result should be one: %d", len(events))
}
if events[0].(*UpdateEvent).Status.Content != "foo" {
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
}
}