feat(flow): 自 go-hua 迁入流量统计器
- 并发安全收发字节数累计(Add/Get/Reset), 附测试、例程与 README
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package flow
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFlowAddGet(t *testing.T) {
|
||||
f := &Flow{}
|
||||
f.Add(100, 200)
|
||||
f.Add(50, 80)
|
||||
|
||||
send, recv := f.Get()
|
||||
if send != 150 {
|
||||
t.Errorf("SendFlow = %d, want 150", send)
|
||||
}
|
||||
if recv != 280 {
|
||||
t.Errorf("RecvFlow = %d, want 280", recv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlowReset(t *testing.T) {
|
||||
f := &Flow{}
|
||||
f.Add(10, 20)
|
||||
|
||||
send, recv := f.Reset()
|
||||
if send != 10 || recv != 20 {
|
||||
t.Errorf("Reset 返回 = (%d, %d), want (10, 20)", send, recv)
|
||||
}
|
||||
|
||||
send, recv = f.Get()
|
||||
if send != 0 || recv != 0 {
|
||||
t.Errorf("Reset 后流量 = (%d, %d), want (0, 0)", send, recv)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user