You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
3.1 KiB

  1. // Copyright 2019, 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. package metricproducer
  15. import (
  16. "testing"
  17. "go.opencensus.io/metric/metricdata"
  18. )
  19. type testProducer struct {
  20. name string
  21. }
  22. var (
  23. myProd1 = newTestProducer("foo")
  24. myProd2 = newTestProducer("bar")
  25. myProd3 = newTestProducer("foobar")
  26. pm = GlobalManager()
  27. )
  28. func newTestProducer(name string) *testProducer {
  29. return &testProducer{name}
  30. }
  31. func (mp *testProducer) Read() []*metricdata.Metric {
  32. return nil
  33. }
  34. func TestAdd(t *testing.T) {
  35. pm.AddProducer(myProd1)
  36. pm.AddProducer(myProd2)
  37. got := pm.GetAll()
  38. want := []*testProducer{myProd1, myProd2}
  39. checkSlice(got, want, t)
  40. deleteAll()
  41. }
  42. func TestAddExisting(t *testing.T) {
  43. pm.AddProducer(myProd1)
  44. pm.AddProducer(myProd2)
  45. pm.AddProducer(myProd1)
  46. got := pm.GetAll()
  47. want := []*testProducer{myProd2, myProd1}
  48. checkSlice(got, want, t)
  49. deleteAll()
  50. }
  51. func TestAddNil(t *testing.T) {
  52. pm.AddProducer(nil)
  53. got := pm.GetAll()
  54. want := []*testProducer{}
  55. checkSlice(got, want, t)
  56. deleteAll()
  57. }
  58. func TestDelete(t *testing.T) {
  59. pm.AddProducer(myProd1)
  60. pm.AddProducer(myProd2)
  61. pm.AddProducer(myProd3)
  62. pm.DeleteProducer(myProd2)
  63. got := pm.GetAll()
  64. want := []*testProducer{myProd1, myProd3}
  65. checkSlice(got, want, t)
  66. deleteAll()
  67. }
  68. func TestDeleteNonExisting(t *testing.T) {
  69. pm.AddProducer(myProd1)
  70. pm.AddProducer(myProd3)
  71. pm.DeleteProducer(myProd2)
  72. got := pm.GetAll()
  73. want := []*testProducer{myProd1, myProd3}
  74. checkSlice(got, want, t)
  75. deleteAll()
  76. }
  77. func TestDeleteNil(t *testing.T) {
  78. pm.AddProducer(myProd1)
  79. pm.AddProducer(myProd3)
  80. pm.DeleteProducer(nil)
  81. got := pm.GetAll()
  82. want := []*testProducer{myProd1, myProd3}
  83. checkSlice(got, want, t)
  84. deleteAll()
  85. }
  86. func TestGetAllNil(t *testing.T) {
  87. got := pm.GetAll()
  88. want := []*testProducer{}
  89. checkSlice(got, want, t)
  90. deleteAll()
  91. }
  92. func TestImmutableProducerList(t *testing.T) {
  93. pm.AddProducer(myProd1)
  94. pm.AddProducer(myProd2)
  95. producersToMutate := pm.GetAll()
  96. producersToMutate[0] = myProd3
  97. got := pm.GetAll()
  98. want := []*testProducer{myProd1, myProd2}
  99. checkSlice(got, want, t)
  100. deleteAll()
  101. }
  102. func checkSlice(got []Producer, want []*testProducer, t *testing.T) {
  103. gotLen := len(got)
  104. wantLen := len(want)
  105. if gotLen != wantLen {
  106. t.Errorf("got len: %d want: %d\n", gotLen, wantLen)
  107. } else {
  108. gotMap := map[Producer]struct{}{}
  109. for i := 0; i < gotLen; i++ {
  110. gotMap[got[i]] = struct{}{}
  111. }
  112. for i := 0; i < wantLen; i++ {
  113. delete(gotMap, want[i])
  114. }
  115. if len(gotMap) > 0 {
  116. t.Errorf("got %v, want %v\n", got, want)
  117. }
  118. }
  119. }
  120. func deleteAll() {
  121. pm.DeleteProducer(myProd1)
  122. pm.DeleteProducer(myProd2)
  123. pm.DeleteProducer(myProd3)
  124. }