parent
71649abefe
commit
5d6392b65d
1 changed files with 6 additions and 2 deletions
|
@ -9,15 +9,15 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestThrottle(t *testing.T) {
|
func TestThrottle(t *testing.T) {
|
||||||
const threshold = 1
|
const threshold = 1
|
||||||
|
busy := make(chan struct{})
|
||||||
|
|
||||||
srv := httptest.NewServer(throttledFn(threshold,
|
srv := httptest.NewServer(throttledFn(threshold,
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
time.Sleep(20 * time.Millisecond)
|
<-busy
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,6 +35,10 @@ func TestThrottle(t *testing.T) {
|
||||||
got := make(map[int]int, cap(codes))
|
got := make(map[int]int, cap(codes))
|
||||||
for i := 0; i < cap(codes); i++ {
|
for i := 0; i < cap(codes); i++ {
|
||||||
got[<-codes]++
|
got[<-codes]++
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
close(busy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
want := map[int]int{200: 1, 429: 1}
|
want := map[int]int{200: 1, 429: 1}
|
||||||
|
|
Loading…
Add table
Reference in a new issue