metadata_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. "google.golang.org/grpc/internal/grpctest"
  25. )
  26. type s struct {
  27. grpctest.Tester
  28. }
  29. func Test(t *testing.T) {
  30. grpctest.RunSubTests(t, s{})
  31. }
  32. func (s) TestPairsMD(t *testing.T) {
  33. for _, test := range []struct {
  34. // input
  35. kv []string
  36. // output
  37. md MD
  38. }{
  39. {[]string{}, MD{}},
  40. {[]string{"k1", "v1", "k1", "v2"}, MD{"k1": []string{"v1", "v2"}}},
  41. } {
  42. md := Pairs(test.kv...)
  43. if !reflect.DeepEqual(md, test.md) {
  44. t.Fatalf("Pairs(%v) = %v, want %v", test.kv, md, test.md)
  45. }
  46. }
  47. }
  48. func (s) TestCopy(t *testing.T) {
  49. const key, val = "key", "val"
  50. orig := Pairs(key, val)
  51. cpy := orig.Copy()
  52. if !reflect.DeepEqual(orig, cpy) {
  53. t.Errorf("copied value not equal to the original, got %v, want %v", cpy, orig)
  54. }
  55. orig[key][0] = "foo"
  56. if v := cpy[key][0]; v != val {
  57. t.Errorf("change in original should not affect copy, got %q, want %q", v, val)
  58. }
  59. }
  60. func (s) TestJoin(t *testing.T) {
  61. for _, test := range []struct {
  62. mds []MD
  63. want MD
  64. }{
  65. {[]MD{}, MD{}},
  66. {[]MD{Pairs("foo", "bar")}, Pairs("foo", "bar")},
  67. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz")}, Pairs("foo", "bar", "foo", "baz")},
  68. {[]MD{Pairs("foo", "bar"), Pairs("foo", "baz"), Pairs("zip", "zap")}, Pairs("foo", "bar", "foo", "baz", "zip", "zap")},
  69. } {
  70. md := Join(test.mds...)
  71. if !reflect.DeepEqual(md, test.want) {
  72. t.Errorf("context's metadata is %v, want %v", md, test.want)
  73. }
  74. }
  75. }
  76. func (s) TestGet(t *testing.T) {
  77. for _, test := range []struct {
  78. md MD
  79. key string
  80. wantVals []string
  81. }{
  82. {md: Pairs("My-Optional-Header", "42"), key: "My-Optional-Header", wantVals: []string{"42"}},
  83. {md: Pairs("Header", "42", "Header", "43", "Header", "44", "other", "1"), key: "HEADER", wantVals: []string{"42", "43", "44"}},
  84. {md: Pairs("HEADER", "10"), key: "HEADER", wantVals: []string{"10"}},
  85. } {
  86. vals := test.md.Get(test.key)
  87. if !reflect.DeepEqual(vals, test.wantVals) {
  88. t.Errorf("value of metadata %v is %v, want %v", test.key, vals, test.wantVals)
  89. }
  90. }
  91. }
  92. func (s) TestSet(t *testing.T) {
  93. for _, test := range []struct {
  94. md MD
  95. setKey string
  96. setVals []string
  97. want MD
  98. }{
  99. {
  100. md: Pairs("My-Optional-Header", "42", "other-key", "999"),
  101. setKey: "Other-Key",
  102. setVals: []string{"1"},
  103. want: Pairs("my-optional-header", "42", "other-key", "1"),
  104. },
  105. {
  106. md: Pairs("My-Optional-Header", "42"),
  107. setKey: "Other-Key",
  108. setVals: []string{"1", "2", "3"},
  109. want: Pairs("my-optional-header", "42", "other-key", "1", "other-key", "2", "other-key", "3"),
  110. },
  111. {
  112. md: Pairs("My-Optional-Header", "42"),
  113. setKey: "Other-Key",
  114. setVals: []string{},
  115. want: Pairs("my-optional-header", "42"),
  116. },
  117. } {
  118. test.md.Set(test.setKey, test.setVals...)
  119. if !reflect.DeepEqual(test.md, test.want) {
  120. t.Errorf("value of metadata is %v, want %v", test.md, test.want)
  121. }
  122. }
  123. }
  124. func (s) TestAppend(t *testing.T) {
  125. for _, test := range []struct {
  126. md MD
  127. appendKey string
  128. appendVals []string
  129. want MD
  130. }{
  131. {
  132. md: Pairs("My-Optional-Header", "42"),
  133. appendKey: "Other-Key",
  134. appendVals: []string{"1"},
  135. want: Pairs("my-optional-header", "42", "other-key", "1"),
  136. },
  137. {
  138. md: Pairs("My-Optional-Header", "42"),
  139. appendKey: "my-OptIoNal-HeAder",
  140. appendVals: []string{"1", "2", "3"},
  141. want: Pairs("my-optional-header", "42", "my-optional-header", "1",
  142. "my-optional-header", "2", "my-optional-header", "3"),
  143. },
  144. {
  145. md: Pairs("My-Optional-Header", "42"),
  146. appendKey: "my-OptIoNal-HeAder",
  147. appendVals: []string{},
  148. want: Pairs("my-optional-header", "42"),
  149. },
  150. } {
  151. test.md.Append(test.appendKey, test.appendVals...)
  152. if !reflect.DeepEqual(test.md, test.want) {
  153. t.Errorf("value of metadata is %v, want %v", test.md, test.want)
  154. }
  155. }
  156. }
  157. func (s) TestAppendToOutgoingContext(t *testing.T) {
  158. // Pre-existing metadata
  159. ctx := NewOutgoingContext(context.Background(), Pairs("k1", "v1", "k2", "v2"))
  160. ctx = AppendToOutgoingContext(ctx, "k1", "v3")
  161. ctx = AppendToOutgoingContext(ctx, "k1", "v4")
  162. md, ok := FromOutgoingContext(ctx)
  163. if !ok {
  164. t.Errorf("Expected MD to exist in ctx, but got none")
  165. }
  166. want := Pairs("k1", "v1", "k1", "v3", "k1", "v4", "k2", "v2")
  167. if !reflect.DeepEqual(md, want) {
  168. t.Errorf("context's metadata is %v, want %v", md, want)
  169. }
  170. // No existing metadata
  171. ctx = AppendToOutgoingContext(context.Background(), "k1", "v1")
  172. md, ok = FromOutgoingContext(ctx)
  173. if !ok {
  174. t.Errorf("Expected MD to exist in ctx, but got none")
  175. }
  176. want = Pairs("k1", "v1")
  177. if !reflect.DeepEqual(md, want) {
  178. t.Errorf("context's metadata is %v, want %v", md, want)
  179. }
  180. }
  181. func (s) TestAppendToOutgoingContext_Repeated(t *testing.T) {
  182. ctx := context.Background()
  183. for i := 0; i < 100; i = i + 2 {
  184. ctx1 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i))
  185. ctx2 := AppendToOutgoingContext(ctx, "k", strconv.Itoa(i+1))
  186. md1, _ := FromOutgoingContext(ctx1)
  187. md2, _ := FromOutgoingContext(ctx2)
  188. if reflect.DeepEqual(md1, md2) {
  189. t.Fatalf("md1, md2 = %v, %v; should not be equal", md1, md2)
  190. }
  191. ctx = ctx1
  192. }
  193. }
  194. func (s) TestAppendToOutgoingContext_FromKVSlice(t *testing.T) {
  195. const k, v = "a", "b"
  196. kv := []string{k, v}
  197. ctx := AppendToOutgoingContext(context.Background(), kv...)
  198. md, _ := FromOutgoingContext(ctx)
  199. if md[k][0] != v {
  200. t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
  201. }
  202. kv[1] = "xxx"
  203. md, _ = FromOutgoingContext(ctx)
  204. if md[k][0] != v {
  205. t.Fatalf("md[%q] = %q; want %q", k, md[k], v)
  206. }
  207. }
  208. // Old/slow approach to adding metadata to context
  209. func Benchmark_AddingMetadata_ContextManipulationApproach(b *testing.B) {
  210. // TODO: Add in N=1-100 tests once Go1.6 support is removed.
  211. const num = 10
  212. for n := 0; n < b.N; n++ {
  213. ctx := context.Background()
  214. for i := 0; i < num; i++ {
  215. md, _ := FromOutgoingContext(ctx)
  216. NewOutgoingContext(ctx, Join(Pairs("k1", "v1", "k2", "v2"), md))
  217. }
  218. }
  219. }
  220. // Newer/faster approach to adding metadata to context
  221. func BenchmarkAppendToOutgoingContext(b *testing.B) {
  222. const num = 10
  223. for n := 0; n < b.N; n++ {
  224. ctx := context.Background()
  225. for i := 0; i < num; i++ {
  226. ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
  227. }
  228. }
  229. }
  230. func BenchmarkFromOutgoingContext(b *testing.B) {
  231. ctx := context.Background()
  232. ctx = NewOutgoingContext(ctx, MD{"k3": {"v3", "v4"}})
  233. ctx = AppendToOutgoingContext(ctx, "k1", "v1", "k2", "v2")
  234. for n := 0; n < b.N; n++ {
  235. FromOutgoingContext(ctx)
  236. }
  237. }