123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package email
- import (
- "testing"
- "github.com/smartystreets/goconvey/convey"
- "time"
- )
- var fd *FastDetector
- var speedThreshold = 3
- var overspeedThreshold = 2
- var overlimit = speedThreshold * overspeedThreshold
- func TestEmailnewFastDetector(t *testing.T) {
- convey.Convey("newFastDetector", t, func(ctx convey.C) {
- fd = NewFastDetector(speedThreshold, overspeedThreshold)
- ctx.Convey("Then fd should not be nil.", func(ctx convey.C) {
- ctx.So(fd, convey.ShouldNotBeNil)
- })
- })
- }
- //一秒多少个
- func limit(t *testing.T, ids []int64) {
- now := time.Now().UnixNano()
- limit := len(ids)
- for i := 0; i < limit; i++ {
- unique := ids[i]
- res := fd.Detect(unique)
- hit := fd.IsFastUnique(unique)
- t.Logf("index=%d, unique=%d, res=%v, hit=%v, detector(%+v)", i, unique, res, hit, fd)
- }
- if diff := now + 1e9 - time.Now().UnixNano(); diff > 0 {
- time.Sleep(time.Duration(diff))
- }
- return
- }
- //分片
- func batch(tk []int64, length int) (path [][]int64) {
- ll := len(tk) / length
- if len(tk)%length > 0 {
- ll++
- }
- path = [][]int64{}
- item := []int64{}
- for i := 0; i < len(tk); i++ {
- if i > 0 && i%length == 0 {
- path = append(path, item)
- item = []int64{}
- }
- item = append(item, tk[i])
- }
- if len(item) > 0 {
- path = append(path, item)
- }
- return
- }
- func TestEmaildetect(t *testing.T) {
- TestEmailnewFastDetector(t)
- ids := []int64{222, 333}
- //convey.Convey("慢速,无超限名额", t, func() {
- // tk := []int64{}
- // for i := 1; i <= overlimit*2; i++ {
- // tk = append(tk, ids[0])
- // }
- // path := batch(tk, speedThreshold-1)
- // for _, tk := range path {
- // limit(t, tk)
- // }
- //})
- //return
- //convey.Convey("部分超限,但未超次数,无超限名额", t, func() {
- // tk := []int64{}
- // for i := 1; i < overlimit; i++ {
- // tk = append(tk, ids[0])
- // }
- // path := [][]int64{tk}
- // path = append(path, batch(tk, speedThreshold-1)...)
- // for _, tk := range path {
- // limit(t, tk)
- // }
- //})
- //return
- convey.Convey("部分超限且超次数,没有回落,有超限名额且被替代, 几秒后再超限,但没有超限名单", t, func() {
- tk := []int64{}
- tk2 := []int64{}
- for i := 1; i < overlimit*2; i++ {
- tk = append(tk, ids[0])
- tk2 = append(tk2, ids[1])
- }
- path := [][]int64{tk}
- path = append(path, batch(tk, speedThreshold+1)...)
- path = append(path, batch(tk2, speedThreshold+1)...)
- for _, tk := range path {
- limit(t, tk)
- }
- limit(t, path[len(path)-1])
- sl := int64(5)
- time.Sleep(time.Duration(sl) * time.Second)
- t.Logf("after %ds sleep", sl)
- limit(t, path[len(path)-1])
- })
- convey.Convey("部分超限且超次数,有超限名额,但有回落,没有超限名额", t, func(ctx convey.C) {
- tk := []int64{}
- for i := 1; i < overlimit*2; i++ {
- tk = append(tk, ids[0])
- }
- path := batch(tk, speedThreshold+1)
- path = append(path, batch(tk, speedThreshold-1)...)
- for _, tk := range path {
- limit(t, tk)
- }
- })
- }
|