|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2022 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 e2e_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "google.golang.org/grpc/internal/testutils" |
| 26 | + "google.golang.org/grpc/internal/testutils/xds/e2e" |
| 27 | + "google.golang.org/grpc/xds/internal/xdsclient" |
| 28 | + "google.golang.org/grpc/xds/internal/xdsclient/xdsresource" |
| 29 | + |
| 30 | + v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" |
| 31 | +) |
| 32 | + |
| 33 | +// TestWatchCallAnotherWatch tests the scenario where a watch is registered for |
| 34 | +// a resource, and more watches are registered from the first watch's callback. |
| 35 | +// The test verifies that this scenario does not lead to a deadlock. |
| 36 | +func (s) TestWatchCallAnotherWatch(t *testing.T) { |
| 37 | + overrideFedEnvVar(t) |
| 38 | + |
| 39 | + // Start an xDS management server and set the option to allow it to respond |
| 40 | + // to requests which only specify a subset of the configured resources. |
| 41 | + mgmtServer, nodeID, bootstrapContents, _, cleanup := e2e.SetupManagementServer(t, &e2e.ManagementServerOptions{AllowResourceSubset: true}) |
| 42 | + defer cleanup() |
| 43 | + |
| 44 | + // Create an xDS client with the above bootstrap contents. |
| 45 | + client, err := xdsclient.NewWithBootstrapContentsForTesting(bootstrapContents) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("Failed to create xDS client: %v", err) |
| 48 | + } |
| 49 | + defer client.Close() |
| 50 | + |
| 51 | + // Configure the management server to respond with route config resources. |
| 52 | + resources := e2e.UpdateOptions{ |
| 53 | + NodeID: nodeID, |
| 54 | + Routes: []*v3routepb.RouteConfiguration{ |
| 55 | + e2e.DefaultRouteConfig(rdsName, ldsName, cdsName), |
| 56 | + e2e.DefaultRouteConfig(rdsNameNewStyle, ldsNameNewStyle, cdsName), |
| 57 | + }, |
| 58 | + SkipValidation: true, |
| 59 | + } |
| 60 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 61 | + defer cancel() |
| 62 | + if err := mgmtServer.Update(ctx, resources); err != nil { |
| 63 | + t.Fatalf("Failed to update management server with resources: %v, err: %v", resources, err) |
| 64 | + } |
| 65 | + |
| 66 | + // Start a watch for one route configuration resource. From the watch |
| 67 | + // callback of the first resource, register two more watches (one for the |
| 68 | + // same resource name, which would be satisfied from the cache, and another |
| 69 | + // for a different resource name, which would be satisfied from the server). |
| 70 | + updateCh1 := testutils.NewChannel() |
| 71 | + updateCh2 := testutils.NewChannel() |
| 72 | + updateCh3 := testutils.NewChannel() |
| 73 | + var rdsCancel2, rdsCancel3 func() |
| 74 | + rdsCancel1 := client.WatchRouteConfig(rdsName, func(u xdsresource.RouteConfigUpdate, err error) { |
| 75 | + updateCh1.Send(xdsresource.RouteConfigUpdateErrTuple{Update: u, Err: err}) |
| 76 | + // Watch for the same resource name. |
| 77 | + rdsCancel2 = client.WatchRouteConfig(rdsName, func(u xdsresource.RouteConfigUpdate, err error) { |
| 78 | + updateCh2.Send(xdsresource.RouteConfigUpdateErrTuple{Update: u, Err: err}) |
| 79 | + }) |
| 80 | + t.Cleanup(rdsCancel2) |
| 81 | + // Watch for a different resource name. |
| 82 | + rdsCancel3 = client.WatchRouteConfig(rdsNameNewStyle, func(u xdsresource.RouteConfigUpdate, err error) { |
| 83 | + updateCh3.Send(xdsresource.RouteConfigUpdateErrTuple{Update: u, Err: err}) |
| 84 | + rdsCancel3() |
| 85 | + }) |
| 86 | + t.Cleanup(rdsCancel3) |
| 87 | + }) |
| 88 | + // defer rdsCancel1() |
| 89 | + t.Cleanup(rdsCancel1) |
| 90 | + |
| 91 | + // Verify the contents of the received update for the all watchers. |
| 92 | + wantUpdate12 := xdsresource.RouteConfigUpdateErrTuple{ |
| 93 | + Update: xdsresource.RouteConfigUpdate{ |
| 94 | + VirtualHosts: []*xdsresource.VirtualHost{ |
| 95 | + { |
| 96 | + Domains: []string{ldsName}, |
| 97 | + Routes: []*xdsresource.Route{ |
| 98 | + { |
| 99 | + Prefix: newStringP("/"), |
| 100 | + ActionType: xdsresource.RouteActionRoute, |
| 101 | + WeightedClusters: map[string]xdsresource.WeightedCluster{cdsName: {Weight: 1}}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + wantUpdate3 := xdsresource.RouteConfigUpdateErrTuple{ |
| 109 | + Update: xdsresource.RouteConfigUpdate{ |
| 110 | + VirtualHosts: []*xdsresource.VirtualHost{ |
| 111 | + { |
| 112 | + Domains: []string{ldsNameNewStyle}, |
| 113 | + Routes: []*xdsresource.Route{ |
| 114 | + { |
| 115 | + Prefix: newStringP("/"), |
| 116 | + ActionType: xdsresource.RouteActionRoute, |
| 117 | + WeightedClusters: map[string]xdsresource.WeightedCluster{cdsName: {Weight: 1}}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | + if err := verifyRouteConfigUpdate(ctx, updateCh1, wantUpdate12); err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + if err := verifyRouteConfigUpdate(ctx, updateCh2, wantUpdate12); err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + if err := verifyRouteConfigUpdate(ctx, updateCh3, wantUpdate3); err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | +} |
0 commit comments