Files
go-upnp/upnp_test.go
2016-12-13 21:22:11 -05:00

73 lines
1.1 KiB
Go

package upnp
import (
"sync"
"testing"
)
// TestConcurrentUPNP tests that several threads calling Discover() concurrently
// succeed.
func TestConcurrentUPNP(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// verify that a router exists
_, err := Discover()
if err != nil {
t.Skip(err)
}
// now try to concurrently Discover() using 20 threads
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := Discover()
if err != nil {
t.Error(err)
}
}()
}
wg.Wait()
}
func TestIGD(t *testing.T) {
// connect to router
d, err := Discover()
if err != nil {
t.Skip(err)
}
// discover external IP
ip, err := d.ExternalIP()
if err != nil {
t.Fatal(err)
}
t.Log("Your external IP is:", ip)
// forward a port
err = d.Forward(9001, "upnp test")
if err != nil {
t.Fatal(err)
}
// un-forward a port
err = d.Clear(9001)
if err != nil {
t.Fatal(err)
}
// record router's location
loc := d.Location()
if err != nil {
t.Fatal(err)
}
// connect to router directly
d, err = Load(loc)
if err != nil {
t.Fatal(err)
}
}