You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/request/tpslimiter_test.go

47 lines
873 B

package request
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestLimit(t *testing.T) {
a := assert.New(t)
l := NewTPSLimiter()
finished := make(chan struct{})
go func() {
l.Limit(context.Background(), "token", 1, 1)
close(finished)
}()
select {
case <-finished:
case <-time.After(10 * time.Second):
a.Fail("Limit should be finished instantly.")
}
finished = make(chan struct{})
go func() {
l.Limit(context.Background(), "token", 1, 1)
close(finished)
}()
select {
case <-finished:
case <-time.After(2 * time.Second):
a.Fail("Limit should be finished in 1 second.")
}
finished = make(chan struct{})
go func() {
l.Limit(context.Background(), "token", 10, 1)
close(finished)
}()
select {
case <-finished:
case <-time.After(1 * time.Second):
a.Fail("Limit should be finished instantly.")
}
}