service_config_test.go 8.2 KB

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