2020-11-09 10:45:44 +08:00
|
|
|
package iplibrary
|
|
|
|
|
|
|
|
|
|
import (
|
2021-01-03 20:18:47 +08:00
|
|
|
"github.com/iwind/TeaGo/assert"
|
|
|
|
|
"github.com/iwind/TeaGo/logs"
|
2020-11-09 10:45:44 +08:00
|
|
|
"runtime"
|
|
|
|
|
"strconv"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-03 20:18:47 +08:00
|
|
|
func TestIPList_Add_Empty(t *testing.T) {
|
|
|
|
|
ipList := NewIPList()
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
})
|
|
|
|
|
logs.PrintAsJSON(ipList.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(ipList.ipMap, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Add_One(t *testing.T) {
|
|
|
|
|
ipList := NewIPList()
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.1"),
|
|
|
|
|
})
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IPTo: IP2Long("192.168.1.2"),
|
|
|
|
|
})
|
|
|
|
|
logs.PrintAsJSON(ipList.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(ipList.ipMap, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Update(t *testing.T) {
|
|
|
|
|
ipList := NewIPList()
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.1"),
|
|
|
|
|
})
|
|
|
|
|
/**ipList.Add(&IPItem{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.1"),
|
|
|
|
|
})**/
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPTo: IP2Long("192.168.1.2"),
|
|
|
|
|
})
|
|
|
|
|
logs.PrintAsJSON(ipList.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(ipList.ipMap, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Add_Range(t *testing.T) {
|
|
|
|
|
ipList := NewIPList()
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.1"),
|
|
|
|
|
IPTo: IP2Long("192.168.2.1"),
|
|
|
|
|
})
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IPTo: IP2Long("192.168.1.2"),
|
|
|
|
|
})
|
|
|
|
|
t.Log(len(ipList.ipMap), "ips")
|
|
|
|
|
logs.PrintAsJSON(ipList.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(ipList.ipMap, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Add_Overflow(t *testing.T) {
|
|
|
|
|
a := assert.NewAssertion(t)
|
|
|
|
|
|
|
|
|
|
ipList := NewIPList()
|
|
|
|
|
ipList.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.1"),
|
|
|
|
|
IPTo: IP2Long("192.169.255.1"),
|
|
|
|
|
})
|
|
|
|
|
t.Log(len(ipList.ipMap), "ips")
|
|
|
|
|
a.IsTrue(len(ipList.ipMap) <= 65535)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 10:45:44 +08:00
|
|
|
func TestNewIPList_Memory(t *testing.T) {
|
|
|
|
|
list := NewIPList()
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 200_0000; i++ {
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
IPFrom: 1,
|
|
|
|
|
IPTo: 2,
|
|
|
|
|
ExpiredAt: time.Now().Unix(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Log("ok")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Contains(t *testing.T) {
|
|
|
|
|
list := NewIPList()
|
|
|
|
|
for i := 0; i < 255; i++ {
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
Id: int64(i),
|
2021-01-03 20:18:47 +08:00
|
|
|
IPFrom: IP2Long(strconv.Itoa(i) + ".168.0.1"),
|
|
|
|
|
IPTo: IP2Long(strconv.Itoa(i) + ".168.255.1"),
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-01-03 20:18:47 +08:00
|
|
|
t.Log(len(list.ipMap))
|
|
|
|
|
|
|
|
|
|
before := time.Now()
|
2020-11-09 10:45:44 +08:00
|
|
|
t.Log(list.Contains(IP2Long("192.168.1.100")))
|
|
|
|
|
t.Log(list.Contains(IP2Long("192.168.2.100")))
|
2021-01-03 20:18:47 +08:00
|
|
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Delete(t *testing.T) {
|
|
|
|
|
list := NewIPList()
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.0.1"),
|
|
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IPFrom: IP2Long("192.168.0.1"),
|
|
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
t.Log("===BEFORE===")
|
|
|
|
|
logs.PrintAsJSON(list.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(list.ipMap, t)
|
|
|
|
|
|
|
|
|
|
list.Delete(1)
|
|
|
|
|
|
|
|
|
|
t.Log("===AFTER===")
|
|
|
|
|
logs.PrintAsJSON(list.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(list.ipMap, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGC(t *testing.T) {
|
|
|
|
|
list := NewIPList()
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.100"),
|
|
|
|
|
IPTo: IP2Long("192.168.1.101"),
|
|
|
|
|
ExpiredAt: time.Now().Unix() + 1,
|
|
|
|
|
})
|
|
|
|
|
list.Add(&IPItem{
|
|
|
|
|
Id: 2,
|
|
|
|
|
IPFrom: IP2Long("192.168.1.102"),
|
|
|
|
|
IPTo: IP2Long("192.168.1.103"),
|
|
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
logs.PrintAsJSON(list.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(list.ipMap, t)
|
|
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
t.Log("===AFTER GC===")
|
|
|
|
|
logs.PrintAsJSON(list.itemsMap, t)
|
|
|
|
|
logs.PrintAsJSON(list.ipMap, t)
|
2020-11-09 10:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkIPList_Contains(b *testing.B) {
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
|
|
|
|
|
list := NewIPList()
|
2021-01-03 20:18:47 +08:00
|
|
|
for i := 192; i < 194; i++ {
|
2020-11-09 10:45:44 +08:00
|
|
|
list.Add(&IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: int64(1),
|
|
|
|
|
IPFrom: IP2Long(strconv.Itoa(i) + ".1.0.1"),
|
|
|
|
|
IPTo: IP2Long(strconv.Itoa(i) + ".2.0.1"),
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt: time.Now().Unix() + 60,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-01-03 20:18:47 +08:00
|
|
|
b.Log(len(list.ipMap), "ip")
|
2020-11-09 10:45:44 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = list.Contains(IP2Long("192.168.1.100"))
|
|
|
|
|
}
|
|
|
|
|
}
|