11/*
22 *
3- * Copyright 2020 gRPC authors.
3+ * Copyright 2026 gRPC authors.
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
1616 *
1717 */
1818
19- package hierarchy
19+ package hierarchy_test
2020
2121import (
2222 "testing"
2323
2424 "github.com/google/go-cmp/cmp"
2525 "google.golang.org/grpc/attributes"
26+ "google.golang.org/grpc/internal/hierarchy"
2627 "google.golang.org/grpc/resolver"
2728)
2829
@@ -39,15 +40,13 @@ func TestFromEndpoint(t *testing.T) {
3940 },
4041 {
4142 name : "set" ,
42- ep : resolver.Endpoint {
43- Attributes : attributes .New (pathKey , pathValue {"a" , "b" }),
44- },
43+ ep : hierarchy .SetInEndpoint (resolver.Endpoint {}, []string {"a" , "b" }),
4544 want : []string {"a" , "b" },
4645 },
4746 }
4847 for _ , tt := range tests {
4948 t .Run (tt .name , func (t * testing.T ) {
50- if got := FromEndpoint (tt .ep ); ! cmp .Equal (got , tt .want ) {
49+ if got := hierarchy . FromEndpoint (tt .ep ); ! cmp .Equal (got , tt .want ) {
5150 t .Errorf ("FromEndpoint() = %v, want %v" , got , tt .want )
5251 }
5352 })
@@ -67,16 +66,14 @@ func TestSetInEndpoint(t *testing.T) {
6766 },
6867 {
6968 name : "before is set" ,
70- ep : resolver.Endpoint {
71- Attributes : attributes .New (pathKey , pathValue {"before" , "a" , "b" }),
72- },
69+ ep : hierarchy .SetInEndpoint (resolver.Endpoint {}, []string {"before" , "a" , "b" }),
7370 path : []string {"a" , "b" },
7471 },
7572 }
7673 for _ , tt := range tests {
7774 t .Run (tt .name , func (t * testing.T ) {
78- newEP := SetInEndpoint (tt .ep , tt .path )
79- newPath := FromEndpoint (newEP )
75+ newEP := hierarchy . SetInEndpoint (tt .ep , tt .path )
76+ newPath := hierarchy . FromEndpoint (newEP )
8077 if ! cmp .Equal (newPath , tt .path ) {
8178 t .Errorf ("path after SetInEndpoint() = %v, want %v" , newPath , tt .path )
8279 }
@@ -93,59 +90,42 @@ func TestGroup(t *testing.T) {
9390 {
9491 name : "all with hierarchy" ,
9592 eps : []resolver.Endpoint {
96- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes . New ( pathKey , pathValue {"a" })} ,
97- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes . New ( pathKey , pathValue {"a" })} ,
98- {Addresses : []resolver.Address {{Addr : "b0" }}, Attributes : attributes . New ( pathKey , pathValue {"b" })} ,
99- {Addresses : []resolver.Address {{Addr : "b1" }}, Attributes : attributes . New ( pathKey , pathValue {"b" })} ,
93+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a0" }}}, [] string {"a" }),
94+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a1" }}}, [] string {"a" }),
95+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "b0" }}}, [] string {"b" }),
96+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "b1" }}}, [] string {"b" }),
10097 },
10198 want : map [string ][]resolver.Endpoint {
10299 "a" : {
103- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes . New ( pathKey , pathValue {})} ,
104- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes . New ( pathKey , pathValue {})} ,
100+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a0" }}}, nil ) ,
101+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a1" }}}, nil ) ,
105102 },
106103 "b" : {
107- {Addresses : []resolver.Address {{Addr : "b0" }}, Attributes : attributes . New ( pathKey , pathValue {})} ,
108- {Addresses : []resolver.Address {{Addr : "b1" }}, Attributes : attributes . New ( pathKey , pathValue {})} ,
104+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "b0" }}}, nil ) ,
105+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "b1" }}}, nil ) ,
109106 },
110107 },
111108 },
112109 {
113110 // Endpoints without hierarchy are ignored.
114111 name : "without hierarchy" ,
115112 eps : []resolver.Endpoint {
116- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes . New ( pathKey , pathValue {"a" })} ,
117- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes . New ( pathKey , pathValue {"a" })} ,
113+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a0" }}}, [] string {"a" }),
114+ hierarchy . SetInEndpoint (resolver. Endpoint {Addresses : []resolver.Address {{Addr : "a1" }}}, [] string {"a" }),
118115 {Addresses : []resolver.Address {{Addr : "b0" }}, Attributes : nil },
119116 {Addresses : []resolver.Address {{Addr : "b1" }}, Attributes : nil },
120117 },
121118 want : map [string ][]resolver.Endpoint {
122119 "a" : {
123- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes .New (pathKey , pathValue {})},
124- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes .New (pathKey , pathValue {})},
125- },
126- },
127- },
128- {
129- // If hierarchy is set to a wrong type (which should never happen),
130- // the endpoint is ignored.
131- name : "wrong type" ,
132- eps : []resolver.Endpoint {
133- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes .New (pathKey , pathValue {"a" })},
134- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes .New (pathKey , pathValue {"a" })},
135- {Addresses : []resolver.Address {{Addr : "b0" }}, Attributes : attributes .New (pathKey , "b" )},
136- {Addresses : []resolver.Address {{Addr : "b1" }}, Attributes : attributes .New (pathKey , 314 )},
137- },
138- want : map [string ][]resolver.Endpoint {
139- "a" : {
140- {Addresses : []resolver.Address {{Addr : "a0" }}, Attributes : attributes .New (pathKey , pathValue {})},
141- {Addresses : []resolver.Address {{Addr : "a1" }}, Attributes : attributes .New (pathKey , pathValue {})},
120+ hierarchy .SetInEndpoint (resolver.Endpoint {Addresses : []resolver.Address {{Addr : "a0" }}}, nil ),
121+ hierarchy .SetInEndpoint (resolver.Endpoint {Addresses : []resolver.Address {{Addr : "a1" }}}, nil ),
142122 },
143123 },
144124 },
145125 }
146126 for _ , tt := range tests {
147127 t .Run (tt .name , func (t * testing.T ) {
148- if got := Group (tt .eps ); ! cmp .Equal (got , tt .want , cmp .AllowUnexported (attributes.Attributes {})) {
128+ if got := hierarchy . Group (tt .eps ); ! cmp .Equal (got , tt .want , cmp .AllowUnexported (attributes.Attributes {})) {
149129 t .Errorf ("Group() = %v, want %v" , got , tt .want )
150130 t .Errorf ("diff: %v" , cmp .Diff (got , tt .want , cmp .AllowUnexported (attributes.Attributes {})))
151131 }
@@ -154,7 +134,7 @@ func TestGroup(t *testing.T) {
154134}
155135
156136func TestGroupE2E (t * testing.T ) {
157- hierarchy := map [string ]map [string ][]string {
137+ testHierarchy := map [string ]map [string ][]string {
158138 "p0" : {
159139 "wt0" : {"addr0" , "addr1" },
160140 "wt1" : {"addr2" , "addr3" },
@@ -166,32 +146,29 @@ func TestGroupE2E(t *testing.T) {
166146 }
167147
168148 var epsWithHierarchy []resolver.Endpoint
169- for p , wts := range hierarchy {
170- path1 := pathValue {p }
149+ for p , wts := range testHierarchy {
150+ path1 := [] string {p }
171151 for wt , addrs := range wts {
172- path2 := append (pathValue (nil ), path1 ... )
152+ path2 := append ([] string (nil ), path1 ... )
173153 path2 = append (path2 , wt )
174154 for _ , addr := range addrs {
175- a := resolver.Endpoint {
176- Addresses : []resolver.Address {{Addr : addr }},
177- Attributes : attributes .New (pathKey , path2 ),
178- }
155+ a := hierarchy .SetInEndpoint (resolver.Endpoint {Addresses : []resolver.Address {{Addr : addr }}}, path2 )
179156 epsWithHierarchy = append (epsWithHierarchy , a )
180157 }
181158 }
182159 }
183160
184161 gotHierarchy := make (map [string ]map [string ][]string )
185- for p1 , wts := range Group (epsWithHierarchy ) {
162+ for p1 , wts := range hierarchy . Group (epsWithHierarchy ) {
186163 gotHierarchy [p1 ] = make (map [string ][]string )
187- for p2 , eps := range Group (wts ) {
164+ for p2 , eps := range hierarchy . Group (wts ) {
188165 for _ , ep := range eps {
189166 gotHierarchy [p1 ][p2 ] = append (gotHierarchy [p1 ][p2 ], ep .Addresses [0 ].Addr )
190167 }
191168 }
192169 }
193170
194- if ! cmp .Equal (gotHierarchy , hierarchy ) {
195- t .Errorf ("diff: %v" , cmp .Diff (gotHierarchy , hierarchy ))
171+ if ! cmp .Equal (gotHierarchy , testHierarchy ) {
172+ t .Errorf ("diff: %v" , cmp .Diff (gotHierarchy , testHierarchy ))
196173 }
197174}
0 commit comments