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.

57 lines
1.8 KiB

  1. package state
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. tmtime "github.com/tendermint/tendermint/libs/time"
  7. )
  8. func TestWeightedMedian(t *testing.T) {
  9. m := make([]*weightedTime, 3)
  10. t1 := tmtime.Now()
  11. t2 := t1.Add(5 * time.Second)
  12. t3 := t1.Add(10 * time.Second)
  13. m[2] = newWeightedTime(t1, 33) // faulty processes
  14. m[0] = newWeightedTime(t2, 40) // correct processes
  15. m[1] = newWeightedTime(t3, 27) // correct processes
  16. totalVotingPower := int64(100)
  17. median := weightedMedian(m, totalVotingPower)
  18. assert.Equal(t, t2, median)
  19. // median always returns value between values of correct processes
  20. assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
  21. (median.Before(t3) || median.Equal(t3)))
  22. m[1] = newWeightedTime(t1, 40) // correct processes
  23. m[2] = newWeightedTime(t2, 27) // correct processes
  24. m[0] = newWeightedTime(t3, 33) // faulty processes
  25. totalVotingPower = int64(100)
  26. median = weightedMedian(m, totalVotingPower)
  27. assert.Equal(t, t2, median)
  28. // median always returns value between values of correct processes
  29. assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
  30. (median.Before(t2) || median.Equal(t2)))
  31. m = make([]*weightedTime, 8)
  32. t4 := t1.Add(15 * time.Second)
  33. t5 := t1.Add(60 * time.Second)
  34. m[3] = newWeightedTime(t1, 10) // correct processes
  35. m[1] = newWeightedTime(t2, 10) // correct processes
  36. m[5] = newWeightedTime(t2, 10) // correct processes
  37. m[4] = newWeightedTime(t3, 23) // faulty processes
  38. m[0] = newWeightedTime(t4, 20) // correct processes
  39. m[7] = newWeightedTime(t5, 10) // faulty processes
  40. totalVotingPower = int64(83)
  41. median = weightedMedian(m, totalVotingPower)
  42. assert.Equal(t, t3, median)
  43. // median always returns value between values of correct processes
  44. assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
  45. (median.Before(t4) || median.Equal(t4)))
  46. }