V2bX/limiter/conn_test.go

57 lines
1.1 KiB
Go
Raw Normal View History

package limiter
import (
"sync"
"testing"
2023-05-18 21:49:31 -04:00
"time"
)
var c *ConnLimiter
func init() {
c = NewConnLimiter(1, 1, true)
}
func TestConnLimiter_AddConnCount(t *testing.T) {
t.Log(c.AddConnCount("1", "1", true))
t.Log(c.AddConnCount("1", "2", true))
}
func TestConnLimiter_DelConnCount(t *testing.T) {
t.Log(c.AddConnCount("1", "1", true))
t.Log(c.AddConnCount("1", "2", true))
c.DelConnCount("1", "1")
t.Log(c.AddConnCount("1", "2", true))
}
2023-05-18 21:49:31 -04:00
func TestConnLimiter_ClearOnlineIP(t *testing.T) {
t.Log(c.AddConnCount("1", "1", false))
t.Log(c.AddConnCount("1", "2", false))
c.ClearOnlineIP()
t.Log(c.AddConnCount("1", "2", true))
c.DelConnCount("1", "2")
t.Log(c.AddConnCount("1", "1", false))
2023-05-18 21:49:31 -04:00
// not realtime
c.realtime = false
t.Log(c.AddConnCount("3", "2", true))
c.ClearOnlineIP()
t.Log(c.ip.Load("3"))
time.Sleep(time.Minute)
c.ClearOnlineIP()
t.Log(c.ip.Load("3"))
}
func BenchmarkConnLimiter(b *testing.B) {
wg := sync.WaitGroup{}
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
c.AddConnCount("1", "2", true)
c.DelConnCount("1", "2")
wg.Done()
}()
}
wg.Wait()
}