-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslider_test.go
50 lines (39 loc) · 1.24 KB
/
slider_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
package equalizer
import (
"context"
"testing"
"time"
"github.com/reugn/equalizer/internal/assert"
)
func TestSlider(t *testing.T) {
slider, err := NewSlider(time.Second, 100*time.Millisecond, 32)
assert.IsNil(t, err)
var quota bool
for i := 0; i < 32; i++ {
quota = slider.TryAcquire()
}
assert.Equal(t, quota, true)
quota = slider.TryAcquire()
assert.Equal(t, quota, false)
time.Sleep(1010 * time.Millisecond)
quota = slider.TryAcquire()
assert.Equal(t, quota, true)
assert.IsNil(t, slider.Acquire(context.Background()))
}
func TestSlider_Context(t *testing.T) {
slider, err := NewSlider(time.Second, 200*time.Millisecond, 1)
assert.IsNil(t, err)
assert.IsNil(t, slider.Acquire(context.Background()))
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond))
defer cancel()
err = slider.Acquire(ctx)
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
func TestSlider_Errors(t *testing.T) {
slider, err := NewSlider(time.Second, 200*time.Millisecond, -1)
assert.ErrorContains(t, err, "nonpositive capacity")
assert.IsNil(t, slider)
slider, err = NewSlider(100*time.Millisecond, time.Second, 32)
assert.ErrorContains(t, err, "slidingInterval must not exceed window")
assert.IsNil(t, slider)
}