|
| 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 autosharding |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/json" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | + "google.golang.org/grpc/internal/grpctest" |
| 28 | + iserviceconfig "google.golang.org/grpc/internal/serviceconfig" |
| 29 | + "google.golang.org/grpc/serviceconfig" |
| 30 | +) |
| 31 | + |
| 32 | +type s struct { |
| 33 | + grpctest.Tester |
| 34 | +} |
| 35 | + |
| 36 | +func Test(t *testing.T) { |
| 37 | + grpctest.RunSubTests(t, s{}) |
| 38 | +} |
| 39 | + |
| 40 | +func (s) TestParseConfig_Success(t *testing.T) { |
| 41 | + parser := bb{} |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + input string |
| 45 | + wantCfg serviceconfig.LoadBalancingConfig |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "all-fields", |
| 49 | + input: `{ |
| 50 | + "channelFactoryKey": "factory-key", |
| 51 | + "autoshardingTarget": "target", |
| 52 | + "keyHeaderName": "header", |
| 53 | + "enableFallback": true, |
| 54 | + "initialAssignmentTimeout": "30s" |
| 55 | + }`, |
| 56 | + wantCfg: &lbConfig{ |
| 57 | + ChannelFactoryKey: "factory-key", |
| 58 | + AutoShardingTarget: "target", |
| 59 | + KeyHeaderName: "header", |
| 60 | + EnableFallback: true, |
| 61 | + InitialAssignmentTimeout: iserviceconfig.Duration(30 * time.Second), |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "default-timeout", |
| 66 | + input: `{ |
| 67 | + "channelFactoryKey": "factory-key", |
| 68 | + "autoshardingTarget": "target", |
| 69 | + "keyHeaderName": "header" |
| 70 | + }`, |
| 71 | + wantCfg: &lbConfig{ |
| 72 | + ChannelFactoryKey: "factory-key", |
| 73 | + AutoShardingTarget: "target", |
| 74 | + KeyHeaderName: "header", |
| 75 | + InitialAssignmentTimeout: iserviceconfig.Duration(60 * time.Second), |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, test := range tests { |
| 80 | + t.Run(test.name, func(t *testing.T) { |
| 81 | + gotCfg, err := parser.ParseConfig(json.RawMessage(test.input)) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("ParseConfig() error = %v, want nil", err) |
| 84 | + } |
| 85 | + if diff := cmp.Diff(test.wantCfg, gotCfg); diff != "" { |
| 86 | + t.Errorf("ParseConfig() config diff (-want +got):\n%s", diff) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (s) TestParseConfig_Failure(t *testing.T) { |
| 93 | + parser := bb{} |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + input string |
| 97 | + }{ |
| 98 | + { |
| 99 | + name: "invalid-json", |
| 100 | + input: "{{invalidjson{{", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "invalid-duration", |
| 104 | + input: `{ |
| 105 | + "channelFactoryKey": "factory-key", |
| 106 | + "autoshardingTarget": "target", |
| 107 | + "keyHeaderName": "header", |
| 108 | + "initialAssignmentTimeout": "invalid" |
| 109 | + }`, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "missing-channel-factory-key", |
| 113 | + input: `{ |
| 114 | + "autoshardingTarget": "target", |
| 115 | + "keyHeaderName": "header" |
| 116 | + }`, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "missing-autosharding-target", |
| 120 | + input: `{ |
| 121 | + "channelFactoryKey": "factory-key", |
| 122 | + "keyHeaderName": "header" |
| 123 | + }`, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "missing-key-header-name", |
| 127 | + input: `{ |
| 128 | + "channelFactoryKey": "factory-key", |
| 129 | + "autoshardingTarget": "target" |
| 130 | + }`, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "empty-config", |
| 134 | + input: `{}`, |
| 135 | + }, |
| 136 | + } |
| 137 | + for _, test := range tests { |
| 138 | + t.Run(test.name, func(t *testing.T) { |
| 139 | + if _, err := parser.ParseConfig(json.RawMessage(test.input)); err == nil { |
| 140 | + t.Fatalf("ParseConfig() succeeded, want error") |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments