-
Notifications
You must be signed in to change notification settings - Fork 4.8k
grpc: place defaultStreamInterceptor after user interceptors #9404
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
Open
easwars
wants to merge
4
commits into
grpc:master
Choose a base branch
from
easwars:change_default_stream_interceptor_position
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+332
−27
Open
Changes from 1 commit
Commits
Show all changes
4 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package xds_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "testing" | ||
|
|
||
| v3xdsxdstypepb "github.com/cncf/xds/go/xds/type/v3" | ||
| v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" | ||
| v3endpointpb "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" | ||
| v3listenerpb "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3" | ||
| v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" | ||
| v3httppb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/internal/stubserver" | ||
| "google.golang.org/grpc/internal/testutils" | ||
| "google.golang.org/grpc/internal/testutils/xds/e2e" | ||
| "google.golang.org/grpc/internal/testutils/xds/e2e/setup" | ||
| "google.golang.org/grpc/internal/xds/httpfilter" | ||
|
|
||
| testgrpc "google.golang.org/grpc/interop/grpc_testing" | ||
| testpb "google.golang.org/grpc/interop/grpc_testing" | ||
| ) | ||
|
|
||
| func (s) TestDefaultStreamInterceptor_InteractionWithXDSFilters(t *testing.T) { | ||
| // Register a custom xDS filter builder for the test. | ||
| testFilterTypeURL := t.Name() | ||
| filterBuilder := newTrackingHTTPFilterBuilder(testFilterTypeURL) | ||
| httpfilter.Register(filterBuilder) | ||
| defer httpfilter.UnregisterForTesting(testFilterTypeURL) | ||
|
|
||
| // Setup a test backend implementing both all four RPC types. | ||
| testServer := &stubserver.StubServer{ | ||
| EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { | ||
| return &testpb.Empty{}, nil | ||
| }, | ||
| StreamingOutputCallF: func(_ *testpb.StreamingOutputCallRequest, stream testgrpc.TestService_StreamingOutputCallServer) error { | ||
| return stream.Send(&testpb.StreamingOutputCallResponse{}) | ||
| }, | ||
| StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error { | ||
| for { | ||
| _, err := stream.Recv() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return stream.SendAndClose(&testpb.StreamingInputCallResponse{}) | ||
| }, | ||
| FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { | ||
| for { | ||
| _, err := stream.Recv() | ||
| if err == io.EOF { | ||
| return nil | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| if err := testServer.Start(nil); err != nil { | ||
| t.Fatal("Error starting server:", err) | ||
| } | ||
| defer testServer.Stop() | ||
|
|
||
| // Start an xDS management server. | ||
| mgmtServer, nodeID, _, xdsResolver := setup.ManagementServerAndResolver(t) | ||
|
|
||
| const serviceName = "my-service-xds" | ||
| clusterSpec := &v3routepb.RouteAction_Cluster{Cluster: "cluster-A"} | ||
| hcm := &v3httppb.HttpConnectionManager{ | ||
| RouteSpecifier: &v3httppb.HttpConnectionManager_RouteConfig{ | ||
| RouteConfig: &v3routepb.RouteConfiguration{ | ||
| Name: "route-" + serviceName, | ||
| VirtualHosts: []*v3routepb.VirtualHost{{ | ||
| Domains: []string{serviceName}, | ||
| Routes: []*v3routepb.Route{{ | ||
| Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: ""}}, | ||
| Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{ | ||
| ClusterSpecifier: clusterSpec, | ||
| }}, | ||
| }}, | ||
| }}, | ||
| }, | ||
| }, | ||
| HttpFilters: []*v3httppb.HttpFilter{ | ||
| { | ||
| Name: "tracking-filter", | ||
| ConfigType: &v3httppb.HttpFilter_TypedConfig{ | ||
| TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{ | ||
| TypeUrl: testFilterTypeURL, | ||
| }), | ||
| }, | ||
| }, | ||
| e2e.RouterHTTPFilter, | ||
| }, | ||
| } | ||
| resources := e2e.UpdateOptions{ | ||
| NodeID: nodeID, | ||
| Listeners: []*v3listenerpb.Listener{{Name: serviceName, ApiListener: &v3listenerpb.ApiListener{ApiListener: testutils.MarshalAny(t, hcm)}}}, | ||
| Clusters: []*v3clusterpb.Cluster{e2e.DefaultCluster("cluster-A", "cluster-A", e2e.SecurityLevelNone)}, | ||
| Endpoints: []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint("cluster-A", "localhost", []uint32{testutils.ParsePort(t, testServer.Address)})}, | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
| if err := mgmtServer.Update(ctx, resources); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(xdsResolver)) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create a gRPC client: %v", err) | ||
| } | ||
| defer cc.Close() | ||
|
|
||
| // Make a unary RPC. | ||
| client := testgrpc.NewTestServiceClient(cc) | ||
| if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil { | ||
| t.Fatalf("EmptyCall() failed: %v", err) | ||
| } | ||
| if got, want := filterBuilder.recvMsgCount.Load(), int32(2); got != want { // One for the request, one for the trailers. | ||
| t.Fatalf("%d calls to RecvMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.sendMsgCount.Load(), int32(1); got != want { // One for the response. | ||
| t.Fatalf("%d calls to SendMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.closeSendCount.Load(), int32(1); got != want { // CloseSend() is called once for the unary RPC. | ||
| t.Fatalf("%d calls to CloseSend(), want %d", got, want) | ||
| } | ||
|
|
||
| // Make a client-streaming RPC, sending two messages and receiving one. | ||
| clientStreamingRPC, err := client.StreamingInputCall(ctx) | ||
| if err != nil { | ||
| t.Fatalf("StreamingInputCall() failed: %v", err) | ||
| } | ||
| if err := clientStreamingRPC.Send(&testpb.StreamingInputCallRequest{}); err != nil { | ||
| t.Fatalf("Send() failed: %v", err) | ||
| } | ||
| if err := clientStreamingRPC.Send(&testpb.StreamingInputCallRequest{}); err != nil { | ||
| t.Fatalf("Send() failed: %v", err) | ||
| } | ||
| if _, err := clientStreamingRPC.CloseAndRecv(); err != nil { | ||
| t.Fatalf("CloseAndRecv() failed: %v", err) | ||
| } | ||
| if got, want := filterBuilder.recvMsgCount.Load(), int32(4); got != want { // One for the request, one for the trailers. | ||
| t.Fatalf("%d calls to RecvMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.sendMsgCount.Load(), int32(3); got != want { // Two for responses. | ||
| t.Fatalf("%d calls to SendMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.closeSendCount.Load(), int32(2); got != want { // CloseSend() is called as part of CloseAndRecv(). | ||
| t.Fatalf("%d calls to CloseSend(), want %d", got, want) | ||
| } | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Make a server-streaming RPC, sending one message and receiving one. | ||
| serverStreamingRPC, err := client.StreamingOutputCall(ctx, &testpb.StreamingOutputCallRequest{}) | ||
| if err != nil { | ||
| t.Fatalf("StreamingOutputCall() failed: %v", err) | ||
| } | ||
| if _, err := serverStreamingRPC.Recv(); err != nil { | ||
| t.Fatalf("Recv() failed: %v", err) | ||
| } | ||
| if got, want := filterBuilder.recvMsgCount.Load(), int32(5); got != want { // One for the request, none for the trailers. | ||
| t.Fatalf("%d calls to RecvMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.sendMsgCount.Load(), int32(4); got != want { // One for the response. | ||
| t.Fatalf("%d calls to SendMsg(), want %d", got, want) | ||
| } | ||
| // CloseSend() is invoked by the proto generated code after sending the | ||
| // request message. It is also invoked by the defaultStreamInterceptor to | ||
| // handle cases where the user is using the ClientStream API instead of the | ||
| // proto generated code. | ||
| if got, want := filterBuilder.closeSendCount.Load(), int32(4); got != want { | ||
| t.Fatalf("%d calls to CloseSend(), want %d", got, want) | ||
| } | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Make a bidirectional-streaming RPC, sending one message and receiving one. | ||
| bidiStreamingRPC, err := client.FullDuplexCall(ctx) | ||
| if err != nil { | ||
| t.Fatalf("FullDuplexCall() failed: %v", err) | ||
| } | ||
| if err := bidiStreamingRPC.Send(&testpb.StreamingOutputCallRequest{}); err != nil { | ||
| t.Fatalf("Send() failed: %v", err) | ||
| } | ||
| if _, err := bidiStreamingRPC.Recv(); err != nil { | ||
| t.Fatalf("Recv() failed: %v", err) | ||
| } | ||
| if got, want := filterBuilder.recvMsgCount.Load(), int32(6); got != want { // One for the request, none for the trailers. | ||
| t.Fatalf("%d calls to RecvMsg(), want %d", got, want) | ||
| } | ||
| if got, want := filterBuilder.sendMsgCount.Load(), int32(5); got != want { // One for the response. | ||
| t.Fatalf("%d calls to SendMsg(), want %d", got, want) | ||
| } | ||
| // Bidi stream is still open, so CloseSend() is yet to be called. | ||
| if got, want := filterBuilder.closeSendCount.Load(), int32(4); got != want { | ||
| t.Fatalf("%d calls to CloseSend(), want %d", got, want) | ||
| } | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
| bidiStreamingRPC.CloseSend() | ||
| if got, want := filterBuilder.closeSendCount.Load(), int32(5); got != want { | ||
| t.Fatalf("%d calls to CloseSend(), want %d", got, want) | ||
| } | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
| } | ||
Oops, something went wrong.
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.