2021-12-15 22:12:40 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-01-02 23:56:12 +01:00
|
|
|
"bytes"
|
2022-01-12 23:03:28 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-15 22:12:40 +01:00
|
|
|
"testing"
|
2022-01-12 23:03:28 +01:00
|
|
|
"time"
|
2021-12-15 22:12:40 +01:00
|
|
|
)
|
|
|
|
|
2022-01-12 23:03:28 +01:00
|
|
|
func TestFixedLimiter_Add(t *testing.T) {
|
|
|
|
l := NewFixedLimiter(10)
|
|
|
|
if err := l.Allow(5); err != nil {
|
2021-12-15 22:12:40 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
if err := l.Allow(5); err != nil {
|
2021-12-15 22:12:40 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
if err := l.Allow(5); err != ErrLimitReached {
|
2021-12-15 22:12:40 +01:00
|
|
|
t.Fatalf("expected ErrLimitReached, got %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 23:03:28 +01:00
|
|
|
func TestFixedLimiter_AddSub(t *testing.T) {
|
|
|
|
l := NewFixedLimiter(10)
|
|
|
|
l.Allow(5)
|
|
|
|
if l.value != 5 {
|
|
|
|
t.Fatalf("expected value to be %d, got %d", 5, l.value)
|
2021-12-15 22:12:40 +01:00
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
l.Allow(-2)
|
|
|
|
if l.value != 3 {
|
|
|
|
t.Fatalf("expected value to be %d, got %d", 7, l.value)
|
2022-01-02 23:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 23:03:28 +01:00
|
|
|
func TestBytesLimiter_Add_Simple(t *testing.T) {
|
|
|
|
l := NewBytesLimiter(250*1024*1024, 24*time.Hour) // 250 MB per 24h
|
|
|
|
require.Nil(t, l.Allow(100*1024*1024))
|
|
|
|
require.Nil(t, l.Allow(100*1024*1024))
|
|
|
|
require.Equal(t, ErrLimitReached, l.Allow(300*1024*1024))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesLimiter_Add_Wait(t *testing.T) {
|
|
|
|
l := NewBytesLimiter(250*1024*1024, 24*time.Hour) // 250 MB per 24h (~ 303 bytes per 100ms)
|
|
|
|
require.Nil(t, l.Allow(250*1024*1024))
|
|
|
|
require.Equal(t, ErrLimitReached, l.Allow(400))
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
require.Nil(t, l.Allow(400))
|
|
|
|
}
|
|
|
|
|
2022-01-02 23:56:12 +01:00
|
|
|
func TestLimitWriter_WriteNoLimiter(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
lw := NewLimitWriter(&buf)
|
|
|
|
if _, err := lw.Write(make([]byte, 10)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := lw.Write(make([]byte, 1)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if buf.Len() != 11 {
|
|
|
|
t.Fatalf("expected buffer length to be %d, got %d", 11, buf.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitWriter_WriteOneLimiter(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2022-01-12 23:03:28 +01:00
|
|
|
l := NewFixedLimiter(10)
|
2022-01-02 23:56:12 +01:00
|
|
|
lw := NewLimitWriter(&buf, l)
|
|
|
|
if _, err := lw.Write(make([]byte, 10)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := lw.Write(make([]byte, 1)); err != ErrLimitReached {
|
|
|
|
t.Fatalf("expected ErrLimitReached, got %#v", err)
|
|
|
|
}
|
|
|
|
if buf.Len() != 10 {
|
|
|
|
t.Fatalf("expected buffer length to be %d, got %d", 10, buf.Len())
|
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
if l.value != 10 {
|
|
|
|
t.Fatalf("expected limiter value to be %d, got %d", 10, l.value)
|
2022-01-02 23:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitWriter_WriteTwoLimiters(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2022-01-12 23:03:28 +01:00
|
|
|
l1 := NewFixedLimiter(11)
|
|
|
|
l2 := NewFixedLimiter(9)
|
2022-01-02 23:56:12 +01:00
|
|
|
lw := NewLimitWriter(&buf, l1, l2)
|
|
|
|
if _, err := lw.Write(make([]byte, 8)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := lw.Write(make([]byte, 2)); err != ErrLimitReached {
|
|
|
|
t.Fatalf("expected ErrLimitReached, got %#v", err)
|
|
|
|
}
|
|
|
|
if buf.Len() != 8 {
|
|
|
|
t.Fatalf("expected buffer length to be %d, got %d", 8, buf.Len())
|
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
if l1.value != 8 {
|
|
|
|
t.Fatalf("expected limiter 1 value to be %d, got %d", 8, l1.value)
|
2022-01-02 23:56:12 +01:00
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
if l2.value != 8 {
|
|
|
|
t.Fatalf("expected limiter 2 value to be %d, got %d", 8, l2.value)
|
2021-12-15 22:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-12 23:03:28 +01:00
|
|
|
|
|
|
|
func TestLimitWriter_WriteTwoDifferentLimiters(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l1 := NewFixedLimiter(32)
|
|
|
|
l2 := NewBytesLimiter(8, 200*time.Millisecond)
|
|
|
|
lw := NewLimitWriter(&buf, l1, l2)
|
|
|
|
_, err := lw.Write(make([]byte, 8))
|
|
|
|
require.Nil(t, err)
|
|
|
|
_, err = lw.Write(make([]byte, 4))
|
|
|
|
require.Equal(t, ErrLimitReached, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitWriter_WriteTwoDifferentLimiters_Wait(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l1 := NewFixedLimiter(32)
|
|
|
|
l2 := NewBytesLimiter(8, 200*time.Millisecond)
|
|
|
|
lw := NewLimitWriter(&buf, l1, l2)
|
|
|
|
_, err := lw.Write(make([]byte, 8))
|
|
|
|
require.Nil(t, err)
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
_, err = lw.Write(make([]byte, 8))
|
|
|
|
require.Nil(t, err)
|
|
|
|
_, err = lw.Write(make([]byte, 4))
|
|
|
|
require.Equal(t, ErrLimitReached, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitWriter_WriteTwoDifferentLimiters_Wait_FixedLimiterFail(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l1 := NewFixedLimiter(11) // <<< This fails below
|
|
|
|
l2 := NewBytesLimiter(8, 200*time.Millisecond)
|
|
|
|
lw := NewLimitWriter(&buf, l1, l2)
|
|
|
|
_, err := lw.Write(make([]byte, 8))
|
|
|
|
require.Nil(t, err)
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
_, err = lw.Write(make([]byte, 8)) // <<< FixedLimiter fails
|
|
|
|
require.Equal(t, ErrLimitReached, err)
|
|
|
|
}
|