Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions pkg/mcp/service/sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type sseTestServer struct {
mu sync.Mutex
clients []chan string
done chan struct{}
wg sync.WaitGroup
}

func (s *sseTestServer) broadcast(line string) {
Expand All @@ -43,16 +42,15 @@ func (s *sseTestServer) handleSSE(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)
flusher.Flush()

ch := make(chan string, 32)
s.mu.Lock()
s.clients = append(s.clients, ch)
s.mu.Unlock()

s.wg.Add(1)
defer s.wg.Done()
// Register the response channel before headers announce readiness. The client
// may POST initialize as soon as GET returns, including after a reconnect.
w.WriteHeader(http.StatusOK)
flusher.Flush()

for {
select {
Expand Down Expand Up @@ -130,7 +128,7 @@ func (s *sseTestServer) handleMessage(w http.ResponseWriter, r *http.Request) {

// newFakeSSEConfig starts a fake SSE MCP server and returns its service config.
// The server is shut down via t.Cleanup: the done channel is closed so that
// SSE handler goroutines exit cleanly before httptest.Server.Close() is called.
// httptest.Server.Close waits for all handlers, including ones still starting.
func newFakeSSEConfig(t *testing.T, id string) service.MCPServiceConfig {
t.Helper()
srv := &sseTestServer{done: make(chan struct{})}
Expand All @@ -140,7 +138,6 @@ func newFakeSSEConfig(t *testing.T, id string) service.MCPServiceConfig {
ts := httptest.NewServer(mux)
t.Cleanup(func() {
close(srv.done) // signal SSE handler goroutines to exit
srv.wg.Wait() // wait until they do
ts.Close()
})
return service.MCPServiceConfig{
Expand Down Expand Up @@ -236,7 +233,6 @@ func TestManagerSSECallToolRelaysProgress(t *testing.T) {
}
}
done := make(chan struct{})
var wg sync.WaitGroup

mux := http.NewServeMux()
mux.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -247,14 +243,12 @@ func TestManagerSSECallToolRelaysProgress(t *testing.T) {
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)
flusher.Flush()
ch := make(chan string, 32)
clientsMu.Lock()
clients = append(clients, ch)
clientsMu.Unlock()
wg.Add(1)
defer wg.Done()
w.WriteHeader(http.StatusOK)
flusher.Flush()
for {
select {
case line := <-ch:
Expand Down Expand Up @@ -302,7 +296,6 @@ func TestManagerSSECallToolRelaysProgress(t *testing.T) {
ts := httptest.NewServer(mux)
t.Cleanup(func() {
close(done)
wg.Wait()
ts.Close()
})

Expand Down
Loading