strategy_leastload_test.go 5.1 KB

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