|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package xds_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "testing" |
| 26 | + |
| 27 | + v3xdsxdstypepb "github.com/cncf/xds/go/xds/type/v3" |
| 28 | + v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" |
| 29 | + v3endpointpb "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" |
| 30 | + v3listenerpb "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3" |
| 31 | + v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" |
| 32 | + v3httppb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" |
| 33 | + "google.golang.org/grpc" |
| 34 | + "google.golang.org/grpc/credentials/insecure" |
| 35 | + "google.golang.org/grpc/internal/stubserver" |
| 36 | + "google.golang.org/grpc/internal/testutils" |
| 37 | + "google.golang.org/grpc/internal/testutils/xds/e2e" |
| 38 | + "google.golang.org/grpc/internal/testutils/xds/e2e/setup" |
| 39 | + "google.golang.org/grpc/internal/xds/httpfilter" |
| 40 | + |
| 41 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 42 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 43 | +) |
| 44 | + |
| 45 | +// Tests the interaction between the defaultStreamInterceptor and xDS filters. |
| 46 | +// It verifies that the SendMsg, RecvMsg, and CloseSend methods are called the |
| 47 | +// expected number of times for different RPC types (unary, client streaming, |
| 48 | +// server streaming, and bidi streaming) when an xDS filter is registered and |
| 49 | +// used in the call chain. |
| 50 | +func (s) TestDefaultStreamInterceptor_InteractionWithXDSFilters(t *testing.T) { |
| 51 | + // Register a custom xDS filter builder for the test. |
| 52 | + testFilterTypeURL := t.Name() |
| 53 | + filterBuilder := newTrackingHTTPFilterBuilder(testFilterTypeURL) |
| 54 | + httpfilter.Register(filterBuilder) |
| 55 | + defer httpfilter.UnregisterForTesting(testFilterTypeURL) |
| 56 | + |
| 57 | + // Setup a test backend implementing both all four RPC types. |
| 58 | + testServer := &stubserver.StubServer{ |
| 59 | + EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
| 60 | + return &testpb.Empty{}, nil |
| 61 | + }, |
| 62 | + StreamingOutputCallF: func(_ *testpb.StreamingOutputCallRequest, stream testgrpc.TestService_StreamingOutputCallServer) error { |
| 63 | + return stream.Send(&testpb.StreamingOutputCallResponse{}) |
| 64 | + }, |
| 65 | + StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error { |
| 66 | + for { |
| 67 | + _, err := stream.Recv() |
| 68 | + if err == io.EOF { |
| 69 | + break |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + } |
| 75 | + return stream.SendAndClose(&testpb.StreamingInputCallResponse{}) |
| 76 | + }, |
| 77 | + FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { |
| 78 | + for { |
| 79 | + _, err := stream.Recv() |
| 80 | + if err == io.EOF { |
| 81 | + return nil |
| 82 | + } |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + } |
| 92 | + if err := testServer.Start(nil); err != nil { |
| 93 | + t.Fatal("Error starting server:", err) |
| 94 | + } |
| 95 | + defer testServer.Stop() |
| 96 | + |
| 97 | + // Start an xDS management server. |
| 98 | + mgmtServer, nodeID, _, xdsResolver := setup.ManagementServerAndResolver(t) |
| 99 | + |
| 100 | + const serviceName = "my-service-xds" |
| 101 | + clusterSpec := &v3routepb.RouteAction_Cluster{Cluster: "cluster-A"} |
| 102 | + hcm := &v3httppb.HttpConnectionManager{ |
| 103 | + RouteSpecifier: &v3httppb.HttpConnectionManager_RouteConfig{ |
| 104 | + RouteConfig: &v3routepb.RouteConfiguration{ |
| 105 | + Name: "route-" + serviceName, |
| 106 | + VirtualHosts: []*v3routepb.VirtualHost{{ |
| 107 | + Domains: []string{serviceName}, |
| 108 | + Routes: []*v3routepb.Route{{ |
| 109 | + Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: ""}}, |
| 110 | + Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{ |
| 111 | + ClusterSpecifier: clusterSpec, |
| 112 | + }}, |
| 113 | + }}, |
| 114 | + }}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + HttpFilters: []*v3httppb.HttpFilter{ |
| 118 | + { |
| 119 | + Name: "tracking-filter", |
| 120 | + ConfigType: &v3httppb.HttpFilter_TypedConfig{ |
| 121 | + TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{ |
| 122 | + TypeUrl: testFilterTypeURL, |
| 123 | + }), |
| 124 | + }, |
| 125 | + }, |
| 126 | + e2e.RouterHTTPFilter, |
| 127 | + }, |
| 128 | + } |
| 129 | + resources := e2e.UpdateOptions{ |
| 130 | + NodeID: nodeID, |
| 131 | + Listeners: []*v3listenerpb.Listener{{Name: serviceName, ApiListener: &v3listenerpb.ApiListener{ApiListener: testutils.MarshalAny(t, hcm)}}}, |
| 132 | + Clusters: []*v3clusterpb.Cluster{e2e.DefaultCluster("cluster-A", "cluster-A", e2e.SecurityLevelNone)}, |
| 133 | + Endpoints: []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint("cluster-A", "localhost", []uint32{testutils.ParsePort(t, testServer.Address)})}, |
| 134 | + } |
| 135 | + |
| 136 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 137 | + defer cancel() |
| 138 | + if err := mgmtServer.Update(ctx, resources); err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + |
| 142 | + cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(xdsResolver)) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Failed to create a gRPC client: %v", err) |
| 145 | + } |
| 146 | + defer cc.Close() |
| 147 | + client := testgrpc.NewTestServiceClient(cc) |
| 148 | + |
| 149 | + tests := []struct { |
| 150 | + name string |
| 151 | + run func(ctx context.Context, client testgrpc.TestServiceClient) |
| 152 | + wantSendMsg int32 |
| 153 | + wantRecvMsg int32 |
| 154 | + }{ |
| 155 | + { |
| 156 | + name: "unary", |
| 157 | + run: func(ctx context.Context, client testgrpc.TestServiceClient) { |
| 158 | + if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil { |
| 159 | + t.Fatalf("EmptyCall() failed: %v", err) |
| 160 | + } |
| 161 | + }, |
| 162 | + wantSendMsg: 1, // 1 request |
| 163 | + wantRecvMsg: 2, // 1 response + 1 trailers |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "client_streaming", |
| 167 | + run: func(ctx context.Context, client testgrpc.TestServiceClient) { |
| 168 | + stream, err := client.StreamingInputCall(ctx) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("StreamingInputCall() failed: %v", err) |
| 171 | + } |
| 172 | + for i := 0; i < 2; i++ { |
| 173 | + if err := stream.Send(&testpb.StreamingInputCallRequest{}); err != nil { |
| 174 | + t.Fatalf("Send() failed: %v", err) |
| 175 | + } |
| 176 | + } |
| 177 | + if _, err := stream.CloseAndRecv(); err != nil { |
| 178 | + t.Fatalf("CloseAndRecv() failed: %v", err) |
| 179 | + } |
| 180 | + }, |
| 181 | + wantSendMsg: 2, // 2 requests |
| 182 | + wantRecvMsg: 2, // 1 response + 1 trailers |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "server_streaming", |
| 186 | + run: func(ctx context.Context, client testgrpc.TestServiceClient) { |
| 187 | + stream, err := client.StreamingOutputCall(ctx, &testpb.StreamingOutputCallRequest{}) |
| 188 | + if err != nil { |
| 189 | + t.Fatalf("StreamingOutputCall() failed: %v", err) |
| 190 | + } |
| 191 | + if _, err := stream.Recv(); err != nil { |
| 192 | + t.Fatalf("Recv() failed: %v", err) |
| 193 | + } |
| 194 | + }, |
| 195 | + wantSendMsg: 1, // 1 request |
| 196 | + wantRecvMsg: 1, // 1 response (trailers not yet consumed since stream not read to EOF) |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "bidi_streaming", |
| 200 | + run: func(ctx context.Context, client testgrpc.TestServiceClient) { |
| 201 | + stream, err := client.FullDuplexCall(ctx) |
| 202 | + if err != nil { |
| 203 | + t.Fatalf("FullDuplexCall() failed: %v", err) |
| 204 | + } |
| 205 | + if err := stream.Send(&testpb.StreamingOutputCallRequest{}); err != nil { |
| 206 | + t.Fatalf("Send() failed: %v", err) |
| 207 | + } |
| 208 | + if _, err := stream.Recv(); err != nil { |
| 209 | + t.Fatalf("Recv() failed: %v", err) |
| 210 | + } |
| 211 | + if err := stream.CloseSend(); err != nil { |
| 212 | + t.Fatalf("CloseSend() failed: %v", err) |
| 213 | + } |
| 214 | + }, |
| 215 | + wantSendMsg: 1, // 1 request |
| 216 | + wantRecvMsg: 1, // 1 response |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + for _, tc := range tests { |
| 221 | + t.Run(tc.name, func(t *testing.T) { |
| 222 | + filterBuilder.sendMsgCount.Store(0) |
| 223 | + filterBuilder.recvMsgCount.Store(0) |
| 224 | + filterBuilder.closeSendCount.Store(0) |
| 225 | + |
| 226 | + tc.run(ctx, client) |
| 227 | + |
| 228 | + if got := filterBuilder.sendMsgCount.Load(); got != tc.wantSendMsg { |
| 229 | + t.Fatalf("SendMsg() count = %d, want %d", got, tc.wantSendMsg) |
| 230 | + } |
| 231 | + if got := filterBuilder.recvMsgCount.Load(); got != tc.wantRecvMsg { |
| 232 | + t.Fatalf("RecvMsg() count = %d, want %d", got, tc.wantRecvMsg) |
| 233 | + } |
| 234 | + if got := filterBuilder.closeSendCount.Load(); got != 1 { |
| 235 | + t.Fatalf("CloseSend() count = %d, want 1", got) |
| 236 | + } |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
0 commit comments