limit_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package util
  2. import (
  3. "bytes"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. "time"
  7. )
  8. func TestFixedLimiter_AllowValueReset(t *testing.T) {
  9. l := NewFixedLimiter(10)
  10. require.True(t, l.AllowN(5))
  11. require.Equal(t, int64(5), l.Value())
  12. require.True(t, l.AllowN(5))
  13. require.Equal(t, int64(10), l.Value())
  14. require.False(t, l.Allow())
  15. require.Equal(t, int64(10), l.Value())
  16. l.Reset()
  17. require.Equal(t, int64(0), l.Value())
  18. require.True(t, l.Allow())
  19. require.True(t, l.AllowN(9))
  20. require.False(t, l.Allow())
  21. }
  22. func TestFixedLimiter_AddSub(t *testing.T) {
  23. l := NewFixedLimiter(10)
  24. l.AllowN(5)
  25. if l.value != 5 {
  26. t.Fatalf("expected value to be %d, got %d", 5, l.value)
  27. }
  28. l.AllowN(-2)
  29. if l.value != 3 {
  30. t.Fatalf("expected value to be %d, got %d", 7, l.value)
  31. }
  32. }
  33. func TestBytesLimiter_Add_Simple(t *testing.T) {
  34. l := NewBytesLimiter(250*1024*1024, 24*time.Hour) // 250 MB per 24h
  35. require.True(t, l.AllowN(100*1024*1024))
  36. require.Equal(t, int64(100*1024*1024), l.Value())
  37. require.True(t, l.AllowN(100*1024*1024))
  38. require.Equal(t, int64(200*1024*1024), l.Value())
  39. require.False(t, l.AllowN(300*1024*1024))
  40. require.Equal(t, int64(200*1024*1024), l.Value())
  41. }
  42. func TestBytesLimiter_Add_Wait(t *testing.T) {
  43. l := NewBytesLimiter(250*1024*1024, 24*time.Hour) // 250 MB per 24h (~ 303 bytes per 100ms)
  44. require.True(t, l.AllowN(250*1024*1024))
  45. require.False(t, l.AllowN(400))
  46. time.Sleep(200 * time.Millisecond)
  47. require.True(t, l.AllowN(400))
  48. }
  49. func TestLimitWriter_WriteNoLimiter(t *testing.T) {
  50. var buf bytes.Buffer
  51. lw := NewLimitWriter(&buf)
  52. if _, err := lw.Write(make([]byte, 10)); err != nil {
  53. t.Fatal(err)
  54. }
  55. if _, err := lw.Write(make([]byte, 1)); err != nil {
  56. t.Fatal(err)
  57. }
  58. if buf.Len() != 11 {
  59. t.Fatalf("expected buffer length to be %d, got %d", 11, buf.Len())
  60. }
  61. }
  62. func TestLimitWriter_WriteOneLimiter(t *testing.T) {
  63. var buf bytes.Buffer
  64. l := NewFixedLimiter(10)
  65. lw := NewLimitWriter(&buf, l)
  66. if _, err := lw.Write(make([]byte, 10)); err != nil {
  67. t.Fatal(err)
  68. }
  69. if _, err := lw.Write(make([]byte, 1)); err != ErrLimitReached {
  70. t.Fatalf("expected ErrLimitReached, got %#v", err)
  71. }
  72. if buf.Len() != 10 {
  73. t.Fatalf("expected buffer length to be %d, got %d", 10, buf.Len())
  74. }
  75. if l.value != 10 {
  76. t.Fatalf("expected limiter value to be %d, got %d", 10, l.value)
  77. }
  78. }
  79. func TestLimitWriter_WriteTwoLimiters(t *testing.T) {
  80. var buf bytes.Buffer
  81. l1 := NewFixedLimiter(11)
  82. l2 := NewFixedLimiter(9)
  83. lw := NewLimitWriter(&buf, l1, l2)
  84. if _, err := lw.Write(make([]byte, 8)); err != nil {
  85. t.Fatal(err)
  86. }
  87. if _, err := lw.Write(make([]byte, 2)); err != ErrLimitReached {
  88. t.Fatalf("expected ErrLimitReached, got %#v", err)
  89. }
  90. if buf.Len() != 8 {
  91. t.Fatalf("expected buffer length to be %d, got %d", 8, buf.Len())
  92. }
  93. if l1.value != 8 {
  94. t.Fatalf("expected limiter 1 value to be %d, got %d", 8, l1.value)
  95. }
  96. if l2.value != 8 {
  97. t.Fatalf("expected limiter 2 value to be %d, got %d", 8, l2.value)
  98. }
  99. }
  100. func TestLimitWriter_WriteTwoDifferentLimiters(t *testing.T) {
  101. var buf bytes.Buffer
  102. l1 := NewFixedLimiter(32)
  103. l2 := NewBytesLimiter(8, 200*time.Millisecond)
  104. lw := NewLimitWriter(&buf, l1, l2)
  105. _, err := lw.Write(make([]byte, 8))
  106. require.Nil(t, err)
  107. _, err = lw.Write(make([]byte, 4))
  108. require.Equal(t, ErrLimitReached, err)
  109. }
  110. func TestLimitWriter_WriteTwoDifferentLimiters_Wait(t *testing.T) {
  111. var buf bytes.Buffer
  112. l1 := NewFixedLimiter(32)
  113. l2 := NewBytesLimiter(8, 200*time.Millisecond)
  114. lw := NewLimitWriter(&buf, l1, l2)
  115. _, err := lw.Write(make([]byte, 8))
  116. require.Nil(t, err)
  117. time.Sleep(250 * time.Millisecond)
  118. _, err = lw.Write(make([]byte, 8))
  119. require.Nil(t, err)
  120. _, err = lw.Write(make([]byte, 4))
  121. require.Equal(t, ErrLimitReached, err)
  122. }
  123. func TestLimitWriter_WriteTwoDifferentLimiters_Wait_FixedLimiterFail(t *testing.T) {
  124. var buf bytes.Buffer
  125. l1 := NewFixedLimiter(11) // <<< This fails below
  126. l2 := NewBytesLimiter(8, 200*time.Millisecond)
  127. lw := NewLimitWriter(&buf, l1, l2)
  128. _, err := lw.Write(make([]byte, 8))
  129. require.Nil(t, err)
  130. time.Sleep(250 * time.Millisecond)
  131. _, err = lw.Write(make([]byte, 8)) // <<< FixedLimiter fails
  132. require.Equal(t, ErrLimitReached, err)
  133. }