weight_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package router_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/xtls/xray-core/app/router"
  6. )
  7. func TestWeight(t *testing.T) {
  8. manager := router.NewWeightManager(
  9. []*router.StrategyWeight{
  10. {
  11. Match: "x5",
  12. Value: 100,
  13. },
  14. {
  15. Match: "x8",
  16. },
  17. {
  18. Regexp: true,
  19. Match: `\bx0+(\.\d+)?\b`,
  20. Value: 1,
  21. },
  22. {
  23. Regexp: true,
  24. Match: `\bx\d+(\.\d+)?\b`,
  25. },
  26. },
  27. 1, func(v, w float64) float64 {
  28. return v * w
  29. },
  30. )
  31. tags := []string{
  32. "node name, x5, and more",
  33. "node name, x8",
  34. "node name, x15",
  35. "node name, x0100, and more",
  36. "node name, x10.1",
  37. "node name, x00.1, and more",
  38. }
  39. // test weight
  40. expected := []float64{100, 8, 15, 100, 10.1, 1}
  41. actual := make([]float64, 0)
  42. for _, tag := range tags {
  43. actual = append(actual, manager.Get(tag))
  44. }
  45. if !reflect.DeepEqual(expected, actual) {
  46. t.Errorf("expected: %v, actual: %v", expected, actual)
  47. }
  48. // test scale
  49. expected2 := []float64{1000, 80, 150, 1000, 101, 10}
  50. actual2 := make([]float64, 0)
  51. for _, tag := range tags {
  52. actual2 = append(actual2, manager.Apply(tag, 10))
  53. }
  54. if !reflect.DeepEqual(expected2, actual2) {
  55. t.Errorf("expected2: %v, actual2: %v", expected2, actual2)
  56. }
  57. }