|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestClient_StreamSessionEvents_DeliversMultipleEvents verifies that the |
| 16 | +// SSE stream stays open across multiple events instead of being torn down |
| 17 | +// when StreamSessionEvents returns. This is a regression test for a bug |
| 18 | +// where a deferred cancel() on the streaming context killed the in-flight |
| 19 | +// HTTP request as soon as the function returned, turning the stream into |
| 20 | +// a one-shot read. |
| 21 | +func TestClient_StreamSessionEvents_DeliversMultipleEvents(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 25 | + w.Header().Set("Content-Type", "text/event-stream") |
| 26 | + w.WriteHeader(http.StatusOK) |
| 27 | + flusher, ok := w.(http.Flusher) |
| 28 | + if !ok { |
| 29 | + t.Errorf("ResponseWriter must support flushing") |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + for i := 1; i <= 3; i++ { |
| 34 | + fmt.Fprintf(w, "data: {\"type\":\"session_title\",\"session_id\":\"s\",\"title\":\"t%d\"}\n\n", i) |
| 35 | + flusher.Flush() |
| 36 | + time.Sleep(20 * time.Millisecond) |
| 37 | + } |
| 38 | + })) |
| 39 | + t.Cleanup(srv.Close) |
| 40 | + |
| 41 | + c, err := NewClient(srv.URL) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + ch, err := c.StreamSessionEvents(t.Context(), "s") |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + var titles []string |
| 48 | + for ev := range ch { |
| 49 | + titleEv, ok := ev.(*SessionTitleEvent) |
| 50 | + if !ok { |
| 51 | + continue |
| 52 | + } |
| 53 | + titles = append(titles, titleEv.Title) |
| 54 | + } |
| 55 | + |
| 56 | + assert.Equal(t, []string{"t1", "t2", "t3"}, titles) |
| 57 | +} |
| 58 | + |
| 59 | +// TestClient_StreamSessionEvents_StopsWhenContextCancelled verifies that |
| 60 | +// cancelling the caller's context tears down the stream and closes the |
| 61 | +// returned channel. |
| 62 | +func TestClient_StreamSessionEvents_StopsWhenContextCancelled(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + |
| 65 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + w.Header().Set("Content-Type", "text/event-stream") |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + flusher, _ := w.(http.Flusher) |
| 69 | + |
| 70 | + ticker := time.NewTicker(20 * time.Millisecond) |
| 71 | + defer ticker.Stop() |
| 72 | + for { |
| 73 | + select { |
| 74 | + case <-r.Context().Done(): |
| 75 | + return |
| 76 | + case <-ticker.C: |
| 77 | + fmt.Fprint(w, "data: {\"type\":\"session_title\",\"session_id\":\"s\",\"title\":\"x\"}\n\n") |
| 78 | + flusher.Flush() |
| 79 | + } |
| 80 | + } |
| 81 | + })) |
| 82 | + t.Cleanup(srv.Close) |
| 83 | + |
| 84 | + c, err := NewClient(srv.URL) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + ctx, cancel := context.WithCancel(t.Context()) |
| 88 | + t.Cleanup(cancel) |
| 89 | + |
| 90 | + ch, err := c.StreamSessionEvents(ctx, "s") |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + // Drain at least one event to confirm the stream is live. |
| 94 | + select { |
| 95 | + case <-ch: |
| 96 | + case <-time.After(2 * time.Second): |
| 97 | + t.Fatal("no events received before cancel") |
| 98 | + } |
| 99 | + |
| 100 | + cancel() |
| 101 | + |
| 102 | + // Channel must close in a bounded time after cancel. |
| 103 | + deadline := time.After(2 * time.Second) |
| 104 | + for { |
| 105 | + select { |
| 106 | + case _, ok := <-ch: |
| 107 | + if !ok { |
| 108 | + return |
| 109 | + } |
| 110 | + case <-deadline: |
| 111 | + t.Fatal("channel was not closed after context cancel") |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments