-
Notifications
You must be signed in to change notification settings - Fork 236
test(router) Proof of SSE buffer issues #2765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| graphql_datasource "github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/graphql_datasource" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve" | ||
| ) | ||
|
|
||
| type aliasingSubscriptionUpdater struct { | ||
| mu sync.Mutex | ||
| updates [][]byte | ||
| firstUpdate chan struct{} | ||
| completed chan struct{} | ||
| closed chan struct{} | ||
| } | ||
|
|
||
| func newAliasingSubscriptionUpdater() *aliasingSubscriptionUpdater { | ||
| return &aliasingSubscriptionUpdater{ | ||
| firstUpdate: make(chan struct{}, 1), | ||
| completed: make(chan struct{}, 1), | ||
| closed: make(chan struct{}, 1), | ||
| } | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) Update(data []byte) { | ||
| a.mu.Lock() | ||
| a.updates = append(a.updates, data) | ||
| isFirst := len(a.updates) == 1 | ||
| a.mu.Unlock() | ||
|
|
||
| if isFirst { | ||
| select { | ||
| case a.firstUpdate <- struct{}{}: | ||
| default: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) UpdateSubscription(_ resolve.SubscriptionIdentifier, data []byte) { | ||
| a.Update(data) | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) Complete() { | ||
| select { | ||
| case a.completed <- struct{}{}: | ||
| default: | ||
| } | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) Close(_ resolve.SubscriptionCloseKind) { | ||
| select { | ||
| case a.closed <- struct{}{}: | ||
| default: | ||
| } | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) CloseSubscription(_ resolve.SubscriptionCloseKind, _ resolve.SubscriptionIdentifier) { | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) Subscriptions() map[context.Context]resolve.SubscriptionIdentifier { | ||
| return nil | ||
| } | ||
|
|
||
| func (a *aliasingSubscriptionUpdater) Updates() []string { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
|
|
||
| out := make([]string, len(a.updates)) | ||
| for i := range a.updates { | ||
| out[i] = string(a.updates[i]) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func TestGraphQLSubscriptionClient_SSESingleLinePayloadsRemainStableAfterNextRead(t *testing.T) { | ||
| serverDone := make(chan struct{}) | ||
| updater := newAliasingSubscriptionUpdater() | ||
|
|
||
| firstPayload := `{"data":{"tokenPriceUpdated":{"priceUsd":1.0001}}}` | ||
| secondPayload := `{"data":{"tokenPriceUpdated":{"priceUsd":1.0002}}}` | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| flusher, ok := w.(http.Flusher) | ||
| require.True(t, ok) | ||
|
|
||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.Header().Set("Cache-Control", "no-cache") | ||
| w.Header().Set("Connection", "keep-alive") | ||
|
|
||
| _, _ = fmt.Fprintf(w, "event: next\ndata: %s\n\n", firstPayload) | ||
| flusher.Flush() | ||
|
|
||
| select { | ||
| case <-updater.firstUpdate: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for first SSE update to be observed") | ||
| } | ||
|
|
||
| _, _ = fmt.Fprintf(w, "event: next\ndata: %s\n\n", secondPayload) | ||
| flusher.Flush() | ||
|
|
||
| _, _ = fmt.Fprint(w, "event: complete\n\n") | ||
| flusher.Flush() | ||
|
|
||
| close(serverDone) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| engineCtx, engineCancel := context.WithCancel(context.Background()) | ||
| defer engineCancel() | ||
|
|
||
| requestCtx, requestCancel := context.WithCancel(context.Background()) | ||
| defer requestCancel() | ||
|
|
||
| client := graphql_datasource.NewGraphQLSubscriptionClient( | ||
| http.DefaultClient, | ||
| http.DefaultClient, | ||
| engineCtx, | ||
| graphql_datasource.WithReadTimeout(time.Millisecond), | ||
| ) | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { | ||
| done <- client.Subscribe(resolve.NewContext(requestCtx), graphql_datasource.GraphQLSubscriptionOptions{ | ||
| URL: server.URL, | ||
| Body: graphql_datasource.GraphQLBody{ | ||
| Query: `subscription { tokenPriceUpdated { priceUsd } }`, | ||
| }, | ||
| UseSSE: true, | ||
| }, updater) | ||
| }() | ||
|
|
||
| select { | ||
| case <-updater.completed: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for completion") | ||
| } | ||
|
|
||
| select { | ||
| case <-updater.closed: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for close") | ||
| } | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| require.NoError(t, err) | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for subscription client to exit") | ||
| } | ||
|
|
||
| select { | ||
| case <-serverDone: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for SSE server to finish") | ||
| } | ||
|
|
||
| updates := updater.Updates() | ||
| require.Len(t, updates, 2) | ||
| require.Equal(t, firstPayload, updates[0], "first SSE payload should remain stable after the next event is read") | ||
| require.Equal(t, secondPayload, updates[1]) | ||
| } | ||
|
|
||
| func TestGraphQLSubscriptionClient_SSEPayloadsRemainStableAcrossBurstReads(t *testing.T) { | ||
| const payloadCount = 64 | ||
|
|
||
| expectedPayloads := make([]string, 0, payloadCount) | ||
| for i := 0; i < payloadCount; i++ { | ||
| expectedPayloads = append(expectedPayloads, fmt.Sprintf( | ||
| `{"data":{"tokenPriceUpdated":{"sequence":%d,"priceUsd":1.%04d,"note":"%s"}}}`, | ||
| i, | ||
| 1000+i, | ||
| strings.Repeat(string(rune('a'+(i%26))), 8+i), | ||
| )) | ||
| } | ||
|
|
||
| serverDone := make(chan struct{}) | ||
| updater := newAliasingSubscriptionUpdater() | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| flusher, ok := w.(http.Flusher) | ||
| require.True(t, ok) | ||
|
|
||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| w.Header().Set("Cache-Control", "no-cache") | ||
| w.Header().Set("Connection", "keep-alive") | ||
|
|
||
| _, _ = fmt.Fprintf(w, "event: next\ndata: %s\n\n", expectedPayloads[0]) | ||
| flusher.Flush() | ||
|
|
||
| select { | ||
| case <-updater.firstUpdate: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("timed out waiting for first SSE update to be observed") | ||
| } | ||
|
|
||
| for i := 1; i < len(expectedPayloads); i++ { | ||
| _, _ = fmt.Fprintf(w, "event: next\ndata: %s\n\n", expectedPayloads[i]) | ||
| flusher.Flush() | ||
| } | ||
|
|
||
| _, _ = fmt.Fprint(w, "event: complete\n\n") | ||
| flusher.Flush() | ||
|
|
||
| close(serverDone) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| engineCtx, engineCancel := context.WithCancel(context.Background()) | ||
| defer engineCancel() | ||
|
|
||
| requestCtx, requestCancel := context.WithCancel(context.Background()) | ||
| defer requestCancel() | ||
|
|
||
| client := graphql_datasource.NewGraphQLSubscriptionClient( | ||
| http.DefaultClient, | ||
| http.DefaultClient, | ||
| engineCtx, | ||
| graphql_datasource.WithReadTimeout(time.Millisecond), | ||
| ) | ||
|
|
||
| done := make(chan error, 1) | ||
| go func() { | ||
| done <- client.Subscribe(resolve.NewContext(requestCtx), graphql_datasource.GraphQLSubscriptionOptions{ | ||
| URL: server.URL, | ||
| Body: graphql_datasource.GraphQLBody{ | ||
| Query: `subscription { tokenPriceUpdated { sequence priceUsd note } }`, | ||
| }, | ||
| UseSSE: true, | ||
| }, updater) | ||
| }() | ||
|
|
||
| select { | ||
| case <-updater.completed: | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("timed out waiting for completion") | ||
| } | ||
|
|
||
| select { | ||
| case <-updater.closed: | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("timed out waiting for close") | ||
| } | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| require.NoError(t, err) | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("timed out waiting for subscription client to exit") | ||
| } | ||
|
|
||
| select { | ||
| case <-serverDone: | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("timed out waiting for SSE server to finish") | ||
| } | ||
|
|
||
| updates := updater.Updates() | ||
| require.Equal(t, expectedPayloads, updates, "every queued SSE payload should remain stable after later events are read") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: wundergraph/cosmo
Length of output: 1280
🏁 Script executed:
Repository: wundergraph/cosmo
Length of output: 4772
Avoid
require/t.Fatalinsidehttptesthandler goroutines.require.True(t, ok)at lines 93 and 191, andt.Fatal(...)at lines 105 and 203 run in handler goroutines spawned byhttptest.NewServer. Calling these assertion/failure methods outside the main test goroutine will cause a runtime panic. Route handler errors to a channel and assert in the main test goroutine instead.💡 Suggested fix pattern
Also applies to: lines 189–215
🤖 Prompt for AI Agents