2020-10-30 23:27:33 +03:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package ratelimit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/juju/ratelimit"
|
2021-01-29 16:47:26 +03:00
|
|
|
rrouter "github.com/unistack-org/micro-router-register/v3"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
type TestRequest struct{}
|
|
|
|
type TestResponse struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateClientLimit(t *testing.T) {
|
|
|
|
// setup
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
|
|
|
tr := transport.NewTransport()
|
2020-10-30 23:27:33 +03:00
|
|
|
testRates := []int{1, 10, 20}
|
|
|
|
|
|
|
|
for _, limit := range testRates {
|
|
|
|
b := ratelimit.NewBucketWithRate(float64(limit), int64(limit))
|
|
|
|
|
|
|
|
c := client.NewClient(
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Register(register))),
|
2020-10-30 23:27:33 +03:00
|
|
|
client.Transport(tr),
|
|
|
|
// add the breaker wrapper
|
|
|
|
client.Wrap(NewClientWrapper(b, false)),
|
|
|
|
)
|
|
|
|
|
|
|
|
req := c.NewRequest(
|
|
|
|
"test.service",
|
|
|
|
"Test.Method",
|
|
|
|
&TestRequest{},
|
|
|
|
client.WithContentType("application/json"),
|
|
|
|
)
|
|
|
|
rsp := TestResponse{}
|
|
|
|
|
|
|
|
for j := 0; j < limit; j++ {
|
|
|
|
err := c.Call(context.TODO(), req, &rsp)
|
|
|
|
e := errors.Parse(err.Error())
|
|
|
|
if e.Code == 429 {
|
|
|
|
t.Errorf("Unexpected rate limit error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, rsp)
|
|
|
|
e := errors.Parse(err.Error())
|
|
|
|
if e.Code != 429 {
|
|
|
|
t.Errorf("Expected rate limit error, got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateServerLimit(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
testRates := []int{1, 5, 6, 10}
|
|
|
|
|
|
|
|
for _, limit := range testRates {
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
|
|
|
b := broker.NewBroker()
|
|
|
|
tr := transport.NewTransport()
|
2020-10-30 23:27:33 +03:00
|
|
|
_ = b
|
|
|
|
|
|
|
|
br := ratelimit.NewBucketWithRate(float64(limit), int64(limit))
|
|
|
|
c := client.NewClient(
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Register(register))),
|
2020-10-30 23:27:33 +03:00
|
|
|
client.Transport(tr))
|
|
|
|
|
|
|
|
name := fmt.Sprintf("test.service.%d", limit)
|
|
|
|
|
|
|
|
srv := server.NewServer(
|
|
|
|
server.Name(name),
|
2021-01-29 16:47:26 +03:00
|
|
|
// add register
|
|
|
|
server.Register(r),
|
2020-10-30 23:27:33 +03:00
|
|
|
server.Transport(tr),
|
|
|
|
// add broker
|
|
|
|
//server.Broker(b),
|
|
|
|
// add the breaker wrapper
|
|
|
|
server.WrapHandler(NewHandlerWrapper(br, false)),
|
|
|
|
)
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Handle(
|
|
|
|
srv.NewHandler(&Test{new(testHandler)}),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
req := c.NewRequest(name, "Test.Method", &TestRequest{}, client.WithContentType("application/json"))
|
|
|
|
rsp := TestResponse{}
|
|
|
|
|
|
|
|
for j := 0; j < limit; j++ {
|
|
|
|
if err := c.Call(context.TODO(), req, &rsp); err != nil {
|
|
|
|
t.Fatalf("Unexpected request error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, &rsp)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected rate limit error, got nil: rate %d, err %v", limit, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := errors.Parse(err.Error())
|
|
|
|
if e.Code != 429 {
|
|
|
|
t.Fatalf("Expected rate limit error, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Stop()
|
|
|
|
|
|
|
|
// artificial test delay
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|