Skip to content

Commit

Permalink
fix: actions
Browse files Browse the repository at this point in the history
  • Loading branch information
WenZu-Zhou committed Oct 13, 2023
1 parent aab7743 commit 9ae5463
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .licenserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"**/*.{yml,toml}": "# Copyright 2023 ecodeclub",
"**/*.sh": "# Copyright 2023 ecodeclub",
"ignore": [
"*.mock.go"
"**/*.mock.go"
]
}
14 changes: 0 additions & 14 deletions internal/mocks/redis_mock.go → internal/mocks/redis.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions internal/ratelimit/mocks/ratelimit.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 31 additions & 82 deletions middlewares/activelimit/locallimit/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,112 +18,61 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLocalActiveLimit_Build(t *testing.T) {

testCases := []struct {
name string
maxCount int64
getReq func() *http.Request
createMiddleware func(maxActive int64) gin.HandlerFunc
before func(server *gin.Engine)

// 响应的code
const (
url = "/"
)
tests := []struct {
name string
countActive int64
reqBuilder func(t *testing.T) *http.Request

// 预期响应
wantCode int
interval time.Duration
}{
{
name: "开启限流,LocalLimit正常操作",

createMiddleware: func(maxActive int64) gin.HandlerFunc {
return NewLocalActiveLimit(maxActive).Build()
},
getReq: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/activelimit", nil)
name: "正常通过",
countActive: 0,
reqBuilder: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
return req
},
before: func(server *gin.Engine) {},

maxCount: 1,
wantCode: 200,
wantCode: http.StatusNoContent,
},
{
name: "开启限流,LocalLimit 有一个人很久没出来,新请求被限流",

createMiddleware: func(maxActive int64) gin.HandlerFunc {
return NewLocalActiveLimit(maxActive).Build()
},
getReq: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/activelimit", nil)
name: "限流中",
countActive: 1,
reqBuilder: func(t *testing.T) *http.Request {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
return req
},
before: func(server *gin.Engine) {
req, err := http.NewRequest(http.MethodGet, "/activelimit3", nil)
require.NoError(t, err)
resp := httptest.NewRecorder()
server.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
},

maxCount: 1,
wantCode: http.StatusTooManyRequests,
},
{
name: "开启限流,LocalLimit 有一个人很久没出来,等待前面的请求退出后,成功通过",

createMiddleware: func(maxActive int64) gin.HandlerFunc {
return NewLocalActiveLimit(maxActive).Build()
},
getReq: func() *http.Request {
req, err := http.NewRequest(http.MethodGet, "/activelimit", nil)
require.NoError(t, err)
return req
},
before: func(server *gin.Engine) {
req, err := http.NewRequest(http.MethodGet, "/activelimit3", nil)
require.NoError(t, err)
resp := httptest.NewRecorder()
server.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
},

interval: time.Millisecond * 600,
maxCount: 1,
wantCode: http.StatusOK,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
limit := NewLocalActiveLimit(1)
limit.countActive.Store(tt.countActive)
server := gin.Default()
server.Use(tc.createMiddleware(tc.maxCount))
server.GET("/activelimit", func(ctx *gin.Context) {
ctx.Status(http.StatusOK)
})
server.GET("/activelimit3", func(ctx *gin.Context) {
time.Sleep(time.Millisecond * 300)
ctx.Status(http.StatusOK)
server.Use(limit.Build())
server.GET(url, func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
resp := httptest.NewRecorder()
go func() {
tc.before(server)
}()
// 加延时保证 tc.before 执行
time.Sleep(time.Millisecond * 10)

time.Sleep(tc.interval)
server.ServeHTTP(resp, tc.getReq())
assert.Equal(t, tc.wantCode, resp.Code)
req := tt.reqBuilder(t)
recorder := httptest.NewRecorder()

server.ServeHTTP(recorder, req)

assert.Equal(t, tt.wantCode, recorder.Code)
})
}

}
14 changes: 14 additions & 0 deletions script/integrate_test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 ecodeclub
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env bash

set -e
Expand Down
14 changes: 14 additions & 0 deletions script/integration_test_compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 ecodeclub
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: '3.0'
services:
redis:
Expand Down

0 comments on commit 9ae5463

Please sign in to comment.