feat(arp): 自 go-hua 迁入 ARP 表查询与定时刷新缓存

- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp
- 附测试、examples/arp 例程与包 README
This commit is contained in:
2026-09-20 12:29:36 +08:00
parent 8a038d4df3
commit a4920a6a8e
10 changed files with 438 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
package arp
import (
"fmt"
"testing"
"time"
)
var (
_ = fmt.Println
)
func TestTable(t *testing.T) {
table := Table()
if table == nil {
t.Errorf("Empty table")
}
}
func TestCacheInfo(t *testing.T) {
prevUpdated := CacheLastUpdate().UnixNano()
prevCount := CacheUpdateCount()
CacheUpdate()
if prevUpdated == CacheLastUpdate().UnixNano() {
t.Error()
}
if prevCount == CacheUpdateCount() {
t.Error()
}
}
func TestAutoRefresh(t *testing.T) {
if testing.Short() {
t.Skip("Skipping autorefresh test")
}
prevUpdated := CacheLastUpdate().UnixNano()
prevCount := CacheUpdateCount()
AutoRefresh(100 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
StopAutoRefresh()
if prevUpdated == CacheLastUpdate().UnixNano() {
t.Error()
}
if prevCount == CacheUpdateCount() {
t.Error()
}
// test to make sure stop worked
prevUpdated = CacheLastUpdate().UnixNano()
prevCount = CacheUpdateCount()
time.Sleep(200 * time.Millisecond)
if prevUpdated != CacheLastUpdate().UnixNano() {
t.Error()
}
if prevCount != CacheUpdateCount() {
t.Error()
}
}
func TestSearch(t *testing.T) {
table := Table()
for ip, test := range table {
result := Search(ip)
if test != result {
t.Errorf("expected %s got %s", test, result)
}
}
}
func BenchmarkSearch(b *testing.B) {
table := Table()
if len(table) == 0 {
return
}
for ip, _ := range Table() {
for i := 0; i < b.N; i++ {
Search(ip)
}
// using the first key is enough
break
}
}