limit_test.go 3.8 KB

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