|
|
@ -9,6 +9,22 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestTimeoutLimit(t *testing.T) {
|
|
|
|
func TestTimeoutLimit(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
|
|
|
name string
|
|
|
|
|
|
|
|
interval time.Duration
|
|
|
|
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
name: "no wait",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
name: "wait",
|
|
|
|
|
|
|
|
interval: time.Millisecond * 100,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
limit := NewTimeoutLimit(2)
|
|
|
|
limit := NewTimeoutLimit(2)
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
|
assert.Nil(t, limit.Borrow(time.Millisecond*200))
|
|
|
@ -19,6 +35,7 @@ func TestTimeoutLimit(t *testing.T) {
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
wait1.Wait()
|
|
|
|
wait1.Wait()
|
|
|
|
wait2.Done()
|
|
|
|
wait2.Done()
|
|
|
|
|
|
|
|
time.Sleep(test.interval)
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
wait3.Done()
|
|
|
|
wait3.Done()
|
|
|
|
}()
|
|
|
|
}()
|
|
|
@ -30,4 +47,6 @@ func TestTimeoutLimit(t *testing.T) {
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Nil(t, limit.Return())
|
|
|
|
assert.Equal(t, ErrLimitReturn, limit.Return())
|
|
|
|
assert.Equal(t, ErrLimitReturn, limit.Return())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|