-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathregistry_test.go
101 lines (90 loc) · 2.66 KB
/
registry_test.go
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
package limiters_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mennanov/limiters"
)
type testingLimiter struct{}
func newTestingLimiter() *testingLimiter {
return &testingLimiter{}
}
func (l *testingLimiter) Limit(context.Context) (time.Duration, error) {
return 0, nil
}
func TestRegistry_GetOrCreate(t *testing.T) {
registry := limiters.NewRegistry()
called := false
clock := newFakeClock()
limiter := newTestingLimiter()
l := registry.GetOrCreate("key", func() interface{} {
called = true
return limiter
}, time.Second, clock.Now())
assert.Equal(t, limiter, l)
// Verify that the closure was called to create a value.
assert.True(t, called)
called = false
l = registry.GetOrCreate("key", func() interface{} {
called = true
return newTestingLimiter()
}, time.Second, clock.Now())
assert.Equal(t, limiter, l)
// Verify that the closure was NOT called to create a value as it already exists.
assert.False(t, called)
}
func TestRegistry_DeleteExpired(t *testing.T) {
registry := limiters.NewRegistry()
clock := newFakeClock()
// Add limiters to the registry.
for i := 1; i <= 10; i++ {
registry.GetOrCreate(fmt.Sprintf("key%d", i), func() interface{} {
return newTestingLimiter()
}, time.Second*time.Duration(i), clock.Now())
}
clock.Sleep(time.Second * 3)
// "touch" the "key3" value that is about to be expired so that its expiration time is extended for 1s.
registry.GetOrCreate("key3", func() interface{} {
return newTestingLimiter()
}, time.Second, clock.Now())
assert.Equal(t, 2, registry.DeleteExpired(clock.Now()))
for i := 1; i <= 10; i++ {
if i <= 2 {
assert.False(t, registry.Exists(fmt.Sprintf("key%d", i)))
} else {
assert.True(t, registry.Exists(fmt.Sprintf("key%d", i)))
}
}
}
func TestRegistry_Delete(t *testing.T) {
registry := limiters.NewRegistry()
clock := newFakeClock()
item := &struct{}{}
require.Equal(t, item, registry.GetOrCreate("key", func() interface{} {
return item
}, time.Second, clock.Now()))
require.Equal(t, item, registry.GetOrCreate("key", func() interface{} {
return &struct{}{}
}, time.Second, clock.Now()))
registry.Delete("key")
assert.False(t, registry.Exists("key"))
}
// This test is expected to fail when run with the --race flag.
func TestRegistry_ConcurrentUsage(t *testing.T) {
registry := limiters.NewRegistry()
clock := newFakeClock()
for i := 0; i < 10; i++ {
go func(i int) {
registry.GetOrCreate(strconv.Itoa(i), func() interface{} { return &struct{}{} }, 0, clock.Now())
}(i)
}
for i := 0; i < 10; i++ {
go func(i int) {
registry.DeleteExpired(clock.Now())
}(i)
}
}