1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
package memory_test
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ulule/limiter/v3/drivers/store/memory"
)
func TestCacheIncrementSequential(t *testing.T) {
is := require.New(t)
key := "foobar"
cache := memory.NewCache(10 * time.Nanosecond)
duration := 50 * time.Millisecond
deleted := time.Now().Add(duration).UnixNano()
epsilon := 0.001
x, expire := cache.Increment(key, 1, duration)
is.Equal(int64(1), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Increment(key, 2, duration)
is.Equal(int64(3), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
time.Sleep(duration)
deleted = time.Now().Add(duration).UnixNano()
x, expire = cache.Increment(key, 1, duration)
is.Equal(int64(1), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
}
func TestCacheIncrementConcurrent(t *testing.T) {
is := require.New(t)
goroutines := 300
ops := 500
expected := int64(0)
for i := 0; i < goroutines; i++ {
if (i % 3) == 0 {
for j := 0; j < ops; j++ {
expected += int64(i + j)
}
}
}
key := "foobar"
cache := memory.NewCache(10 * time.Nanosecond)
wg := &sync.WaitGroup{}
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(i int) {
if (i % 3) == 0 {
time.Sleep(1 * time.Second)
for j := 0; j < ops; j++ {
cache.Increment(key, int64(i+j), (1 * time.Second))
}
} else {
time.Sleep(50 * time.Millisecond)
stopAt := time.Now().Add(500 * time.Millisecond)
for time.Now().Before(stopAt) {
cache.Increment(key, int64(i), (75 * time.Millisecond))
}
}
wg.Done()
}(i)
}
wg.Wait()
value, expire := cache.Get(key, (100 * time.Millisecond))
is.Equal(expected, value)
is.True(time.Now().Before(expire))
}
func TestCacheGet(t *testing.T) {
is := require.New(t)
key := "foobar"
cache := memory.NewCache(10 * time.Nanosecond)
duration := 50 * time.Millisecond
deleted := time.Now().Add(duration).UnixNano()
epsilon := 0.001
x, expire := cache.Get(key, duration)
is.Equal(int64(0), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
}
func TestCacheReset(t *testing.T) {
is := require.New(t)
key := "foobar"
cache := memory.NewCache(10 * time.Nanosecond)
duration := 50 * time.Millisecond
deleted := time.Now().Add(duration).UnixNano()
epsilon := 0.001
x, expire := cache.Get(key, duration)
is.Equal(int64(0), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Increment(key, 1, duration)
is.Equal(int64(1), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Increment(key, 1, duration)
is.Equal(int64(2), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Reset(key, duration)
is.Equal(int64(0), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Increment(key, 1, duration)
is.Equal(int64(1), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
x, expire = cache.Increment(key, 1, duration)
is.Equal(int64(2), x)
is.InEpsilon(deleted, expire.UnixNano(), epsilon)
}
|