Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

223 Zeilen
4.9 KiB

  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package tag
  16. import (
  17. "context"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. )
  23. func TestContext(t *testing.T) {
  24. k1, _ := NewKey("k1")
  25. k2, _ := NewKey("k2")
  26. ctx := context.Background()
  27. ctx, _ = New(ctx,
  28. Insert(k1, "v1"),
  29. Insert(k2, "v2"),
  30. )
  31. got := FromContext(ctx)
  32. want := newMap()
  33. want.insert(k1, "v1")
  34. want.insert(k2, "v2")
  35. if !reflect.DeepEqual(got, want) {
  36. t.Errorf("Map = %#v; want %#v", got, want)
  37. }
  38. }
  39. func TestDo(t *testing.T) {
  40. k1, _ := NewKey("k1")
  41. k2, _ := NewKey("k2")
  42. ctx := context.Background()
  43. ctx, _ = New(ctx,
  44. Insert(k1, "v1"),
  45. Insert(k2, "v2"),
  46. )
  47. got := FromContext(ctx)
  48. want := newMap()
  49. want.insert(k1, "v1")
  50. want.insert(k2, "v2")
  51. Do(ctx, func(ctx context.Context) {
  52. got = FromContext(ctx)
  53. })
  54. if !reflect.DeepEqual(got, want) {
  55. t.Errorf("Map = %#v; want %#v", got, want)
  56. }
  57. }
  58. func TestNewMap(t *testing.T) {
  59. k1, _ := NewKey("k1")
  60. k2, _ := NewKey("k2")
  61. k3, _ := NewKey("k3")
  62. k4, _ := NewKey("k4")
  63. k5, _ := NewKey("k5")
  64. initial := makeTestTagMap(5)
  65. tests := []struct {
  66. name string
  67. initial *Map
  68. mods []Mutator
  69. want *Map
  70. }{
  71. {
  72. name: "from empty; insert",
  73. initial: nil,
  74. mods: []Mutator{
  75. Insert(k5, "v5"),
  76. },
  77. want: makeTestTagMap(2, 4, 5),
  78. },
  79. {
  80. name: "from empty; insert existing",
  81. initial: nil,
  82. mods: []Mutator{
  83. Insert(k1, "v1"),
  84. },
  85. want: makeTestTagMap(1, 2, 4),
  86. },
  87. {
  88. name: "from empty; update",
  89. initial: nil,
  90. mods: []Mutator{
  91. Update(k1, "v1"),
  92. },
  93. want: makeTestTagMap(2, 4),
  94. },
  95. {
  96. name: "from empty; update unexisting",
  97. initial: nil,
  98. mods: []Mutator{
  99. Update(k5, "v5"),
  100. },
  101. want: makeTestTagMap(2, 4),
  102. },
  103. {
  104. name: "from existing; upsert",
  105. initial: initial,
  106. mods: []Mutator{
  107. Upsert(k5, "v5"),
  108. },
  109. want: makeTestTagMap(2, 4, 5),
  110. },
  111. {
  112. name: "from existing; delete",
  113. initial: initial,
  114. mods: []Mutator{
  115. Delete(k2),
  116. },
  117. want: makeTestTagMap(4, 5),
  118. },
  119. {
  120. name: "from empty; invalid",
  121. initial: nil,
  122. mods: []Mutator{
  123. Insert(k5, "v\x19"),
  124. Upsert(k5, "v\x19"),
  125. Update(k5, "v\x19"),
  126. },
  127. want: nil,
  128. },
  129. {
  130. name: "from empty; no partial",
  131. initial: nil,
  132. mods: []Mutator{
  133. Insert(k5, "v1"),
  134. Update(k5, "v\x19"),
  135. },
  136. want: nil,
  137. },
  138. }
  139. for _, tt := range tests {
  140. mods := []Mutator{
  141. Insert(k1, "v1"),
  142. Insert(k2, "v2"),
  143. Update(k3, "v3"),
  144. Upsert(k4, "v4"),
  145. Insert(k2, "v2"),
  146. Delete(k1),
  147. }
  148. mods = append(mods, tt.mods...)
  149. ctx := NewContext(context.Background(), tt.initial)
  150. ctx, err := New(ctx, mods...)
  151. if tt.want != nil && err != nil {
  152. t.Errorf("%v: New = %v", tt.name, err)
  153. }
  154. if got, want := FromContext(ctx), tt.want; !reflect.DeepEqual(got, want) {
  155. t.Errorf("%v: got %v; want %v", tt.name, got, want)
  156. }
  157. }
  158. }
  159. func TestNewValidation(t *testing.T) {
  160. tests := []struct {
  161. err string
  162. seed *Map
  163. }{
  164. // Key name validation in seed
  165. {err: "invalid key", seed: &Map{m: map[Key]string{{name: ""}: "foo"}}},
  166. {err: "", seed: &Map{m: map[Key]string{{name: "key"}: "foo"}}},
  167. {err: "", seed: &Map{m: map[Key]string{{name: strings.Repeat("a", 255)}: "census"}}},
  168. {err: "invalid key", seed: &Map{m: map[Key]string{{name: strings.Repeat("a", 256)}: "census"}}},
  169. {err: "invalid key", seed: &Map{m: map[Key]string{{name: "Приве́т"}: "census"}}},
  170. // Value validation
  171. {err: "", seed: &Map{m: map[Key]string{{name: "key"}: ""}}},
  172. {err: "", seed: &Map{m: map[Key]string{{name: "key"}: strings.Repeat("a", 255)}}},
  173. {err: "invalid value", seed: &Map{m: map[Key]string{{name: "key"}: "Приве́т"}}},
  174. {err: "invalid value", seed: &Map{m: map[Key]string{{name: "key"}: strings.Repeat("a", 256)}}},
  175. }
  176. for i, tt := range tests {
  177. ctx := NewContext(context.Background(), tt.seed)
  178. ctx, err := New(ctx)
  179. if tt.err != "" {
  180. if err == nil {
  181. t.Errorf("#%d: got nil error; want %q", i, tt.err)
  182. continue
  183. } else if s, substr := err.Error(), tt.err; !strings.Contains(s, substr) {
  184. t.Errorf("#%d:\ngot %q\nwant %q", i, s, substr)
  185. }
  186. continue
  187. }
  188. if err != nil {
  189. t.Errorf("#%d: got %q want nil", i, err)
  190. continue
  191. }
  192. m := FromContext(ctx)
  193. if m == nil {
  194. t.Errorf("#%d: got nil map", i)
  195. continue
  196. }
  197. }
  198. }
  199. func makeTestTagMap(ids ...int) *Map {
  200. m := newMap()
  201. for _, v := range ids {
  202. k, _ := NewKey(fmt.Sprintf("k%d", v))
  203. m.m[k] = fmt.Sprintf("v%d", v)
  204. }
  205. return m
  206. }