Add TestDoStreaming
parent
088cb0b74d
commit
9b39dd033f
|
@ -63,9 +63,12 @@ data: 1234567
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStreaming(t *testing.T) {
|
func TestStreaming(t *testing.T) {
|
||||||
|
var isEnd bool
|
||||||
canErr := true
|
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 canErr {
|
if isEnd {
|
||||||
|
return
|
||||||
|
} else if canErr {
|
||||||
canErr = false
|
canErr = false
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -76,6 +79,7 @@ event: update
|
||||||
data: {"content": "foo"}
|
data: {"content": "foo"}
|
||||||
`)
|
`)
|
||||||
f.Flush()
|
f.Flush()
|
||||||
|
isEnd = true
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
@ -87,13 +91,12 @@ data: {"content": "foo"}
|
||||||
|
|
||||||
c = NewClient(&Config{Server: ts.URL})
|
c = NewClient(&Config{Server: ts.URL})
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
time.AfterFunc(time.Second, func() {
|
time.AfterFunc(time.Second, cancel)
|
||||||
cancel()
|
|
||||||
})
|
|
||||||
q, err := c.streaming(ctx, "", nil)
|
q, err := c.streaming(ctx, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("should not be fail: %v", err)
|
t.Fatalf("should not be fail: %v", err)
|
||||||
}
|
}
|
||||||
|
var cnt int
|
||||||
var passError, passUpdate bool
|
var passError, passUpdate bool
|
||||||
for e := range q {
|
for e := range q {
|
||||||
switch event := e.(type) {
|
switch event := e.(type) {
|
||||||
|
@ -103,17 +106,63 @@ data: {"content": "foo"}
|
||||||
t.Fatalf("should be fail: %v", event.err)
|
t.Fatalf("should be fail: %v", event.err)
|
||||||
}
|
}
|
||||||
case *UpdateEvent:
|
case *UpdateEvent:
|
||||||
|
cnt++
|
||||||
passUpdate = true
|
passUpdate = true
|
||||||
if event.Status.Content != "foo" {
|
if event.Status.Content != "foo" {
|
||||||
t.Fatalf("want %q but %q", "foo", event.Status.Content)
|
t.Fatalf("want %q but %q", "foo", event.Status.Content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
t.Fatalf("result should be one: %d", cnt)
|
||||||
|
}
|
||||||
if !passError || !passUpdate {
|
if !passError || !passUpdate {
|
||||||
t.Fatalf("have not passed through somewhere: error %t, update %t", 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) {
|
||||||
|
}
|
||||||
|
|
||||||
func TestStreamingPublic(t *testing.T) {
|
func TestStreamingPublic(t *testing.T) {
|
||||||
var isEnd bool
|
var isEnd bool
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -150,9 +199,7 @@ data: {"content": "bar"}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("should not be fail: %v", err)
|
t.Fatalf("should not be fail: %v", err)
|
||||||
}
|
}
|
||||||
time.AfterFunc(time.Second, func() {
|
time.AfterFunc(time.Second, cancel)
|
||||||
cancel()
|
|
||||||
})
|
|
||||||
events := []Event{}
|
events := []Event{}
|
||||||
for e := range q {
|
for e := range q {
|
||||||
if _, ok := e.(*ErrorEvent); !ok {
|
if _, ok := e.(*ErrorEvent); !ok {
|
||||||
|
@ -169,3 +216,6 @@ 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) {
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue