plugin_spec_internal_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package xfer
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/weaveworks/scope/test/reflect"
  6. )
  7. var benchmarkResult PluginSpecs
  8. func TestMakePluginSpecs(t *testing.T) {
  9. for _, testcase := range []struct {
  10. inputs []string
  11. wants []string
  12. }{
  13. {inputs: nil, wants: nil},
  14. {
  15. inputs: []string{"a"},
  16. wants: []string{"a"},
  17. },
  18. {
  19. inputs: []string{"a", "a"},
  20. wants: []string{"a"},
  21. },
  22. {
  23. inputs: []string{"b", "c", "a"},
  24. wants: []string{"a", "b", "c"},
  25. },
  26. } {
  27. var inputs []PluginSpec
  28. for _, id := range testcase.inputs {
  29. inputs = append(inputs, PluginSpec{ID: id})
  30. }
  31. have := MakePluginSpecs(inputs...)
  32. var haveIDs []string
  33. have.ForEach(func(p PluginSpec) {
  34. haveIDs = append(haveIDs, p.ID)
  35. })
  36. if !reflect.DeepEqual(testcase.wants, haveIDs) {
  37. t.Errorf("%#v: want %#v, have %#v", inputs, testcase.wants, haveIDs)
  38. }
  39. }
  40. }
  41. func BenchmarkMakePluginSpecs(b *testing.B) {
  42. plugins := []PluginSpec{}
  43. for i := 1000; i >= 0; i-- {
  44. plugins = append(plugins, PluginSpec{ID: fmt.Sprint(i)})
  45. }
  46. b.ResetTimer()
  47. for i := 0; i < b.N; i++ {
  48. benchmarkResult = MakePluginSpecs(plugins...)
  49. }
  50. }
  51. func TestPluginSpecsAdd(t *testing.T) {
  52. for _, testcase := range []struct {
  53. input PluginSpecs
  54. plugins []PluginSpec
  55. want PluginSpecs
  56. }{
  57. {
  58. input: PluginSpecs{},
  59. plugins: []PluginSpec{},
  60. want: PluginSpecs{},
  61. },
  62. {
  63. input: EmptyPluginSpecs,
  64. plugins: []PluginSpec{},
  65. want: EmptyPluginSpecs,
  66. },
  67. {
  68. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  69. plugins: []PluginSpec{},
  70. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  71. },
  72. {
  73. input: EmptyPluginSpecs,
  74. plugins: []PluginSpec{{ID: "a"}},
  75. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  76. },
  77. {
  78. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  79. plugins: []PluginSpec{{ID: "a"}},
  80. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  81. },
  82. {
  83. input: MakePluginSpecs(PluginSpec{ID: "b"}),
  84. plugins: []PluginSpec{
  85. {ID: "a"},
  86. {ID: "b"},
  87. },
  88. want: MakePluginSpecs(
  89. PluginSpec{ID: "a"},
  90. PluginSpec{ID: "b"},
  91. ),
  92. },
  93. {
  94. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  95. plugins: []PluginSpec{
  96. {ID: "c"},
  97. {ID: "b"},
  98. },
  99. want: MakePluginSpecs(
  100. PluginSpec{ID: "a"},
  101. PluginSpec{ID: "b"},
  102. PluginSpec{ID: "c"},
  103. ),
  104. },
  105. {
  106. input: MakePluginSpecs(
  107. PluginSpec{ID: "a"},
  108. PluginSpec{ID: "c"},
  109. ),
  110. plugins: []PluginSpec{
  111. {ID: "b"},
  112. {ID: "b"},
  113. {ID: "b"},
  114. },
  115. want: MakePluginSpecs(
  116. PluginSpec{ID: "a"},
  117. PluginSpec{ID: "b"},
  118. PluginSpec{ID: "c"},
  119. ),
  120. },
  121. } {
  122. originalLen := testcase.input.Size()
  123. if want, have := testcase.want, testcase.input.Add(testcase.plugins...); !reflect.DeepEqual(want, have) {
  124. t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.plugins, want, have)
  125. }
  126. if testcase.input.Size() != originalLen {
  127. t.Errorf("%v + %v: modified the original input!", testcase.input, testcase.plugins)
  128. }
  129. }
  130. }
  131. func BenchmarkPluginSpecsAdd(b *testing.B) {
  132. n := EmptyPluginSpecs
  133. for i := 0; i < 600; i++ {
  134. n = n.Add(PluginSpec{ID: fmt.Sprint(i)})
  135. }
  136. plugin := PluginSpec{ID: "401.5"}
  137. b.ResetTimer()
  138. for i := 0; i < b.N; i++ {
  139. benchmarkResult = n.Add(plugin)
  140. }
  141. }
  142. func TestPluginSpecsMerge(t *testing.T) {
  143. for _, testcase := range []struct {
  144. input PluginSpecs
  145. other PluginSpecs
  146. want PluginSpecs
  147. }{
  148. {input: PluginSpecs{}, other: PluginSpecs{}, want: PluginSpecs{}},
  149. {input: EmptyPluginSpecs, other: EmptyPluginSpecs, want: EmptyPluginSpecs},
  150. {
  151. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  152. other: EmptyPluginSpecs,
  153. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  154. },
  155. {
  156. input: EmptyPluginSpecs,
  157. other: MakePluginSpecs(PluginSpec{ID: "a"}),
  158. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  159. },
  160. {
  161. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  162. other: MakePluginSpecs(PluginSpec{ID: "b"}),
  163. want: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "b"}),
  164. },
  165. {
  166. input: MakePluginSpecs(PluginSpec{ID: "b"}),
  167. other: MakePluginSpecs(PluginSpec{ID: "a"}),
  168. want: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "b"}),
  169. },
  170. {
  171. input: MakePluginSpecs(PluginSpec{ID: "a"}),
  172. other: MakePluginSpecs(PluginSpec{ID: "a"}),
  173. want: MakePluginSpecs(PluginSpec{ID: "a"}),
  174. },
  175. {
  176. input: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "c"}),
  177. other: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "b"}),
  178. want: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "b"}, PluginSpec{ID: "c"}),
  179. },
  180. {
  181. input: MakePluginSpecs(PluginSpec{ID: "b"}),
  182. other: MakePluginSpecs(PluginSpec{ID: "a"}),
  183. want: MakePluginSpecs(PluginSpec{ID: "a"}, PluginSpec{ID: "b"}),
  184. },
  185. } {
  186. originalLen := testcase.input.Size()
  187. if want, have := testcase.want, testcase.input.Merge(testcase.other); !reflect.DeepEqual(want, have) {
  188. t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.other, want, have)
  189. }
  190. if testcase.input.Size() != originalLen {
  191. t.Errorf("%v + %v: modified the original input!", testcase.input, testcase.other)
  192. }
  193. }
  194. }
  195. func BenchmarkPluginSpecsMerge(b *testing.B) {
  196. n, other := PluginSpecs{}, PluginSpecs{}
  197. for i := 0; i < 600; i++ {
  198. n = n.Add(PluginSpec{ID: fmt.Sprint(i)})
  199. }
  200. for i := 400; i < 1000; i++ {
  201. other = other.Add(PluginSpec{ID: fmt.Sprint(i)})
  202. }
  203. b.ResetTimer()
  204. for i := 0; i < b.N; i++ {
  205. benchmarkResult = n.Merge(other)
  206. }
  207. }