-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlimit_test.go
56 lines (47 loc) · 1.18 KB
/
limit_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
package sessdata
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/go-kiss/anylink/base"
)
// func TestCheckUser(t *testing.T) {
// user["user1"] = User{Password: "7c4a8d09ca3762af61e59520943dc26494f8941b"}
// user["user2"] = User{Password: "7c4a8d09ca3762af61e59520943dc26494f8941c"}
//
// var res bool
// res = CheckUser("user1", "123456", "")
// AssertTrue(t, res == true)
//
// res = CheckUser("user2", "123457", "")
// AssertTrue(t, res == false)
// }
func TestLimitClient(t *testing.T) {
assert := assert.New(t)
base.Cfg.MaxClient = 2
base.Cfg.MaxUserClient = 1
res1 := LimitClient("user1", false)
res2 := LimitClient("user1", false)
res3 := LimitClient("user2", false)
res4 := LimitClient("user3", false)
res5 := LimitClient("user1", true)
assert.True(res1)
assert.False(res2)
assert.True(res3)
assert.False(res4)
assert.True(res5)
}
func TestLimitWait(t *testing.T) {
assert := assert.New(t)
limit := NewLimitRater(1, 2)
err := limit.Wait(2)
assert.Nil(err)
start := time.Now()
err = limit.Wait(2)
assert.Nil(err)
err = limit.Wait(1)
assert.Nil(err)
end := time.Now()
sub := end.Sub(start)
assert.Equal(3, int(sub.Seconds()))
}