1
18
19 package priority
20
21 import (
22 "testing"
23
24 "github.com/google/go-cmp/cmp"
25 "google.golang.org/grpc/balancer/roundrobin"
26 internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
27 )
28
29 func TestParseConfig(t *testing.T) {
30 tests := []struct {
31 name string
32 js string
33 want *LBConfig
34 wantErr bool
35 }{
36 {
37 name: "child not found",
38 js: `{
39 "priorities": ["child-1", "child-2", "child-3"],
40 "children": {
41 "child-1": {"config": [{"round_robin":{}}]},
42 "child-3": {"config": [{"round_robin":{}}]}
43 }
44 }
45 `,
46 wantErr: true,
47 },
48 {
49 name: "child not used",
50 js: `{
51 "priorities": ["child-1", "child-2"],
52 "children": {
53 "child-1": {"config": [{"round_robin":{}}]},
54 "child-2": {"config": [{"round_robin":{}}]},
55 "child-3": {"config": [{"round_robin":{}}]}
56 }
57 }
58 `,
59 wantErr: true,
60 },
61 {
62 name: "good",
63 js: `{
64 "priorities": ["child-1", "child-2", "child-3"],
65 "children": {
66 "child-1": {"config": [{"round_robin":{}}], "ignoreReresolutionRequests": true},
67 "child-2": {"config": [{"round_robin":{}}]},
68 "child-3": {"config": [{"round_robin":{}}]}
69 }
70 }
71 `,
72 want: &LBConfig{
73 Children: map[string]*Child{
74 "child-1": {
75 Config: &internalserviceconfig.BalancerConfig{
76 Name: roundrobin.Name,
77 },
78 IgnoreReresolutionRequests: true,
79 },
80 "child-2": {
81 Config: &internalserviceconfig.BalancerConfig{
82 Name: roundrobin.Name,
83 },
84 },
85 "child-3": {
86 Config: &internalserviceconfig.BalancerConfig{
87 Name: roundrobin.Name,
88 },
89 },
90 },
91 Priorities: []string{"child-1", "child-2", "child-3"},
92 },
93 wantErr: false,
94 },
95 }
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98 got, err := parseConfig([]byte(tt.js))
99 if (err != nil) != tt.wantErr {
100 t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
101 return
102 }
103 if diff := cmp.Diff(got, tt.want); diff != "" {
104 t.Errorf("parseConfig() got = %v, want %v, diff: %s", got, tt.want, diff)
105 }
106 })
107 }
108 }
109
View as plain text