metadata_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. *
  3. * Copyright 2014 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 metadata
  19. import (
  20. "context"
  21. "reflect"
  22. "strconv"
  23. "testing"
  24. "time"
  25. "google.golang.org/grpc/internal/grpctest"
  26. )
  27. const defaultTestTimeout = 10 * time.Second
  28. type s struct {
  29. grpctest.Tester
  30. }
  31. func Test(t *testing.T) {
  32. grpctest.RunSubTests(t, s{})
  33. }
  34. func (s) TestPairsMD(t *testing.T) {
  35. for _, test := range []struct {
  36. // input
  37. kv []string
  38. // output
  39. md MD
  40. }{
  41. {[]string{}, MD{}},
  42. {[]string{"k1", "v1", "k1", "v2"}, MD{"k1": []string{"v1", "v2"}}},
  43. } {
  44. md := Pairs(test.kv...)
  45. if !reflect.DeepEqual(md, test.md) {
  46. t.Fatalf("Pairs(%v) = %v, want %v", test.kv, md, test.md)
  47. }
  48. }
  49. }
  50. func (s) TestCopy(t *testing.T) {
  51. const key, val = "key", "val"
  52. orig := Pairs(key, val)
  53. cpy := orig.Copy()
  54. if !reflect.DeepEqual(orig, cpy) {
  55. t.Errorf("copied value not equal to the original, got %v, want %v", cpy, orig)
  56. }
  57. orig[key][0] = "foo"
  58. if v := cpy[key][0]; v != val {
  59. t.Errorf("change in original should not affect copy, got %q, want %q", v, val)
  60. }
  61. }
  62. func (s) TestJoin(t *testing.T) {
  63. for _, test := range []struct {
  64. mds []MD
  65. want MD
  66. }{
  67. {[]MD{}, MD{}},
  68. {[]MD{Pairs("foo", "bar")}, Pairs("foo", "bar")},
  69. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz")}, Pairs("foo", "bar", "foo", "baz")},
  70. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz"), Pairs("zip", "zap")}, Pairs("foo", "bar", "foo", "baz", "zip", "zap")},
  71. } {
  72. md := Join(test.mds...)
  73. if !reflect.DeepEqual(md, test.want) {
  74. t.Errorf("context's metadata is %v, want %v", md, test.want)
  75. }
  76. }
  77. }
  78. func (s) TestGet(t *testing.T) {
  79. for _, test := range []struct {
  80. md MD
  81. key string
  82. wantVals []string
  83. }{
  84. {md: Pairs("My-Optional-Header", "42"), key: "My-Optional-Header", wantVals: []string{"42"}},
  85. {md: Pairs("Header", "42", "Header", "43", "Header", "44", "other", "1"), key: "HEADER", wantVals: []string{"42", "43", "44"}},
  86. {md: Pairs("HEADER", "10"), key: "HEADER", wantVals: []string{"10"}},
  87. } {
  88. vals := test.md.Get(test.key)
  89. if !reflect.DeepEqual(vals, test.wantVals) {
  90. t.Errorf("value of metadata %v is %v, want %v", test.key, vals, test.wantVals)
  91. }
  92. }
  93. }
  94. func (s) TestSet(t *testing.T) {
  95. for _, test := range []struct {
  96. md MD
  97. setKey string
  98. setVals []string
  99. want MD
  100. }{
  101. {
  102. md: Pairs("My-Optional-Header", "42", "other-key", "999"),
  103. setKey: "Other-Key",
  104. setVals: []string{"1"},
  105. want: Pairs("my-optional-header", "42", "other-key", "1"),
  106. },
  107. {
  108. md: Pairs("My-Optional-Header", "42"),
  109. setKey: "Other-Key",
  110. setVals: []string{"1", "2", "3"},
  111. want: Pairs("my-optional-header", "42", "other-key", "1", "other-key", "2", "other-key", "3"),
  112. },
  113. {
  114. md: Pairs("My-Optional-Header", "42"),
  115. setKey: "Other-Key",
  116. setVals: []string{},
  117. want: Pairs("my-optional-header", "42"),
  118. },
  119. } {
  120. test.md.Set(test.setKey, test.setVals...)
  121. if !reflect.DeepEqual(test.md, test.want) {
  122. t.Errorf("value of metadata is %v, want %v", test.md, test.want)
  123. }
  124. }
  125. }
  126. func (s) TestAppend(t *testing.T) {
  127. for _, test := range []struct {
  128. md MD
  129. appendKey string
  130. appendVals []string
  131. want MD
  132. }{
  133. {
  134. md: Pairs("My-Optional-Header", "42"),
  135. appendKey: "Other-Key",
  136. appendVals: []string{"1"},
  137. want: Pairs("my-optional-header", "42", "other-key", "1"),
  138. },
  139. {
  140. md: Pairs("My-Optional-Header", "42"),
  141. appendKey: "my-OptIoNal-HeAder",
  142. appendVals: []string{"1", "2", "3"},
  143. want: Pairs("my-optional-header", "42", "my-optional-header", "1",
  144. "my-optional-header", "2", "my-optional-header", "3"),
  145. },
  146. {
  147. md: Pairs("My-Optional-Header", "42"),
  148. appendKey: "my-OptIoNal-HeAder",
  149. appendVals: []string{},
  150. want: Pairs("my-optional-header", "42"),
  151. },
  152. } {
  153. test.md.Append(test.appendKey, test.appendVals...)
  154. if !reflect.DeepEqual(test.md, test.want) {
  155. t.Errorf("value of metadata is %v, want %v", test.md, test.want)
  156. }
  157. }
  158. }
  159. func (s) TestDelete(t *testing.T) {
  160. for _, test := range []struct {
  161. md MD
  162. deleteKey string
  163. want MD
  164. }{
  165. {
  166. md: Pairs("My-Optional-Header", "42"),
  167. deleteKey: "My-Optional-Header",
  168. want: Pairs(),
  169. },
  170. {
  171. md: Pairs("My-Optional-Header", "42"),
  172. deleteKey: "Other-Key",
  173. want: Pairs("my-optional-header", "42"),
  174. },
  175. {
  176. md: Pairs("My-Optional-Header", "42"),
  177. deleteKey: "my-OptIoNal-HeAder",
  178. want: Pairs(),
  179. },
  180. } {
  181. test.md.Delete(test.deleteKey)
  182. if !reflect.DeepEqual(test.md, test.want) {
  183. t.Errorf("value of metadata is %v, want %v", test.md, test.want)
  184. }
  185. }
  186. }
  187. func (s) TestValueFromIncomingContext(t *testing.T) {
  188. md := Pairs(
  189. "X-My-Header-1", "42",
  190. "X-My-Header-2", "43-1",
  191. "X-My-Header-2", "43-2",
  192. "x-my-header-3", "44",
  193. )
  194. ctx := NewIncomingContext(context.Background(), md)
  195. for _, test := range []struct {
  196. key string
  197. want []string
  198. }{
  199. {
  200. key: "x-my-header-1",
  201. want: []string{"42"},
  202. },
  203. {
  204. key: "x-my-header-2",
  205. want: []string{"43-1", "43-2"},
  206. },
  207. {
  208. key: "x-my-header-3",
  209. want: []string{"44"},
  210. },
  211. {
  212. key: "x-unknown",
  213. want: nil,
  214. },
  215. } {
  216. v := ValueFromIncomingContext(ctx, test.key)
  217. if !reflect.DeepEqual(v, test.want) {
  218. t.Errorf("value of metadata is %v, want %v", v, test.want)
  219. }
  220. }
  221. }
  222. func (s) TestAppendToOutgoingContext(t *testing.T) {
  223. // Pre-existing metadata
  224. tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  225. defer cancel()
  226. ctx := NewOutgoingContext(tCtx, Pairs("k1", "v1", "k2", "v2"))
  227. ctx = AppendToOutgoingContext(ctx, "k1", "v3")
  228. ctx = AppendToOutgoingContext(ctx, "k1", "v4")
  229. md, ok := FromOutgoingContext(ctx)
  230. if !ok {
  231. t.Errorf("Expected MD to exist in ctx, but got none")
  232. }
  233. want := Pairs("k1", "v1", "k1", "v3", "k1", "v4", "k2", "v2")
  234. if !reflect.DeepEqual(md, want) {
  235. t.Errorf("context's metadata is %v, want %v", md, want)
  236. }
  237. // No existing metadata
  238. ctx = AppendToOutgoingContext(tCtx, "k1", "v1")
  239. md, ok = FromOutgoingContext(ctx)
  240. if !ok {
  241. t.Errorf("Expected MD to exist in ctx, but got none")
  242. }
  243. want = Pairs("k1", "v1")
  244. if !reflect.DeepEqual(md, want) {
  245. t.Errorf("context's metadata is %v, want %v", md, want)
  246. }
  247. }
  248. func (s) TestAppendToOutgoingContext_Repeated(t *testing.T) {
  249. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  250. defer cancel()
  251. for i := 0; i < 100; i = i + 2 {
  252. ctx1 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i))
  253. ctx2 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i+1))
  254. md1, _ := FromOutgoingContext(ctx1)
  255. md2, _ := FromOutgoingContext(ctx2)
  256. if reflect.DeepEqual(md1, md2) {
  257. t.Fatalf("md1, md2 = %v, %v; should not be equal", md1, md2)
  258. }
  259. ctx = ctx1
  260. }
  261. }
  262. func (s) TestAppendToOutgoingContext_FromKVSlice(t *testing.T) {
  263. const k, v = "a", "b"
  264. kv := []string{k, v}
  265. tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  266. defer cancel()
  267. ctx := AppendToOutgoingContext(tCtx, kv...)
  268. md, _ := FromOutgoingContext(ctx)
  269. if md[k][0] != v {
  270. t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
  271. }
  272. kv[1] = "xxx"
  273. md, _ = FromOutgoingContext(ctx)
  274. if md[k][0] != v {
  275. t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
  276. }
  277. }
  278. // Old/slow approach to adding metadata to context
  279. func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) {
  280. // TODO: Add in N=1-100 tests once Go1.6 support is removed.
  281. const num = 10
  282. for n := 0; n < b.N; n++ {
  283. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  284. defer cancel()
  285. for i := 0; i < num; i++ {
  286. md, _ := FromOutgoingContext(ctx)
  287. NewOutgoingContext(ctx, Join(Pairs("k1", "v1", "k2", "v2"), md))
  288. }
  289. }
  290. }
  291. // Newer/faster approach to adding metadata to context
  292. func BenchmarkAppendToOutgoingContext(b *testing.B) {
  293. const num = 10
  294. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  295. defer cancel()
  296. for n := 0; n < b.N; n++ {
  297. for i := 0; i < num; i++ {
  298. ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
  299. }
  300. }
  301. }
  302. func BenchmarkFromOutgoingContext(b *testing.B) {
  303. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  304. defer cancel()
  305. ctx = NewOutgoingContext(ctx, MD{"k3": {"v3", "v4"}})
  306. ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
  307. for n := 0; n < b.N; n++ {
  308. FromOutgoingContext(ctx)
  309. }
  310. }
  311. func BenchmarkFromIncomingContext(b *testing.B) {
  312. md := Pairs("X-My-Header-1", "42")
  313. ctx := NewIncomingContext(context.Background(), md)
  314. b.ResetTimer()
  315. for n := 0; n < b.N; n++ {
  316. FromIncomingContext(ctx)
  317. }
  318. }
  319. func BenchmarkValueFromIncomingContext(b *testing.B) {
  320. md := Pairs("X-My-Header-1", "42")
  321. ctx := NewIncomingContext(context.Background(), md)
  322. b.ResetTimer()
  323. for n := 0; n < b.N; n++ {
  324. ValueFromIncomingContext(ctx, "x-my-header-1")
  325. }
  326. }