-
Notifications
You must be signed in to change notification settings - Fork 234
fix: add back NewServer #2546
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
Merged
SkArchon
merged 2 commits into
main
from
milinda/eng-8988-router-clients-using-newserver-cannot-upgrade-to-latest
Feb 23, 2026
Merged
fix: add back NewServer #2546
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,153 @@ | ||
| package integration | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/wundergraph/cosmo/demo/pkg/subgraphs" | ||
| "github.com/wundergraph/cosmo/router-tests/testenv" | ||
| "github.com/wundergraph/cosmo/router/core" | ||
| nodev1 "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/node/v1" | ||
| "github.com/wundergraph/cosmo/router/pkg/config" | ||
| natsPubsub "github.com/wundergraph/cosmo/router/pkg/pubsub/nats" | ||
| "go.uber.org/zap" | ||
| "google.golang.org/protobuf/encoding/protojson" | ||
| ) | ||
|
|
||
| func TestNewServer(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("creates a working server without starting a listener", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| rr, cleanup := setupNewServerTest(t) | ||
| defer cleanup() | ||
|
|
||
| svr, err := rr.NewServer(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| svr.HealthChecks().SetReady(true) | ||
| ts := httptest.NewServer(svr.HttpServer().Handler) | ||
| defer ts.Close() | ||
|
|
||
| data, err := json.Marshal(testenv.GraphQLRequest{Query: `query { employees { id } }`}) | ||
| require.NoError(t, err) | ||
|
|
||
| req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, ts.URL+"/graphql", bytes.NewReader(data)) | ||
| require.NoError(t, err) | ||
|
|
||
| response, err := testenv.MakeGraphQLRequestRawFromClient(req, &http.Client{}) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, http.StatusOK, response.Response.StatusCode) | ||
| require.JSONEq(t, `{"data":{"employees":[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":7},{"id":8},{"id":10},{"id":11},{"id":12}]}}`, response.Body) | ||
| }) | ||
|
|
||
| t.Run("health check endpoint is reachable", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| rr, cleanup := setupNewServerTest(t) | ||
| defer cleanup() | ||
|
|
||
| svr, err := rr.NewServer(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| svr.HealthChecks().SetReady(true) | ||
| ts := httptest.NewServer(svr.HttpServer().Handler) | ||
| defer ts.Close() | ||
|
|
||
| resp, err := http.Get(ts.URL + "/health/ready") | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| _ = resp.Body.Close() | ||
| }() | ||
| require.Equal(t, http.StatusOK, resp.StatusCode) | ||
| }) | ||
|
|
||
| t.Run("new server shutdown prevents further requests", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| rr, cleanup := setupNewServerTest(t) | ||
| defer cleanup() | ||
|
|
||
| svr, err := rr.NewServer(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| svr.HealthChecks().SetReady(true) | ||
| ts := httptest.NewServer(svr.HttpServer().Handler) | ||
|
|
||
| // Verify the server works before shutdown | ||
| data, err := json.Marshal(testenv.GraphQLRequest{Query: `query { employees { id } }`}) | ||
| require.NoError(t, err) | ||
|
|
||
| req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, ts.URL+"/graphql", bytes.NewReader(data)) | ||
| require.NoError(t, err) | ||
|
|
||
| response1, err := testenv.MakeGraphQLRequestRawFromClient(req, &http.Client{}) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, http.StatusOK, response1.Response.StatusCode) | ||
| require.JSONEq(t, `{"data":{"employees":[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5},{"id":7},{"id":8},{"id":10},{"id":11},{"id":12}]}}`, response1.Body) | ||
|
|
||
| // Shutdown the router, then close the httptest server | ||
| shutdownCtx, shutdownCancel := context.WithTimeout(t.Context(), 5*time.Second) | ||
| defer shutdownCancel() | ||
| require.NoError(t, rr.Shutdown(shutdownCtx)) | ||
| ts.Close() | ||
|
|
||
| // After shutdown, requests should fail | ||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
| req, err = http.NewRequestWithContext(ctx, http.MethodPost, ts.URL+"/graphql", bytes.NewReader(data)) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = testenv.MakeGraphQLRequestRawFromClient(req, &http.Client{}) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| func setupNewServerTest(t *testing.T) (rr *core.Router, cleanup func()) { | ||
| t.Helper() | ||
|
|
||
| employeesServer := httptest.NewServer(subgraphs.EmployeesHandler(&subgraphs.SubgraphOptions{ | ||
| NatsPubSubByProviderID: map[string]natsPubsub.Adapter{}, | ||
| })) | ||
|
|
||
| // Build the router config from the embedded template | ||
| replaced := testenv.ConfigJSONTemplate | ||
| replacements := map[string]string{ | ||
| subgraphs.EmployeesDefaultDemoURL: testenv.GqlURL(employeesServer), | ||
| } | ||
| for k, v := range replacements { | ||
| replaced = strings.ReplaceAll(replaced, k, v) | ||
| } | ||
|
|
||
| var routerConfig nodev1.RouterConfig | ||
| require.NoError(t, protojson.Unmarshal([]byte(replaced), &routerConfig)) | ||
|
|
||
| rr, err := core.NewRouter( | ||
| core.WithDisableUsageTracking(), | ||
| core.WithLogger(zap.NewNop()), | ||
| core.WithDevelopmentMode(true), | ||
| core.WithStaticExecutionConfig(&routerConfig), | ||
| core.WithEngineExecutionConfig(config.EngineExecutionConfiguration{}), | ||
| core.WithBatching(&core.BatchingConfig{}), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| allServers := []*httptest.Server{employeesServer} | ||
|
|
||
| return rr, func() { | ||
| _ = rr.Shutdown(t.Context()) | ||
| for _, s := range allServers { | ||
| s.Close() | ||
| } | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.