This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_concurrent_limiter_test.go
145 lines (126 loc) · 3.41 KB
/
router_concurrent_limiter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 tree xie
//
// 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.
package routerconcurrentlimiter
import (
"fmt"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
"time"
"github.com/vicanso/elton"
"github.com/stretchr/testify/assert"
)
func TestLimiter(t *testing.T) {
assert := assert.New(t)
limiter := NewLocalLimiter(map[string]uint32{
"/users/login": 10,
"/books/:id": 100,
})
cur, max := limiter.IncConcurrency("/not-macth-route")
assert.Equal(uint32(0), max)
assert.Equal(uint32(0), cur)
cur, max = limiter.IncConcurrency("/users/login")
assert.Equal(uint32(10), max)
assert.Equal(uint32(1), cur)
limiter.DecConcurrency("/not-macth-route")
assert.Equal(uint32(0), limiter.GetConcurrency("/not-macth-route"))
limiter.DecConcurrency("/users/login")
assert.Equal(uint32(0), limiter.GetConcurrency("/users/login"))
}
func TestNoLimiterPanic(t *testing.T) {
assert := assert.New(t)
defer func() {
r := recover()
assert.NotNil(r)
assert.Equal(r.(error), errRequireLimiter)
}()
New(Config{})
}
func TestRouterConcurrentLimiter(t *testing.T) {
limiter := NewLocalLimiter(map[string]uint32{
"POST /users/login": 1,
"GET /books/:id": 100,
})
fn := New(Config{
Limiter: limiter,
})
t.Run("skip", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/", nil)
c := elton.NewContext(nil, req)
c.Committed = true
done := false
c.Next = func() error {
done = true
return nil
}
err := fn(c)
assert.Nil(err)
assert.True(done)
})
t.Run("below limit", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/books/1", nil)
c := elton.NewContext(nil, req)
c.Route = "/books/:id"
var count int32
max := 10
c.Next = func() error {
atomic.AddInt32(&count, 1)
return nil
}
for index := 0; index < max; index++ {
err := fn(c)
assert.Nil(err)
}
assert.Equal(int32(max), count)
})
t.Run("higher than limit", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("POST", "/users/login", nil)
c := elton.NewContext(nil, req)
c.Route = "/users/login"
c.Next = func() error {
time.Sleep(10 * time.Millisecond)
return nil
}
done := make(chan bool)
go func() {
time.Sleep(2 * time.Millisecond)
err := fn(c)
assert.NotNil(err)
assert.Equal("category=elton-router-concurrent-limiter, message=too many requset, current:2, max:1", err.Error())
done <- true
}()
err := fn(c)
assert.Nil(err)
<-done
})
}
// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.9 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}