|
| 1 | +/* |
| 2 | + * Copyright 2023 gRPC authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +import { experimental } from '../src'; |
| 19 | +import * as assert from 'assert'; |
| 20 | +import parseLoadBalancingConfig = experimental.parseLoadBalancingConfig; |
| 21 | + |
| 22 | +/** |
| 23 | + * Describes a test case for config parsing. input is passed to |
| 24 | + * parseLoadBalancingConfig. If error is set, the expectation is that that |
| 25 | + * operation throws an error with a matching message. Otherwise, toJsonObject |
| 26 | + * is called on the result, and it is expected to match output, or input if |
| 27 | + * output is unset. |
| 28 | + */ |
| 29 | +interface TestCase { |
| 30 | + name: string; |
| 31 | + input: object, |
| 32 | + output?: object; |
| 33 | + error?: RegExp; |
| 34 | +} |
| 35 | + |
| 36 | +/* The main purpose of these tests is to verify that configs that are expected |
| 37 | + * to be valid parse successfully, and configs that are expected to be invalid |
| 38 | + * throw errors. The specific output of this parsing is a lower priority |
| 39 | + * concern. |
| 40 | + * Note: some tests have an expected output that is different from the output, |
| 41 | + * but all non-error tests additionally verify that parsing the output again |
| 42 | + * produces the same output. */ |
| 43 | +const allTestCases: {[lbPolicyName: string]: TestCase[]} = { |
| 44 | + pick_first: [ |
| 45 | + { |
| 46 | + name: 'no fields set', |
| 47 | + input: {}, |
| 48 | + output: { |
| 49 | + shuffleAddressList: false |
| 50 | + } |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'shuffleAddressList set', |
| 54 | + input: { |
| 55 | + shuffleAddressList: true |
| 56 | + } |
| 57 | + } |
| 58 | + ], |
| 59 | + round_robin: [ |
| 60 | + { |
| 61 | + name: 'no fields set', |
| 62 | + input: {} |
| 63 | + } |
| 64 | + ], |
| 65 | + outlier_detection: [ |
| 66 | + { |
| 67 | + name: 'only required fields set', |
| 68 | + input: { |
| 69 | + child_policy: [{round_robin: {}}] |
| 70 | + }, |
| 71 | + output: { |
| 72 | + interval: { |
| 73 | + seconds: 10, |
| 74 | + nanos: 0 |
| 75 | + }, |
| 76 | + base_ejection_time: { |
| 77 | + seconds: 30, |
| 78 | + nanos: 0 |
| 79 | + }, |
| 80 | + max_ejection_time: { |
| 81 | + seconds: 300, |
| 82 | + nanos: 0 |
| 83 | + }, |
| 84 | + max_ejection_percent: 10, |
| 85 | + success_rate_ejection: undefined, |
| 86 | + failure_percentage_ejection: undefined, |
| 87 | + child_policy: [{round_robin: {}}] |
| 88 | + } |
| 89 | + }, |
| 90 | + { |
| 91 | + name: 'all optional fields undefined', |
| 92 | + input: { |
| 93 | + interval: undefined, |
| 94 | + base_ejection_time: undefined, |
| 95 | + max_ejection_time: undefined, |
| 96 | + max_ejection_percent: undefined, |
| 97 | + success_rate_ejection: undefined, |
| 98 | + failure_percentage_ejection: undefined, |
| 99 | + child_policy: [{round_robin: {}}] |
| 100 | + }, |
| 101 | + output: { |
| 102 | + interval: { |
| 103 | + seconds: 10, |
| 104 | + nanos: 0 |
| 105 | + }, |
| 106 | + base_ejection_time: { |
| 107 | + seconds: 30, |
| 108 | + nanos: 0 |
| 109 | + }, |
| 110 | + max_ejection_time: { |
| 111 | + seconds: 300, |
| 112 | + nanos: 0 |
| 113 | + }, |
| 114 | + max_ejection_percent: 10, |
| 115 | + success_rate_ejection: undefined, |
| 116 | + failure_percentage_ejection: undefined, |
| 117 | + child_policy: [{round_robin: {}}] |
| 118 | + } |
| 119 | + }, |
| 120 | + { |
| 121 | + name: 'empty ejection configs', |
| 122 | + input: { |
| 123 | + success_rate_ejection: {}, |
| 124 | + failure_percentage_ejection: {}, |
| 125 | + child_policy: [{round_robin: {}}] |
| 126 | + }, |
| 127 | + output: { |
| 128 | + interval: { |
| 129 | + seconds: 10, |
| 130 | + nanos: 0 |
| 131 | + }, |
| 132 | + base_ejection_time: { |
| 133 | + seconds: 30, |
| 134 | + nanos: 0 |
| 135 | + }, |
| 136 | + max_ejection_time: { |
| 137 | + seconds: 300, |
| 138 | + nanos: 0 |
| 139 | + }, |
| 140 | + max_ejection_percent: 10, |
| 141 | + success_rate_ejection: { |
| 142 | + stdev_factor: 1900, |
| 143 | + enforcement_percentage: 100, |
| 144 | + minimum_hosts: 5, |
| 145 | + request_volume: 100 |
| 146 | + }, |
| 147 | + failure_percentage_ejection: { |
| 148 | + threshold: 85, |
| 149 | + enforcement_percentage: 100, |
| 150 | + minimum_hosts: 5, |
| 151 | + request_volume: 50, |
| 152 | + }, |
| 153 | + child_policy: [{round_robin: {}}] |
| 154 | + } |
| 155 | + }, |
| 156 | + { |
| 157 | + name: 'all fields populated', |
| 158 | + input: { |
| 159 | + interval: { |
| 160 | + seconds: 20, |
| 161 | + nanos: 0 |
| 162 | + }, |
| 163 | + base_ejection_time: { |
| 164 | + seconds: 40, |
| 165 | + nanos: 0 |
| 166 | + }, |
| 167 | + max_ejection_time: { |
| 168 | + seconds: 400, |
| 169 | + nanos: 0 |
| 170 | + }, |
| 171 | + max_ejection_percent: 20, |
| 172 | + success_rate_ejection: { |
| 173 | + stdev_factor: 1800, |
| 174 | + enforcement_percentage: 90, |
| 175 | + minimum_hosts: 4, |
| 176 | + request_volume: 200 |
| 177 | + }, |
| 178 | + failure_percentage_ejection: { |
| 179 | + threshold: 95, |
| 180 | + enforcement_percentage: 90, |
| 181 | + minimum_hosts: 4, |
| 182 | + request_volume: 60, |
| 183 | + }, |
| 184 | + child_policy: [{round_robin: {}}] |
| 185 | + |
| 186 | + } |
| 187 | + } |
| 188 | + ] |
| 189 | +} |
| 190 | + |
| 191 | +describe('Load balancing policy config parsing', () => { |
| 192 | + for (const [lbPolicyName, testCases] of Object.entries(allTestCases)) { |
| 193 | + describe(lbPolicyName, () => { |
| 194 | + for (const testCase of testCases) { |
| 195 | + it(testCase.name, () => { |
| 196 | + const lbConfigInput = {[lbPolicyName]: testCase.input}; |
| 197 | + if (testCase.error) { |
| 198 | + assert.throws(() => { |
| 199 | + parseLoadBalancingConfig(lbConfigInput); |
| 200 | + }, testCase.error); |
| 201 | + } else { |
| 202 | + const expectedOutput = testCase.output ?? testCase.input; |
| 203 | + const parsedJson = parseLoadBalancingConfig(lbConfigInput).toJsonObject(); |
| 204 | + assert.deepStrictEqual(parsedJson, {[lbPolicyName]: expectedOutput}); |
| 205 | + // Test idempotency |
| 206 | + assert.deepStrictEqual(parseLoadBalancingConfig(parsedJson).toJsonObject(), parsedJson); |
| 207 | + } |
| 208 | + }); |
| 209 | + } |
| 210 | + }); |
| 211 | + } |
| 212 | +}); |
0 commit comments