mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-11 19:47:00 +08:00
Merge 2591a34c1dd1b8139e3e7b36386f203ea60a8bbf into 19b877fa50cbbb9282763099fb177a1e5cc5c850
This commit is contained in:
commit
5bd4ac31a9
@ -1298,10 +1298,9 @@ func (c *Context) SSEvent(name string, message any) {
|
|||||||
// indicates "Is client disconnected in middle of stream"
|
// indicates "Is client disconnected in middle of stream"
|
||||||
func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
||||||
w := c.Writer
|
w := c.Writer
|
||||||
clientGone := w.CloseNotify()
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-clientGone:
|
case <-c.Request.Context().Done():
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
keepOpen := step(w)
|
keepOpen := step(w)
|
||||||
|
|||||||
@ -2917,10 +2917,6 @@ func (r *TestResponseRecorder) CloseNotify() <-chan bool {
|
|||||||
return r.closeChannel
|
return r.closeChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *TestResponseRecorder) closeClient() {
|
|
||||||
r.closeChannel <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateTestResponseRecorder() *TestResponseRecorder {
|
func CreateTestResponseRecorder() *TestResponseRecorder {
|
||||||
return &TestResponseRecorder{
|
return &TestResponseRecorder{
|
||||||
httptest.NewRecorder(),
|
httptest.NewRecorder(),
|
||||||
@ -2931,6 +2927,7 @@ func CreateTestResponseRecorder() *TestResponseRecorder {
|
|||||||
func TestContextStream(t *testing.T) {
|
func TestContextStream(t *testing.T) {
|
||||||
w := CreateTestResponseRecorder()
|
w := CreateTestResponseRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodGet, "", nil)
|
||||||
|
|
||||||
stopStream := true
|
stopStream := true
|
||||||
c.Stream(func(w io.Writer) bool {
|
c.Stream(func(w io.Writer) bool {
|
||||||
@ -2950,10 +2947,12 @@ func TestContextStream(t *testing.T) {
|
|||||||
func TestContextStreamWithClientGone(t *testing.T) {
|
func TestContextStreamWithClientGone(t *testing.T) {
|
||||||
w := CreateTestResponseRecorder()
|
w := CreateTestResponseRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
done, cancel := context.WithCancel(context.Background())
|
||||||
|
c.Request, _ = http.NewRequestWithContext(done, http.MethodGet, "", nil)
|
||||||
|
|
||||||
c.Stream(func(writer io.Writer) bool {
|
c.Stream(func(writer io.Writer) bool {
|
||||||
defer func() {
|
defer func() {
|
||||||
w.closeClient()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
_, err := writer.Write([]byte("test"))
|
_, err := writer.Write([]byte("test"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user