Skip to content

Commit

Permalink
resolve the conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Aug 23, 2023
1 parent b2b3864 commit 9b6440a
Showing 1 changed file with 0 additions and 136 deletions.
136 changes: 0 additions & 136 deletions tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"math"
"net/http"
"path"
"reflect"
"sort"
Expand Down Expand Up @@ -1507,138 +1506,3 @@ func TestClientWatchWithRevision(t *testing.T) {
}
}
}

func TestGRPCRateLimitMiddleware(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 3)
re.NoError(err)
defer cluster.Destroy()
endpoints := runServer(re, cluster)
client := setupCli(re, ctx, endpoints)
defer client.Close()
leader := cluster.GetServer(cluster.GetLeader())
input := map[string]interface{}{
"enable-grpc-rate-limit": "true",
}
data, err := json.Marshal(input)
re.NoError(err)
req, _ := http.NewRequest(http.MethodPost, leader.GetAddr()+"/pd/api/v1/service-middleware/config", bytes.NewBuffer(data))
// dialClient used to dial http request.
var dialClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}

resp, err := dialClient.Do(req)
re.NoError(err)
resp.Body.Close()
re.Equal(leader.GetServer().GetServiceMiddlewarePersistOptions().IsGRPCRateLimitEnabled(), true)

// returns StatusOK when no rate-limit config
_, err = client.GetStore(ctx, 1)
re.NoError(err)
input = make(map[string]interface{})
input["label"] = "GetStore"
input["qps"] = 0.5
input["concurrency"] = 1
jsonBody, err := json.Marshal(input)
re.NoError(err)
req, _ = http.NewRequest(http.MethodPost, leader.GetAddr()+"/pd/api/v1/service-middleware/config/grpc-rate-limit", bytes.NewBuffer(jsonBody))
resp, err = dialClient.Do(req)
re.NoError(err)
resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
expectErr := errs.ErrRateLimitExceeded.Error()
for i := 0; i < 3; i++ {
_, err := client.GetStore(ctx, 1)
if i > 0 {
re.Contains(err.Error(), expectErr)
} else {
re.NoError(err)
}
}

// qps = 0.5, so sleep 2s
time.Sleep(time.Second * 2)
for i := 0; i < 2; i++ {
_, err := client.GetStore(ctx, 1)
if i > 0 {
re.Contains(err.Error(), expectErr)
} else {
re.NoError(err)
}
}

// test only sleep 1s
time.Sleep(time.Second)
for i := 0; i < 2; i++ {
_, err := client.GetStore(ctx, 1)
re.Contains(err.Error(), expectErr)
}

// resign leader
oldLeaderName := leader.GetServer().Name()
leader.GetServer().GetMember().ResignEtcdLeader(leader.GetServer().Context(), oldLeaderName, "")
var servers []*server.Server
for _, s := range cluster.GetServers() {
servers = append(servers, s.GetServer())
}
server.MustWaitLeader(re, servers)
leader = cluster.GetServer(cluster.GetLeader())
re.Equal(leader.GetServer().GetServiceMiddlewarePersistOptions().IsGRPCRateLimitEnabled(), true)
cfg, ok := leader.GetServer().GetGRPCRateLimitConfig().LimiterConfig["GetStore"]
re.Equal(ok, true)
re.Equal(cfg.ConcurrencyLimit, uint64(1))
re.Equal(cfg.QPS, 0.5)
re.Equal(cfg.QPSBurst, 1)

testutil.Eventually(re, func() bool {
_, err = client.GetRegion(ctx, []byte("a"))
return err == nil
})
for i := 0; i < 3; i++ {
_, err := client.GetStore(ctx, 1)
if i > 0 {
re.Contains(err.Error(), expectErr)
} else {
re.NoError(err)
}
}

// qps = 0.5, so sleep 2s
time.Sleep(time.Second * 2)
for i := 0; i < 2; i++ {
_, err := client.GetStore(ctx, 1)
if i > 0 {
re.Contains(err.Error(), expectErr)
} else {
re.NoError(err)
}
}

// test only sleep 1s
time.Sleep(time.Second)
for i := 0; i < 2; i++ {
_, err := client.GetStore(ctx, 1)
re.Contains(err.Error(), expectErr)
}

input = map[string]interface{}{
"enable-grpc-rate-limit": "false",
}
data, err = json.Marshal(input)
re.NoError(err)
req, _ = http.NewRequest(http.MethodPost, leader.GetAddr()+"/pd/api/v1/service-middleware/config", bytes.NewBuffer(data))
resp, err = dialClient.Do(req)
re.NoError(err)
resp.Body.Close()
re.Equal(leader.GetServer().GetServiceMiddlewarePersistOptions().IsGRPCRateLimitEnabled(), false)

for i := 0; i < 3; i++ {
_, err := client.GetStore(ctx, 1)
re.NoError(err)
}
}

0 comments on commit 9b6440a

Please sign in to comment.