util_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package queue
  2. import (
  3. "errors"
  4. "math"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/core/mathx"
  9. )
  10. var (
  11. proba = mathx.NewProba()
  12. failProba = 0.01
  13. )
  14. func init() {
  15. logx.Disable()
  16. }
  17. func TestGenerateName(t *testing.T) {
  18. pushers := []Pusher{
  19. &mockedPusher{name: "first"},
  20. &mockedPusher{name: "second"},
  21. &mockedPusher{name: "third"},
  22. }
  23. assert.Equal(t, "first,second,third", generateName(pushers))
  24. }
  25. func TestGenerateNameNil(t *testing.T) {
  26. var pushers []Pusher
  27. assert.Equal(t, "", generateName(pushers))
  28. }
  29. func calcMean(vals []int) float64 {
  30. if len(vals) == 0 {
  31. return 0
  32. }
  33. var result float64
  34. for _, val := range vals {
  35. result += float64(val)
  36. }
  37. return result / float64(len(vals))
  38. }
  39. func calcVariance(mean float64, vals []int) float64 {
  40. if len(vals) == 0 {
  41. return 0
  42. }
  43. var result float64
  44. for _, val := range vals {
  45. result += math.Pow(float64(val)-mean, 2)
  46. }
  47. return result / float64(len(vals))
  48. }
  49. type mockedPusher struct {
  50. name string
  51. count int
  52. }
  53. func (p *mockedPusher) Name() string {
  54. return p.name
  55. }
  56. func (p *mockedPusher) Push(s string) error {
  57. if proba.TrueOnProba(failProba) {
  58. return errors.New("dummy")
  59. }
  60. p.count++
  61. return nil
  62. }