Files
xlgo-core/sse/sse_stream_internal_test.go
T
杭州明婳科技 0f13292a46 GLM 5.2修改版
2026-07-02 18:26:27 +08:00

35 lines
946 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package sse
import (
"context"
"errors"
"testing"
"time"
)
// 回归 C3aStream 在 ctx 取消时返回 ctx.Err,不再阻塞在 for-range ch。
// 旧实现 for range ch 无 ctx.Done 分支,ctx 取消后仍阻塞等 ch → goroutine 泄漏 + 上游 LLM 流持续。
func TestStreamStopsOnCtxCancelInternal(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
w := &SSEWriter{ctx: ctx} // writer/flusher 不用(ctx.Done 先触发,不写数据)
ch := make(chan any)
// 不向 ch 发数据,也不关闭 ch——若 Stream 无 ctx.Done 分支将永久阻塞。
done := make(chan error, 1)
go func() {
done <- w.Stream("msg", ch)
}()
cancel()
select {
case err := <-done:
if !errors.Is(err, context.Canceled) {
t.Errorf("Stream err = %v, want context.Canceled", err)
}
case <-time.After(2 * time.Second):
t.Fatal("Stream did not return after ctx cancel (C3a: no ctx.Done branch)")
}
}