You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

77 lines
1.6 KiB

  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package edsbalancer
  17. import (
  18. "testing"
  19. )
  20. func TestDropper(t *testing.T) {
  21. const repeat = 2
  22. type args struct {
  23. numerator uint32
  24. denominator uint32
  25. }
  26. tests := []struct {
  27. name string
  28. args args
  29. }{
  30. {
  31. name: "2_3",
  32. args: args{
  33. numerator: 2,
  34. denominator: 3,
  35. },
  36. },
  37. {
  38. name: "4_8",
  39. args: args{
  40. numerator: 4,
  41. denominator: 8,
  42. },
  43. },
  44. {
  45. name: "7_20",
  46. args: args{
  47. numerator: 7,
  48. denominator: 20,
  49. },
  50. },
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. d := newDropper(tt.args.numerator, tt.args.denominator)
  55. var (
  56. dCount int
  57. wantCount = int(tt.args.numerator) * repeat
  58. loopCount = int(tt.args.denominator) * repeat
  59. )
  60. for i := 0; i < loopCount; i++ {
  61. if d.drop() {
  62. dCount++
  63. }
  64. }
  65. if dCount != (wantCount) {
  66. t.Errorf("with numerator %v, denominator %v repeat %v, got drop count: %v, want %v",
  67. tt.args.numerator, tt.args.denominator, repeat, dCount, wantCount)
  68. }
  69. })
  70. }
  71. }