Merge 2591a34c1dd1b8139e3e7b36386f203ea60a8bbf into 9914178584e42458ff7d23891463a880f58c9d86

This commit is contained in:
smoky 2026-01-03 10:34:49 +08:00 committed by GitHub
commit dd072ef9c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 7 deletions

View File

@ -1291,10 +1291,9 @@ func (c *Context) SSEvent(name string, message any) {
// indicates "Is client disconnected in middle of stream"
func (c *Context) Stream(step func(w io.Writer) bool) bool {
w := c.Writer
clientGone := w.CloseNotify()
for {
select {
case <-clientGone:
case <-c.Request.Context().Done():
return true
default:
keepOpen := step(w)

View File

@ -2948,10 +2948,6 @@ func (r *TestResponseRecorder) CloseNotify() <-chan bool {
return r.closeChannel
}
func (r *TestResponseRecorder) closeClient() {
r.closeChannel <- true
}
func CreateTestResponseRecorder() *TestResponseRecorder {
return &TestResponseRecorder{
httptest.NewRecorder(),
@ -2962,6 +2958,7 @@ func CreateTestResponseRecorder() *TestResponseRecorder {
func TestContextStream(t *testing.T) {
w := CreateTestResponseRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodGet, "", nil)
stopStream := true
c.Stream(func(w io.Writer) bool {
@ -2981,10 +2978,12 @@ func TestContextStream(t *testing.T) {
func TestContextStreamWithClientGone(t *testing.T) {
w := CreateTestResponseRecorder()
c, _ := CreateTestContext(w)
done, cancel := context.WithCancel(context.Background())
c.Request, _ = http.NewRequestWithContext(done, http.MethodGet, "", nil)
c.Stream(func(writer io.Writer) bool {
defer func() {
w.closeClient()
cancel()
}()
_, err := writer.Write([]byte("test"))