metrics_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package origin
  2. import (
  3. "strconv"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // can only be called once
  9. var m = NewTunnelMetrics()
  10. func TestConcurrentRequestsSingleTunnel(t *testing.T) {
  11. routines := 20
  12. var wg sync.WaitGroup
  13. wg.Add(routines)
  14. for i := 0; i < routines; i++ {
  15. go func() {
  16. m.incrementRequests("0")
  17. wg.Done()
  18. }()
  19. }
  20. wg.Wait()
  21. assert.Len(t, m.concurrentRequests, 1)
  22. assert.Equal(t, uint64(routines), m.concurrentRequests["0"])
  23. assert.Len(t, m.maxConcurrentRequests, 1)
  24. assert.Equal(t, uint64(routines), m.maxConcurrentRequests["0"])
  25. wg.Add(routines / 2)
  26. for i := 0; i < routines/2; i++ {
  27. go func() {
  28. m.decrementConcurrentRequests("0")
  29. wg.Done()
  30. }()
  31. }
  32. wg.Wait()
  33. assert.Equal(t, uint64(routines-routines/2), m.concurrentRequests["0"])
  34. assert.Equal(t, uint64(routines), m.maxConcurrentRequests["0"])
  35. }
  36. func TestConcurrentRequestsMultiTunnel(t *testing.T) {
  37. m.concurrentRequests = make(map[string]uint64)
  38. m.maxConcurrentRequests = make(map[string]uint64)
  39. tunnels := 20
  40. var wg sync.WaitGroup
  41. wg.Add(tunnels)
  42. for i := 0; i < tunnels; i++ {
  43. go func(i int) {
  44. // if we have j < i, then tunnel 0 won't have a chance to call incrementRequests
  45. for j := 0; j < i+1; j++ {
  46. id := strconv.Itoa(i)
  47. m.incrementRequests(id)
  48. }
  49. wg.Done()
  50. }(i)
  51. }
  52. wg.Wait()
  53. assert.Len(t, m.concurrentRequests, tunnels)
  54. assert.Len(t, m.maxConcurrentRequests, tunnels)
  55. for i := 0; i < tunnels; i++ {
  56. id := strconv.Itoa(i)
  57. assert.Equal(t, uint64(i+1), m.concurrentRequests[id])
  58. assert.Equal(t, uint64(i+1), m.maxConcurrentRequests[id])
  59. }
  60. wg.Add(tunnels)
  61. for i := 0; i < tunnels; i++ {
  62. go func(i int) {
  63. for j := 0; j < i+1; j++ {
  64. id := strconv.Itoa(i)
  65. m.decrementConcurrentRequests(id)
  66. }
  67. wg.Done()
  68. }(i)
  69. }
  70. wg.Wait()
  71. assert.Len(t, m.concurrentRequests, tunnels)
  72. assert.Len(t, m.maxConcurrentRequests, tunnels)
  73. for i := 0; i < tunnels; i++ {
  74. id := strconv.Itoa(i)
  75. assert.Equal(t, uint64(0), m.concurrentRequests[id])
  76. assert.Equal(t, uint64(i+1), m.maxConcurrentRequests[id])
  77. }
  78. }
  79. func TestRegisterServerLocation(t *testing.T) {
  80. tunnels := 20
  81. var wg sync.WaitGroup
  82. wg.Add(tunnels)
  83. for i := 0; i < tunnels; i++ {
  84. go func(i int) {
  85. id := strconv.Itoa(i)
  86. m.registerServerLocation(id, "LHR")
  87. wg.Done()
  88. }(i)
  89. }
  90. wg.Wait()
  91. for i := 0; i < tunnels; i++ {
  92. id := strconv.Itoa(i)
  93. assert.Equal(t, "LHR", m.oldServerLocations[id])
  94. }
  95. wg.Add(tunnels)
  96. for i := 0; i < tunnels; i++ {
  97. go func(i int) {
  98. id := strconv.Itoa(i)
  99. m.registerServerLocation(id, "AUS")
  100. wg.Done()
  101. }(i)
  102. }
  103. wg.Wait()
  104. for i := 0; i < tunnels; i++ {
  105. id := strconv.Itoa(i)
  106. assert.Equal(t, "AUS", m.oldServerLocations[id])
  107. }
  108. }