|
|
@ -9,25 +9,44 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestTimeoutLimit(t *testing.T) {
|
|
|
|
func TestTimeoutLimit(t *testing.T) {
|
|
|
|
limit := NewTimeoutLimit(2)
|
|
|
|
tests := []struct {
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
name string
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
interval time.Duration
|
|
|
|
var wait1, wait2, wait3 sync.WaitGroup
|
|
|
|
}{
|
|
|
|
wait1.Add(1)
|
|
|
|
{
|
|
|
|
wait2.Add(1)
|
|
|
|
name: "no wait",
|
|
|
|
wait3.Add(1)
|
|
|
|
},
|
|
|
|
go func() {
|
|
|
|
{
|
|
|
|
wait1.Wait()
|
|
|
|
name: "wait",
|
|
|
|
wait2.Done()
|
|
|
|
interval: time.Millisecond * 100,
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
},
|
|
|
|
wait3.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wait1.Done()
|
|
|
|
for _, test := range tests {
|
|
|
|
wait2.Wait()
|
|
|
|
test := test
|
|
|
|
assert.Nil(t, limit.Borrow(time.Second))
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
wait3.Wait()
|
|
|
|
limit := NewTimeoutLimit(2)
|
|
|
|
assert.Equal(t, ErrTimeout, limit.Borrow(time.Millisecond*100))
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
var wait1, wait2, wait3 sync.WaitGroup
|
|
|
|
assert.Equal(t, ErrLimitReturn, limit.Return())
|
|
|
|
wait1.Add(1)
|
|
|
|
|
|
|
|
wait2.Add(1)
|
|
|
|
|
|
|
|
wait3.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
wait1.Wait()
|
|
|
|
|
|
|
|
wait2.Done()
|
|
|
|
|
|
|
|
time.Sleep(test.interval)
|
|
|
|
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
|
|
|
|
wait3.Done()
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
wait1.Done()
|
|
|
|
|
|
|
|
wait2.Wait()
|
|
|
|
|
|
|
|
assert.Nil(t, limit.Borrow(time.Second))
|
|
|
|
|
|
|
|
wait3.Wait()
|
|
|
|
|
|
|
|
assert.Equal(t, ErrTimeout, limit.Borrow(time.Millisecond*100))
|
|
|
|
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
|
|
|
|
assert.Equal(t, ErrLimitReturn, limit.Return())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|