feat(chat): StreamerHub with ring buffer + cond + Done/Error/Cancel flows

This commit is contained in:
2026-05-04 17:27:50 +08:00
parent d1be9e6eb0
commit 617794c7a3
5 changed files with 1181 additions and 0 deletions
+13
View File
@@ -15,6 +15,9 @@ type FakeClient struct {
StreamError error // Stream() 立即返回的错误(如果非 nil)
TokenCount int // CountTokens 返回值
StreamCalls int
// BlockCh, if non-nil, causes Stream goroutine to block after emitting all
// Events until ctx is cancelled or BlockCh is closed. Useful for cancel tests.
BlockCh chan struct{}
}
// NewFake 构造一个 FakeClient,默认 provider="fake"。
@@ -33,6 +36,8 @@ func (f *FakeClient) Stream(ctx context.Context, modelID string, req Request) (<
events := append([]StreamEvent(nil), f.Events...)
f.mu.Unlock()
blockCh := f.BlockCh
ch := make(chan StreamEvent, len(events)+1)
go func() {
defer close(ch)
@@ -44,6 +49,14 @@ func (f *FakeClient) Stream(ctx context.Context, modelID string, req Request) (<
case ch <- e:
}
}
// If a block channel is set, pause here until ctx is cancelled or BlockCh
// is closed. This lets cancel tests confirm mid-stream cancellation.
if blockCh != nil {
select {
case <-ctx.Done():
case <-blockCh:
}
}
}()
return ch, nil
}