fix(webui): open control-plane watch streams before the first change

This commit is contained in:
sam committed 2026-09-25 09:37:36 +10:00
1 parent db7d9c790b
commit 5c7ea47b25
2 files changed
+39 -1

No files matched your search

+4
View File
@@ -148,6 +148,10 @@ func (s *Server) handleControlPlaneWatch(w http.ResponseWriter, r *http.Request)
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
// Headers go out now: the browser marks the stream live on them, and an
// unchanged resource may send nothing for hours.
w.WriteHeader(http.StatusOK)
flusher.Flush()
for {
response, recvErr := stream.Recv()
if errors.Is(recvErr, io.EOF) || r.Context().Err() != nil {
+35 -1
View File
@@ -8,6 +8,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -22,6 +23,7 @@ type controlPlaneClientStub struct {
query *controlpb.Resource
history *controlpb.HistoryRequest
revisions []*controlpb.Revision
watch grpc.ServerStreamingClient[controlpb.WatchResponse]
err error
}
@@ -47,7 +49,39 @@ func (f *controlPlaneClientStub) Command(_ context.Context, request *controlpb.C
}
func (f *controlPlaneClientStub) Watch(context.Context, *controlpb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[controlpb.WatchResponse], error) {
return nil, f.err
return f.watch, f.err
}
// idleWatch is a watch stream with nothing to deliver: Recv blocks until the
// request ends, as a real stream does while the resource is unchanged.
type idleWatch struct {
grpc.ClientStream
done <-chan struct{}
}
func (w idleWatch) Recv() (*controlpb.WatchResponse, error) {
<-w.done
return nil, context.Canceled
}
// TestControlPlaneWatchOpensBeforeTheFirstChange: a browser marks an event
// stream live when its headers arrive. Holding them until the first change
// leaves every settings card reading "Connecting" on an unchanged resource.
func TestControlPlaneWatchOpensBeforeTheFirstChange(t *testing.T) {
server := httptest.NewServer((&Server{ControlPlane: &controlPlaneClientStub{watch: idleWatch{done: t.Context().Done()}}}).Handler())
t.Cleanup(server.Close)
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
request, _ := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/api/control-plane/watch/workflow-execution-settings", nil)
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatalf("watch did not answer before its first change: %v", err)
}
defer response.Body.Close()
if got := response.Header.Get("Content-Type"); got != "text/event-stream" {
t.Fatalf("Content-Type = %q", got)
}
}
func TestControlPlaneCommandSuppliesTrustedAttribution(t *testing.T) {