2023-05-15 21:15:29 -04:00
|
|
|
package limiter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var c *ConnLimiter
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
c = NewConnLimiter(1, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnLimiter_AddConnCount(t *testing.T) {
|
2023-05-16 12:56:14 -04:00
|
|
|
t.Log(c.AddConnCount("1", "1", true))
|
|
|
|
t.Log(c.AddConnCount("1", "2", true))
|
2023-05-15 21:15:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnLimiter_DelConnCount(t *testing.T) {
|
2023-05-16 12:56:14 -04:00
|
|
|
t.Log(c.AddConnCount("1", "1", true))
|
|
|
|
t.Log(c.AddConnCount("1", "2", true))
|
2023-05-15 21:15:29 -04:00
|
|
|
c.DelConnCount("1", "1")
|
2023-05-16 12:56:14 -04:00
|
|
|
t.Log(c.AddConnCount("1", "2", true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnLimiter_ClearPacketOnlineIP(t *testing.T) {
|
|
|
|
t.Log(c.AddConnCount("1", "1", false))
|
|
|
|
t.Log(c.AddConnCount("1", "2", false))
|
|
|
|
c.ClearPacketOnlineIP()
|
|
|
|
t.Log(c.AddConnCount("1", "2", true))
|
|
|
|
c.DelConnCount("1", "2")
|
|
|
|
t.Log(c.AddConnCount("1", "1", false))
|
2023-05-15 21:15:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkConnLimiter(b *testing.B) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2023-05-16 12:56:14 -04:00
|
|
|
c.AddConnCount("1", "2", true)
|
2023-05-15 21:15:29 -04:00
|
|
|
c.DelConnCount("1", "2")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|