diff --git a/.licenserc.json b/.licenserc.json index 4709c9b..32d9c55 100644 --- a/.licenserc.json +++ b/.licenserc.json @@ -3,6 +3,6 @@ "**/*.{yml,toml}": "# Copyright 2023 ecodeclub", "**/*.sh": "# Copyright 2023 ecodeclub", "ignore": [ - "*.mock.go" + "**/*.mock.go" ] } \ No newline at end of file diff --git a/internal/mocks/redis_mock.go b/internal/mocks/redis.mock.go similarity index 99% rename from internal/mocks/redis_mock.go rename to internal/mocks/redis.mock.go index dc5180a..6013341 100644 --- a/internal/mocks/redis_mock.go +++ b/internal/mocks/redis.mock.go @@ -1,17 +1,3 @@ -// 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. - // Code generated by MockGen. DO NOT EDIT. // Source: github.com/redis/go-redis/v9 (interfaces: Cmdable) // diff --git a/internal/ratelimit/mocks/ratelimit.mock.go b/internal/ratelimit/mocks/ratelimit.mock.go index 063b1f2..01883ae 100644 --- a/internal/ratelimit/mocks/ratelimit.mock.go +++ b/internal/ratelimit/mocks/ratelimit.mock.go @@ -1,17 +1,3 @@ -// 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. - // Code generated by MockGen. DO NOT EDIT. // Source: types.go diff --git a/middlewares/activelimit/locallimit/builder_test.go b/middlewares/activelimit/locallimit/builder_test.go index ddaf5cb..9831154 100644 --- a/middlewares/activelimit/locallimit/builder_test.go +++ b/middlewares/activelimit/locallimit/builder_test.go @@ -18,7 +18,6 @@ import ( "net/http" "net/http/httptest" "testing" - "time" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" @@ -26,104 +25,54 @@ import ( ) 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) }) } - } diff --git a/script/integrate_test.sh b/script/integrate_test.sh index c14633b..975029d 100644 --- a/script/integrate_test.sh +++ b/script/integrate_test.sh @@ -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 diff --git a/script/integration_test_compose.yml b/script/integration_test_compose.yml index b9112c8..48f6658 100644 --- a/script/integration_test_compose.yml +++ b/script/integration_test_compose.yml @@ -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: