Skip to content

Commit 4dd7f55

Browse files
authored
ringhash: port e2e tests from c-core (#7271)
1 parent de51a63 commit 4dd7f55

File tree

5 files changed

+910
-54
lines changed

5 files changed

+910
-54
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
*
3+
* Copyright 2024 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 testutils
20+
21+
import (
22+
"context"
23+
"net"
24+
)
25+
26+
// BlockingDialer is a dialer that waits for Resume() to be called before
27+
// dialing.
28+
type BlockingDialer struct {
29+
dialer *net.Dialer
30+
blockCh chan struct{}
31+
}
32+
33+
// NewBlockingDialer returns a dialer that waits for Resume() to be called
34+
// before dialing.
35+
func NewBlockingDialer() *BlockingDialer {
36+
return &BlockingDialer{
37+
dialer: &net.Dialer{},
38+
blockCh: make(chan struct{}),
39+
}
40+
}
41+
42+
// DialContext implements a context dialer for use with grpc.WithContextDialer
43+
// dial option for a BlockingDialer.
44+
func (d *BlockingDialer) DialContext(ctx context.Context, addr string) (net.Conn, error) {
45+
select {
46+
case <-d.blockCh:
47+
case <-ctx.Done():
48+
return nil, ctx.Err()
49+
}
50+
return d.dialer.DialContext(ctx, "tcp", addr)
51+
}
52+
53+
// Resume unblocks the dialer. It panics if called more than once.
54+
func (d *BlockingDialer) Resume() {
55+
close(d.blockCh)
56+
}

internal/testutils/xds/e2e/clientresources.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ type LocalityOptions struct {
675675
Weight uint32
676676
// Backends is a set of backends belonging to this locality.
677677
Backends []BackendOptions
678+
// Priority is the priority of the locality. Defaults to 0.
679+
Priority uint32
678680
}
679681

680682
// BackendOptions contains options to configure individual backends in a
@@ -686,6 +688,8 @@ type BackendOptions struct {
686688
// Health status of the backend. Default is UNKNOWN which is treated the
687689
// same as HEALTHY.
688690
HealthStatus v3corepb.HealthStatus
691+
// Weight sets the backend weight. Defaults to 1.
692+
Weight uint32
689693
}
690694

691695
// EndpointOptions contains options to configure an Endpoint (or
@@ -708,7 +712,7 @@ type EndpointOptions struct {
708712
func DefaultEndpoint(clusterName string, host string, ports []uint32) *v3endpointpb.ClusterLoadAssignment {
709713
var bOpts []BackendOptions
710714
for _, p := range ports {
711-
bOpts = append(bOpts, BackendOptions{Port: p})
715+
bOpts = append(bOpts, BackendOptions{Port: p, Weight: 1})
712716
}
713717
return EndpointResourceWithOptions(EndpointOptions{
714718
ClusterName: clusterName,
@@ -729,6 +733,10 @@ func EndpointResourceWithOptions(opts EndpointOptions) *v3endpointpb.ClusterLoad
729733
for i, locality := range opts.Localities {
730734
var lbEndpoints []*v3endpointpb.LbEndpoint
731735
for _, b := range locality.Backends {
736+
// Weight defaults to 1.
737+
if b.Weight == 0 {
738+
b.Weight = 1
739+
}
732740
lbEndpoints = append(lbEndpoints, &v3endpointpb.LbEndpoint{
733741
HostIdentifier: &v3endpointpb.LbEndpoint_Endpoint{Endpoint: &v3endpointpb.Endpoint{
734742
Address: &v3corepb.Address{Address: &v3corepb.Address_SocketAddress{
@@ -740,7 +748,7 @@ func EndpointResourceWithOptions(opts EndpointOptions) *v3endpointpb.ClusterLoad
740748
}},
741749
}},
742750
HealthStatus: b.HealthStatus,
743-
LoadBalancingWeight: &wrapperspb.UInt32Value{Value: 1},
751+
LoadBalancingWeight: &wrapperspb.UInt32Value{Value: b.Weight},
744752
})
745753
}
746754

@@ -752,7 +760,7 @@ func EndpointResourceWithOptions(opts EndpointOptions) *v3endpointpb.ClusterLoad
752760
},
753761
LbEndpoints: lbEndpoints,
754762
LoadBalancingWeight: &wrapperspb.UInt32Value{Value: locality.Weight},
755-
Priority: 0,
763+
Priority: locality.Priority,
756764
})
757765
}
758766

xds/internal/balancer/cdsbalancer/cdsbalancer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func registerWrappedCDSPolicy(t *testing.T) chan balancer.Balancer {
196196
// - the nodeID expected by the management server
197197
// - the grpc channel to the test backend service
198198
// - the manual resolver configured on the channel
199-
// - the xDS cient used the grpc channel
199+
// - the xDS client used the grpc channel
200200
// - a channel on which requested cluster resource names are sent
201201
// - a channel used to signal that previously requested cluster resources are
202202
// no longer requested

xds/internal/balancer/clusterresolver/e2e_test/aggregate_cluster_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,18 @@ func makeLogicalDNSClusterResource(name, dnsHost string, dnsPort uint32) *v3clus
7979
// Returns the following:
8080
// - a channel onto which the DNS target being resolved is written to by the
8181
// mock DNS resolver
82-
// - a channel to notify close of the DNS resolver
83-
// - a channel to notify re-resolution requests to the DNS resolver
8482
// - a manual resolver which is used to mock the actual DNS resolution
85-
// - a cleanup function which re-registers the original DNS resolver
86-
func setupDNS() (chan resolver.Target, chan struct{}, chan resolver.ResolveNowOptions, *manual.Resolver, func()) {
83+
func setupDNS(t *testing.T) (chan resolver.Target, *manual.Resolver) {
8784
targetCh := make(chan resolver.Target, 1)
88-
closeCh := make(chan struct{}, 1)
89-
resolveNowCh := make(chan resolver.ResolveNowOptions, 1)
9085

9186
mr := manual.NewBuilderWithScheme("dns")
92-
mr.BuildCallback = func(target resolver.Target, _ resolver.ClientConn, _ resolver.BuildOptions) {
93-
targetCh <- target
94-
}
95-
mr.CloseCallback = func() { closeCh <- struct{}{} }
96-
mr.ResolveNowCallback = func(opts resolver.ResolveNowOptions) { resolveNowCh <- opts }
87+
mr.BuildCallback = func(target resolver.Target, _ resolver.ClientConn, _ resolver.BuildOptions) { targetCh <- target }
9788

9889
dnsResolverBuilder := resolver.Get("dns")
9990
resolver.Register(mr)
10091

101-
return targetCh, closeCh, resolveNowCh, mr, func() { resolver.Register(dnsResolverBuilder) }
92+
t.Cleanup(func() { resolver.Register(dnsResolverBuilder) })
93+
return targetCh, mr
10294
}
10395

10496
// TestAggregateCluster_WithTwoEDSClusters tests the case where the top-level
@@ -471,8 +463,7 @@ func (s) TestAggregateCluster_WithOneDNSCluster_HostnameChange(t *testing.T) {
471463
// cluster. The test verifies that RPCs fail until both clusters are resolved to
472464
// endpoints, and RPCs are routed to the higher priority EDS cluster.
473465
func (s) TestAggregateCluster_WithEDSAndDNS(t *testing.T) {
474-
dnsTargetCh, _, _, dnsR, cleanup1 := setupDNS()
475-
defer cleanup1()
466+
dnsTargetCh, dnsR := setupDNS(t)
476467

477468
// Start an xDS management server that pushes the name of the requested EDS
478469
// resource onto a channel.
@@ -661,8 +652,7 @@ func (s) TestAggregateCluster_SwitchEDSAndDNS(t *testing.T) {
661652
// still successful. This is the expected behavior because the cluster resolver
662653
// policy eats errors from DNS Resolver after it has returned an error.
663654
func (s) TestAggregateCluster_BadEDS_GoodToBadDNS(t *testing.T) {
664-
dnsTargetCh, _, _, dnsR, cleanup1 := setupDNS()
665-
defer cleanup1()
655+
dnsTargetCh, dnsR := setupDNS(t)
666656

667657
// Start an xDS management server.
668658
managementServer, nodeID, bootstrapContents, _, cleanup2 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true})

0 commit comments

Comments
 (0)