mirror of
https://github.com/taigrr/go-fastping
synced 2025-01-18 05:03:15 -08:00
adding RemoveIP and RemoveIPAddr funcs
This commit is contained in:
parent
41b7495ae3
commit
42027cc47f
21
fastping.go
21
fastping.go
@ -213,6 +213,27 @@ func (p *Pinger) AddIPAddr(ip *net.IPAddr) {
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
// RemoveIP removes an IP address from Pinger. ipaddr arg should be a string
|
||||
// like "192.0.2.1".
|
||||
func (p *Pinger) RemoveIP(ipaddr string) error {
|
||||
addr := net.ParseIP(ipaddr)
|
||||
if addr == nil {
|
||||
return fmt.Errorf("%s is not a valid textual representation of an IP address", ipaddr)
|
||||
}
|
||||
p.mu.Lock()
|
||||
delete(p.addrs, addr.String())
|
||||
p.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveIPAddr removes an IP address from Pinger. ip arg should be a net.IPAddr
|
||||
// pointer.
|
||||
func (p *Pinger) RemoveIPAddr(ip *net.IPAddr) {
|
||||
p.mu.Lock()
|
||||
delete(p.addrs, ip.String())
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
// AddHandler adds event handler to Pinger. event arg should be "receive" or
|
||||
// "idle" string.
|
||||
//
|
||||
|
@ -37,6 +37,37 @@ func TestAddIP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveIP(t *testing.T) {
|
||||
p := NewPinger()
|
||||
|
||||
if err := p.AddIP("127.0.0.1"); err != nil {
|
||||
t.Fatalf("AddIP failed: %v", err)
|
||||
}
|
||||
|
||||
if err := p.RemoveIP("127.0.0.1"); err != nil {
|
||||
t.Fatalf("RemoveIP failed: %v", err)
|
||||
}
|
||||
|
||||
length := len(p.addrs)
|
||||
if length != 0 {
|
||||
t.Fatalf("RemoveIP failed: got %v, expected %v", length, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveIPAddr(t *testing.T) {
|
||||
p := NewPinger()
|
||||
|
||||
if err := p.AddIP("127.0.0.1"); err != nil {
|
||||
t.Fatalf("AddIP failed: %v", err)
|
||||
}
|
||||
|
||||
p.RemoveIPAddr(&net.IPAddr{IP: net.IPv4(127, 0, 0, 1)})
|
||||
length := len(p.addrs)
|
||||
if length != 0 {
|
||||
t.Fatalf("RemoveIP failed: got %v, expected %v", length, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
for _, network := range []string{"ip", "udp"} {
|
||||
p := NewPinger()
|
||||
|
Loading…
x
Reference in New Issue
Block a user