guard_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package dao
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. )
  6. func TestIncrExp(t *testing.T) {
  7. Convey("test IncrExp", t, func() {
  8. re := new(Guard)
  9. re.incrExp()
  10. So(re.expPerMin, ShouldResemble, int64(2))
  11. })
  12. }
  13. func TestDecrExp(t *testing.T) {
  14. Convey("test DecrExp", t, func() {
  15. re := new(Guard)
  16. re.incrExp()
  17. re.decrExp()
  18. So(re.expPerMin, ShouldResemble, int64(0))
  19. })
  20. }
  21. func TestSetExp(t *testing.T) {
  22. Convey("test SetExp", t, func() {
  23. re := new(Guard)
  24. re.setExp(10)
  25. So(re.expPerMin, ShouldResemble, int64(20))
  26. So(re.expThreshold, ShouldResemble, int64(17))
  27. })
  28. }
  29. func TestUpdateFac(t *testing.T) {
  30. Convey("test UpdateFac", t, func() {
  31. re := new(Guard)
  32. re.incrFac()
  33. re.updateFac()
  34. So(re.facLastMin, ShouldResemble, int64(1))
  35. })
  36. }
  37. func TestIncrFac(t *testing.T) {
  38. Convey("test IncrFac", t, func() {
  39. re := new(Guard)
  40. re.incrFac()
  41. So(re.facInMin, ShouldResemble, int64(1))
  42. })
  43. }
  44. func TestIsProtected(t *testing.T) {
  45. Convey("test IncrFac", t, func() {
  46. re := new(Guard)
  47. re.incrExp()
  48. re.incrExp()
  49. re.incrFac()
  50. re.updateFac()
  51. So(re.ok(), ShouldBeTrue)
  52. re = new(Guard)
  53. re.incrExp()
  54. re.incrFac()
  55. re.updateFac()
  56. So(re.ok(), ShouldBeFalse)
  57. })
  58. }