strategy_leastload_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package router
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestSelectLeastLoad(t *testing.T) {
  7. settings := &StrategyLeastLoadConfig{
  8. HealthCheck: &HealthPingConfig{
  9. SamplingCount: 10,
  10. },
  11. Expected: 1,
  12. MaxRTT: int64(time.Millisecond * time.Duration(800)),
  13. }
  14. strategy := NewLeastLoadStrategy(settings, nil)
  15. // std 40
  16. strategy.PutResult("a", time.Millisecond*time.Duration(60))
  17. strategy.PutResult("a", time.Millisecond*time.Duration(140))
  18. strategy.PutResult("a", time.Millisecond*time.Duration(60))
  19. strategy.PutResult("a", time.Millisecond*time.Duration(140))
  20. // std 60
  21. strategy.PutResult("b", time.Millisecond*time.Duration(40))
  22. strategy.PutResult("b", time.Millisecond*time.Duration(160))
  23. strategy.PutResult("b", time.Millisecond*time.Duration(40))
  24. strategy.PutResult("b", time.Millisecond*time.Duration(160))
  25. // std 0, but >MaxRTT
  26. strategy.PutResult("c", time.Millisecond*time.Duration(1000))
  27. strategy.PutResult("c", time.Millisecond*time.Duration(1000))
  28. strategy.PutResult("c", time.Millisecond*time.Duration(1000))
  29. strategy.PutResult("c", time.Millisecond*time.Duration(1000))
  30. expected := "a"
  31. actual := strategy.SelectAndPick([]string{"a", "b", "c", "untested"})
  32. if actual != expected {
  33. t.Errorf("expected: %v, actual: %v", expected, actual)
  34. }
  35. }
  36. func TestSelectLeastLoadWithCost(t *testing.T) {
  37. settings := &StrategyLeastLoadConfig{
  38. HealthCheck: &HealthPingConfig{
  39. SamplingCount: 10,
  40. },
  41. Costs: []*StrategyWeight{
  42. {Match: "a", Value: 9},
  43. },
  44. Expected: 1,
  45. }
  46. strategy := NewLeastLoadStrategy(settings, nil)
  47. // std 40, std+c 120
  48. strategy.PutResult("a", time.Millisecond*time.Duration(60))
  49. strategy.PutResult("a", time.Millisecond*time.Duration(140))
  50. strategy.PutResult("a", time.Millisecond*time.Duration(60))
  51. strategy.PutResult("a", time.Millisecond*time.Duration(140))
  52. // std 60
  53. strategy.PutResult("b", time.Millisecond*time.Duration(40))
  54. strategy.PutResult("b", time.Millisecond*time.Duration(160))
  55. strategy.PutResult("b", time.Millisecond*time.Duration(40))
  56. strategy.PutResult("b", time.Millisecond*time.Duration(160))
  57. expected := "b"
  58. actual := strategy.SelectAndPick([]string{"a", "b", "untested"})
  59. if actual != expected {
  60. t.Errorf("expected: %v, actual: %v", expected, actual)
  61. }
  62. }
  63. func TestSelectLeastExpected(t *testing.T) {
  64. strategy := &LeastLoadStrategy{
  65. settings: &StrategyLeastLoadConfig{
  66. Baselines: nil,
  67. Expected: 3,
  68. },
  69. }
  70. nodes := []*node{
  71. {Tag: "a", applied: 100},
  72. {Tag: "b", applied: 200},
  73. {Tag: "c", applied: 300},
  74. {Tag: "d", applied: 350},
  75. }
  76. expected := 3
  77. ns := strategy.selectLeastLoad(nodes)
  78. if len(ns) != expected {
  79. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  80. }
  81. }
  82. func TestSelectLeastExpected2(t *testing.T) {
  83. strategy := &LeastLoadStrategy{
  84. settings: &StrategyLeastLoadConfig{
  85. Baselines: nil,
  86. Expected: 3,
  87. },
  88. }
  89. nodes := []*node{
  90. {Tag: "a", applied: 100},
  91. {Tag: "b", applied: 200},
  92. }
  93. expected := 2
  94. ns := strategy.selectLeastLoad(nodes)
  95. if len(ns) != expected {
  96. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  97. }
  98. }
  99. func TestSelectLeastExpectedAndBaselines(t *testing.T) {
  100. strategy := &LeastLoadStrategy{
  101. settings: &StrategyLeastLoadConfig{
  102. Baselines: []int64{200, 300, 400},
  103. Expected: 3,
  104. },
  105. }
  106. nodes := []*node{
  107. {Tag: "a", applied: 100},
  108. {Tag: "b", applied: 200},
  109. {Tag: "c", applied: 250},
  110. {Tag: "d", applied: 300},
  111. {Tag: "e", applied: 310},
  112. }
  113. expected := 4
  114. ns := strategy.selectLeastLoad(nodes)
  115. if len(ns) != expected {
  116. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  117. }
  118. }
  119. func TestSelectLeastExpectedAndBaselines2(t *testing.T) {
  120. strategy := &LeastLoadStrategy{
  121. settings: &StrategyLeastLoadConfig{
  122. Baselines: []int64{200, 300, 400},
  123. Expected: 3,
  124. },
  125. }
  126. nodes := []*node{
  127. {Tag: "a", applied: 500},
  128. {Tag: "b", applied: 600},
  129. {Tag: "c", applied: 700},
  130. {Tag: "d", applied: 800},
  131. {Tag: "e", applied: 900},
  132. }
  133. expected := 3
  134. ns := strategy.selectLeastLoad(nodes)
  135. if len(ns) != expected {
  136. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  137. }
  138. }
  139. func TestSelectLeastLoadBaselines(t *testing.T) {
  140. strategy := &LeastLoadStrategy{
  141. settings: &StrategyLeastLoadConfig{
  142. Baselines: []int64{200, 400, 600},
  143. Expected: 0,
  144. },
  145. }
  146. nodes := []*node{
  147. {Tag: "a", applied: 100},
  148. {Tag: "b", applied: 200},
  149. {Tag: "c", applied: 300},
  150. }
  151. expected := 2
  152. ns := strategy.selectLeastLoad(nodes)
  153. if len(ns) != expected {
  154. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  155. }
  156. }
  157. func TestSelectLeastLoadBaselinesNoQualified(t *testing.T) {
  158. strategy := &LeastLoadStrategy{
  159. settings: &StrategyLeastLoadConfig{
  160. Baselines: []int64{200, 400, 600},
  161. Expected: 0,
  162. },
  163. }
  164. nodes := []*node{
  165. {Tag: "a", applied: 800},
  166. {Tag: "b", applied: 1000},
  167. }
  168. expected := 0
  169. ns := strategy.selectLeastLoad(nodes)
  170. if len(ns) != expected {
  171. t.Errorf("expected: %v, actual: %v", expected, len(ns))
  172. }
  173. }