service_config_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. *
  3. * Copyright 2017 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. package grpc
  19. import (
  20. "encoding/json"
  21. "fmt"
  22. "math"
  23. "reflect"
  24. "testing"
  25. "time"
  26. "google.golang.org/grpc/balancer"
  27. "google.golang.org/grpc/serviceconfig"
  28. )
  29. type parseTestCase struct {
  30. scjs string
  31. wantSC *ServiceConfig
  32. wantErr bool
  33. }
  34. func runParseTests(t *testing.T, testCases []parseTestCase) {
  35. t.Helper()
  36. for _, c := range testCases {
  37. scpr := parseServiceConfig(c.scjs)
  38. var sc *ServiceConfig
  39. sc, _ = scpr.Config.(*ServiceConfig)
  40. if !c.wantErr {
  41. c.wantSC.rawJSONString = c.scjs
  42. }
  43. if c.wantErr != (scpr.Err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
  44. t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, scpr.Err, c.wantSC, c.wantErr)
  45. }
  46. }
  47. }
  48. type pbbData struct {
  49. serviceconfig.LoadBalancingConfig
  50. Foo string
  51. Bar int
  52. }
  53. type parseBalancerBuilder struct{}
  54. func (parseBalancerBuilder) Name() string {
  55. return "pbb"
  56. }
  57. func (parseBalancerBuilder) ParseConfig(c json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
  58. d := pbbData{}
  59. if err := json.Unmarshal(c, &d); err != nil {
  60. return nil, err
  61. }
  62. return d, nil
  63. }
  64. func (parseBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
  65. panic("unimplemented")
  66. }
  67. func init() {
  68. balancer.Register(parseBalancerBuilder{})
  69. }
  70. func (s) TestParseLBConfig(t *testing.T) {
  71. testcases := []parseTestCase{
  72. {
  73. `{
  74. "loadBalancingConfig": [{"pbb": { "foo": "hi" } }]
  75. }`,
  76. &ServiceConfig{
  77. Methods: make(map[string]MethodConfig),
  78. lbConfig: &lbConfig{name: "pbb", cfg: pbbData{Foo: "hi"}},
  79. },
  80. false,
  81. },
  82. }
  83. runParseTests(t, testcases)
  84. }
  85. func (s) TestParseNoLBConfigSupported(t *testing.T) {
  86. // We have a loadBalancingConfig field but will not encounter a supported
  87. // policy. The config will be considered invalid in this case.
  88. testcases := []parseTestCase{
  89. {
  90. scjs: `{
  91. "loadBalancingConfig": [{"not_a_balancer1": {} }, {"not_a_balancer2": {}}]
  92. }`,
  93. wantErr: true,
  94. }, {
  95. scjs: `{"loadBalancingConfig": []}`,
  96. wantErr: true,
  97. },
  98. }
  99. runParseTests(t, testcases)
  100. }
  101. func (s) TestParseLoadBalancer(t *testing.T) {
  102. testcases := []parseTestCase{
  103. {
  104. `{
  105. "loadBalancingPolicy": "round_robin",
  106. "methodConfig": [
  107. {
  108. "name": [
  109. {
  110. "service": "foo",
  111. "method": "Bar"
  112. }
  113. ],
  114. "waitForReady": true
  115. }
  116. ]
  117. }`,
  118. &ServiceConfig{
  119. LB: newString("round_robin"),
  120. Methods: map[string]MethodConfig{
  121. "/foo/Bar": {
  122. WaitForReady: newBool(true),
  123. },
  124. },
  125. },
  126. false,
  127. },
  128. {
  129. `{
  130. "loadBalancingPolicy": 1,
  131. "methodConfig": [
  132. {
  133. "name": [
  134. {
  135. "service": "foo",
  136. "method": "Bar"
  137. }
  138. ],
  139. "waitForReady": false
  140. }
  141. ]
  142. }`,
  143. nil,
  144. true,
  145. },
  146. }
  147. runParseTests(t, testcases)
  148. }
  149. func (s) TestParseWaitForReady(t *testing.T) {
  150. testcases := []parseTestCase{
  151. {
  152. `{
  153. "methodConfig": [
  154. {
  155. "name": [
  156. {
  157. "service": "foo",
  158. "method": "Bar"
  159. }
  160. ],
  161. "waitForReady": true
  162. }
  163. ]
  164. }`,
  165. &ServiceConfig{
  166. Methods: map[string]MethodConfig{
  167. "/foo/Bar": {
  168. WaitForReady: newBool(true),
  169. },
  170. },
  171. },
  172. false,
  173. },
  174. {
  175. `{
  176. "methodConfig": [
  177. {
  178. "name": [
  179. {
  180. "service": "foo",
  181. "method": "Bar"
  182. }
  183. ],
  184. "waitForReady": false
  185. }
  186. ]
  187. }`,
  188. &ServiceConfig{
  189. Methods: map[string]MethodConfig{
  190. "/foo/Bar": {
  191. WaitForReady: newBool(false),
  192. },
  193. },
  194. },
  195. false,
  196. },
  197. {
  198. `{
  199. "methodConfig": [
  200. {
  201. "name": [
  202. {
  203. "service": "foo",
  204. "method": "Bar"
  205. }
  206. ],
  207. "waitForReady": fall
  208. },
  209. {
  210. "name": [
  211. {
  212. "service": "foo",
  213. "method": "Bar"
  214. }
  215. ],
  216. "waitForReady": true
  217. }
  218. ]
  219. }`,
  220. nil,
  221. true,
  222. },
  223. }
  224. runParseTests(t, testcases)
  225. }
  226. func (s) TestParseTimeOut(t *testing.T) {
  227. testcases := []parseTestCase{
  228. {
  229. `{
  230. "methodConfig": [
  231. {
  232. "name": [
  233. {
  234. "service": "foo",
  235. "method": "Bar"
  236. }
  237. ],
  238. "timeout": "1s"
  239. }
  240. ]
  241. }`,
  242. &ServiceConfig{
  243. Methods: map[string]MethodConfig{
  244. "/foo/Bar": {
  245. Timeout: newDuration(time.Second),
  246. },
  247. },
  248. },
  249. false,
  250. },
  251. {
  252. `{
  253. "methodConfig": [
  254. {
  255. "name": [
  256. {
  257. "service": "foo",
  258. "method": "Bar"
  259. }
  260. ],
  261. "timeout": "3c"
  262. }
  263. ]
  264. }`,
  265. nil,
  266. true,
  267. },
  268. {
  269. `{
  270. "methodConfig": [
  271. {
  272. "name": [
  273. {
  274. "service": "foo",
  275. "method": "Bar"
  276. }
  277. ],
  278. "timeout": "3c"
  279. },
  280. {
  281. "name": [
  282. {
  283. "service": "foo",
  284. "method": "Bar"
  285. }
  286. ],
  287. "timeout": "1s"
  288. }
  289. ]
  290. }`,
  291. nil,
  292. true,
  293. },
  294. }
  295. runParseTests(t, testcases)
  296. }
  297. func (s) TestParseMsgSize(t *testing.T) {
  298. testcases := []parseTestCase{
  299. {
  300. `{
  301. "methodConfig": [
  302. {
  303. "name": [
  304. {
  305. "service": "foo",
  306. "method": "Bar"
  307. }
  308. ],
  309. "maxRequestMessageBytes": 1024,
  310. "maxResponseMessageBytes": 2048
  311. }
  312. ]
  313. }`,
  314. &ServiceConfig{
  315. Methods: map[string]MethodConfig{
  316. "/foo/Bar": {
  317. MaxReqSize: newInt(1024),
  318. MaxRespSize: newInt(2048),
  319. },
  320. },
  321. },
  322. false,
  323. },
  324. {
  325. `{
  326. "methodConfig": [
  327. {
  328. "name": [
  329. {
  330. "service": "foo",
  331. "method": "Bar"
  332. }
  333. ],
  334. "maxRequestMessageBytes": "1024",
  335. "maxResponseMessageBytes": "2048"
  336. },
  337. {
  338. "name": [
  339. {
  340. "service": "foo",
  341. "method": "Bar"
  342. }
  343. ],
  344. "maxRequestMessageBytes": 1024,
  345. "maxResponseMessageBytes": 2048
  346. }
  347. ]
  348. }`,
  349. nil,
  350. true,
  351. },
  352. }
  353. runParseTests(t, testcases)
  354. }
  355. func (s) TestParseDuration(t *testing.T) {
  356. testCases := []struct {
  357. s *string
  358. want *time.Duration
  359. err bool
  360. }{
  361. {s: nil, want: nil},
  362. {s: newString("1s"), want: newDuration(time.Second)},
  363. {s: newString("-1s"), want: newDuration(-time.Second)},
  364. {s: newString("1.1s"), want: newDuration(1100 * time.Millisecond)},
  365. {s: newString("1.s"), want: newDuration(time.Second)},
  366. {s: newString("1.0s"), want: newDuration(time.Second)},
  367. {s: newString(".002s"), want: newDuration(2 * time.Millisecond)},
  368. {s: newString(".002000s"), want: newDuration(2 * time.Millisecond)},
  369. {s: newString("0.003s"), want: newDuration(3 * time.Millisecond)},
  370. {s: newString("0.000004s"), want: newDuration(4 * time.Microsecond)},
  371. {s: newString("5000.000000009s"), want: newDuration(5000*time.Second + 9*time.Nanosecond)},
  372. {s: newString("4999.999999999s"), want: newDuration(5000*time.Second - time.Nanosecond)},
  373. {s: newString("1"), err: true},
  374. {s: newString("s"), err: true},
  375. {s: newString(".s"), err: true},
  376. {s: newString("1 s"), err: true},
  377. {s: newString(" 1s"), err: true},
  378. {s: newString("1ms"), err: true},
  379. {s: newString("1.1.1s"), err: true},
  380. {s: newString("Xs"), err: true},
  381. {s: newString("as"), err: true},
  382. {s: newString(".0000000001s"), err: true},
  383. {s: newString(fmt.Sprint(math.MaxInt32) + "s"), want: newDuration(math.MaxInt32 * time.Second)},
  384. {s: newString(fmt.Sprint(int64(math.MaxInt32)+1) + "s"), err: true},
  385. }
  386. for _, tc := range testCases {
  387. got, err := parseDuration(tc.s)
  388. if tc.err != (err != nil) ||
  389. (got == nil) != (tc.want == nil) ||
  390. (got != nil && *got != *tc.want) {
  391. wantErr := "<nil>"
  392. if tc.err {
  393. wantErr = "<non-nil error>"
  394. }
  395. s := "<nil>"
  396. if tc.s != nil {
  397. s = `&"` + *tc.s + `"`
  398. }
  399. t.Errorf("parseDuration(%v) = %v, %v; want %v, %v", s, got, err, tc.want, wantErr)
  400. }
  401. }
  402. }
  403. func newBool(b bool) *bool {
  404. return &b
  405. }
  406. func newDuration(b time.Duration) *time.Duration {
  407. return &b
  408. }
  409. func newString(b string) *string {
  410. return &b
  411. }