Skip to content

Commit

Permalink
Merge pull request #76 from mutablelogic/v4
Browse files Browse the repository at this point in the history
Updated so ping only happens when no event
  • Loading branch information
djthorpe authored Jul 31, 2024
2 parents 88fc1e7 + e8803f3 commit fa9c42d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/httpresponse/textstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewTextStream(w http.ResponseWriter, tuples ...string) *TextStream {
return
}
self.emit(evt)
ticker.Reset(defaultKeepAlive)
case <-ticker.C:
self.err = errors.Join(self.err, self.emit(&textevent{strPing, nil}))
ticker.Reset(defaultKeepAlive)
Expand Down
12 changes: 6 additions & 6 deletions pkg/httpresponse/textstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func Test_textstream_001(t *testing.T) {
resp := httptest.NewRecorder()
ts := httpresponse.NewTextStream(resp)
assert.NotNil(ts)
t.Log(ts)
assert.NoError(ts.Close())
})

Expand All @@ -33,21 +32,22 @@ func Test_textstream_001(t *testing.T) {
assert.Equal("event: ping\n\n", resp.Body.String())
})

t.Run("EventNoData", func(t *testing.T) {
t.Run("EventDataAfterPing", func(t *testing.T) {
resp := httptest.NewRecorder()
ts := httpresponse.NewTextStream(resp)
assert.NotNil(ts)

time.Sleep(200 * time.Millisecond)
ts.Write("foo")

time.Sleep(1 * time.Second)
assert.NoError(ts.Close())
assert.Equal(100, resp.Code)
assert.Equal("text/event-stream", resp.Header().Get("Content-Type"))
assert.Equal("event: foo\n\n"+"event: ping\n\n", resp.Body.String())
assert.Equal("event: ping\n\n"+"event: foo\n\n", resp.Body.String())
})

t.Run("EventData", func(t *testing.T) {
t.Run("EventDataNoPing", func(t *testing.T) {
resp := httptest.NewRecorder()
ts := httpresponse.NewTextStream(resp)
assert.NotNil(ts)
Expand All @@ -58,7 +58,7 @@ func Test_textstream_001(t *testing.T) {
assert.NoError(ts.Close())
assert.Equal(100, resp.Code)
assert.Equal("text/event-stream", resp.Header().Get("Content-Type"))
assert.Equal("event: foo\n"+"data: \"bar\"\n\n"+"event: ping\n\n", resp.Body.String())
assert.Equal("event: foo\n"+"data: \"bar\"\n\n", resp.Body.String())
})

t.Run("EventDataData", func(t *testing.T) {
Expand All @@ -72,7 +72,7 @@ func Test_textstream_001(t *testing.T) {
assert.NoError(ts.Close())
assert.Equal(100, resp.Code)
assert.Equal("text/event-stream", resp.Header().Get("Content-Type"))
assert.Equal("event: foo\n"+"data: \"bar1\"\n"+"data: \"bar2\"\n\n"+"event: ping\n\n", resp.Body.String())
assert.Equal("event: foo\n"+"data: \"bar1\"\n"+"data: \"bar2\"\n\n", resp.Body.String())
})

}

0 comments on commit fa9c42d

Please sign in to comment.