Debian Codegraph @

upnp_test.go

raw symbols SCIP
1 package upnp
2
3 import (
4 "context"
5 "sync"
6 "testing"
7 "time"
8 )
9
10 // TestConcurrentUPNP tests that several threads calling Discover() concurrently
11 // succeed.
12 func TestConcurrentUPNP(t *testing.T) {
13 if testing.Short() {
14 t.SkipNow()
15 }
16 // verify that a router exists
17 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
18 defer cancel()
19 _, err := DiscoverCtx(ctx)
20 if err != nil {
21 t.Skip(err)
22 }
23
24 // now try to concurrently Discover() using 20 threads
25 var wg sync.WaitGroup
26 for i := 0; i < 20; i++ {
27 wg.Add(1)
28 go func() {
29 defer wg.Done()
30 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
31 defer cancel()
32 _, err := DiscoverCtx(ctx)
33 if err != nil {
34 t.Error(err)
35 }
36 }()
37 }
38 wg.Wait()
39 }
40
41 func TestIGD(t *testing.T) {
42 // connect to router
43 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
44 defer cancel()
45 d, err := DiscoverCtx(ctx)
46 if err != nil {
47 t.Skip(err)
48 }
49
50 // discover external IP
51 ip, err := d.ExternalIP()
52 if err != nil {
53 t.Fatal(err)
54 }
55 t.Log("Your external IP is:", ip)
56
57 // forward a port
58 err = d.Forward(9001, "upnp test")
59 if err != nil {
60 t.Fatal(err)
61 }
62
63 // un-forward a port
64 err = d.Clear(9001)
65 if err != nil {
66 t.Fatal(err)
67 }
68
69 // record router's location
70 loc := d.Location()
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 // connect to router directly
76 d, err = Load(loc)
77 if err != nil {
78 t.Fatal(err)
79 }
80 }