balancer_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019 Yunion
  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 balancer
  15. import (
  16. "reflect"
  17. "testing"
  18. "yunion.io/x/jsonutils"
  19. )
  20. type floatC float64
  21. func (f floatC) GetId() string {
  22. return ""
  23. }
  24. func (f floatC) GetName() string {
  25. return ""
  26. }
  27. func (f floatC) GetObject() jsonutils.JSONObject {
  28. return nil
  29. }
  30. func (f floatC) GetHostName() string {
  31. return ""
  32. }
  33. func (f floatC) GetScore() float64 {
  34. return float64(f)
  35. }
  36. func newFCs(n ...float64) []ICandidate {
  37. ret := make([]ICandidate, len(n))
  38. for i := range n {
  39. ret[i] = floatC(n[i])
  40. }
  41. return ret
  42. }
  43. func Test_findFitCandidates(t *testing.T) {
  44. type args struct {
  45. input []ICandidate
  46. delta float64
  47. }
  48. tests := []struct {
  49. name string
  50. args args
  51. want []ICandidate
  52. wantErr bool
  53. }{
  54. {
  55. name: "{}",
  56. args: args{
  57. input: newFCs(),
  58. delta: 3.0,
  59. },
  60. want: nil,
  61. wantErr: true,
  62. },
  63. {
  64. name: "{1, 2, 3}, 3",
  65. args: args{
  66. input: newFCs(1, 2, 3),
  67. delta: 3.0,
  68. },
  69. want: newFCs(1, 2),
  70. wantErr: false,
  71. },
  72. {
  73. name: "{1, 2, 3}, 0.5",
  74. args: args{
  75. input: newFCs(1, 2, 3),
  76. delta: 0.5,
  77. },
  78. want: newFCs(1),
  79. wantErr: false,
  80. },
  81. {
  82. name: "{1, 2, 3}, 4",
  83. args: args{
  84. input: newFCs(1, 2, 3),
  85. delta: 4,
  86. },
  87. want: newFCs(1, 2, 3),
  88. wantErr: false,
  89. },
  90. }
  91. for _, tt := range tests {
  92. t.Run(tt.name, func(t *testing.T) {
  93. got, err := findFitCandidates(tt.args.input, tt.args.delta)
  94. if (err != nil) != tt.wantErr {
  95. t.Errorf("findN() got = %v, error = %v, wantErr %v, err = %v", got, err, tt.wantErr, err)
  96. return
  97. }
  98. if !reflect.DeepEqual(got, tt.want) {
  99. t.Errorf("findN() = %v, want %v", got, tt.want)
  100. }
  101. })
  102. }
  103. }