expvar_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package expvar
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "net/http/httptest"
  9. "strconv"
  10. "testing"
  11. )
  12. // RemoveAll removes all exported variables.
  13. // This is for tests only.
  14. func RemoveAll() {
  15. mutex.Lock()
  16. defer mutex.Unlock()
  17. vars = make(map[string]Var)
  18. varKeys = nil
  19. }
  20. func TestInt(t *testing.T) {
  21. RemoveAll()
  22. reqs := NewInt("requests")
  23. if reqs.i != 0 {
  24. t.Errorf("reqs.i = %v, want 0", reqs.i)
  25. }
  26. if reqs != Get("requests").(*Int) {
  27. t.Errorf("Get() failed.")
  28. }
  29. reqs.Add(1)
  30. reqs.Add(3)
  31. if reqs.i != 4 {
  32. t.Errorf("reqs.i = %v, want 4", reqs.i)
  33. }
  34. if s := reqs.String(); s != "4" {
  35. t.Errorf("reqs.String() = %q, want \"4\"", s)
  36. }
  37. reqs.Set(-2)
  38. if reqs.i != -2 {
  39. t.Errorf("reqs.i = %v, want -2", reqs.i)
  40. }
  41. }
  42. func TestFloat(t *testing.T) {
  43. RemoveAll()
  44. reqs := NewFloat("requests-float")
  45. if reqs.f != 0.0 {
  46. t.Errorf("reqs.f = %v, want 0", reqs.f)
  47. }
  48. if reqs != Get("requests-float").(*Float) {
  49. t.Errorf("Get() failed.")
  50. }
  51. reqs.Add(1.5)
  52. reqs.Add(1.25)
  53. if reqs.f != 2.75 {
  54. t.Errorf("reqs.f = %v, want 2.75", reqs.f)
  55. }
  56. if s := reqs.String(); s != "2.75" {
  57. t.Errorf("reqs.String() = %q, want \"4.64\"", s)
  58. }
  59. reqs.Add(-2)
  60. if reqs.f != 0.75 {
  61. t.Errorf("reqs.f = %v, want 0.75", reqs.f)
  62. }
  63. }
  64. func TestString(t *testing.T) {
  65. RemoveAll()
  66. name := NewString("my-name")
  67. if name.s != "" {
  68. t.Errorf("name.s = %q, want \"\"", name.s)
  69. }
  70. name.Set("Mike")
  71. if name.s != "Mike" {
  72. t.Errorf("name.s = %q, want \"Mike\"", name.s)
  73. }
  74. if s := name.String(); s != "\"Mike\"" {
  75. t.Errorf("reqs.String() = %q, want \"\"Mike\"\"", s)
  76. }
  77. }
  78. func TestMapCounter(t *testing.T) {
  79. RemoveAll()
  80. colors := NewMap("bike-shed-colors")
  81. colors.Add("red", 1)
  82. colors.Add("red", 2)
  83. colors.Add("blue", 4)
  84. colors.AddFloat(`green "midori"`, 4.125)
  85. if x := colors.m["red"].(*Int).i; x != 3 {
  86. t.Errorf("colors.m[\"red\"] = %v, want 3", x)
  87. }
  88. if x := colors.m["blue"].(*Int).i; x != 4 {
  89. t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
  90. }
  91. if x := colors.m[`green "midori"`].(*Float).f; x != 4.125 {
  92. t.Errorf("colors.m[`green \"midori\"] = %v, want 3.14", x)
  93. }
  94. // colors.String() should be '{"red":3, "blue":4}',
  95. // though the order of red and blue could vary.
  96. s := colors.String()
  97. var j interface{}
  98. err := json.Unmarshal([]byte(s), &j)
  99. if err != nil {
  100. t.Errorf("colors.String() isn't valid JSON: %v", err)
  101. }
  102. m, ok := j.(map[string]interface{})
  103. if !ok {
  104. t.Error("colors.String() didn't produce a map.")
  105. }
  106. red := m["red"]
  107. x, ok := red.(float64)
  108. if !ok {
  109. t.Error("red.Kind() is not a number.")
  110. }
  111. if x != 3 {
  112. t.Errorf("red = %v, want 3", x)
  113. }
  114. }
  115. func TestFunc(t *testing.T) {
  116. RemoveAll()
  117. var x interface{} = []string{"a", "b"}
  118. f := Func(func() interface{} { return x })
  119. if s, exp := f.String(), `["a","b"]`; s != exp {
  120. t.Errorf(`f.String() = %q, want %q`, s, exp)
  121. }
  122. x = 17
  123. if s, exp := f.String(), `17`; s != exp {
  124. t.Errorf(`f.String() = %q, want %q`, s, exp)
  125. }
  126. }
  127. func TestHandler(t *testing.T) {
  128. RemoveAll()
  129. m := NewMap("map1")
  130. m.Add("a", 1)
  131. m.Add("z", 2)
  132. m2 := NewMap("map2")
  133. for i := 0; i < 9; i++ {
  134. m2.Add(strconv.Itoa(i), int64(i))
  135. }
  136. rr := httptest.NewRecorder()
  137. rr.Body = new(bytes.Buffer)
  138. expvarHandler(rr, nil)
  139. want := `{
  140. "map1": {"a": 1, "z": 2},
  141. "map2": {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8}
  142. }
  143. `
  144. if got := rr.Body.String(); got != want {
  145. t.Errorf("HTTP handler wrote:\n%s\nWant:\n%s", got, want)
  146. }
  147. }