-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathannounce_test.go
99 lines (92 loc) · 2.62 KB
/
announce_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ssdp
import (
"net"
"strings"
"sync"
"testing"
"time"
)
func TestAnnounceAlive(t *testing.T) {
var mu sync.Mutex
var mm []*AliveMessage
m := newTestMonitor("test:announce+alive", func(m *AliveMessage) {
mu.Lock()
mm = append(mm, m)
mu.Unlock()
}, nil, nil)
err := m.Start()
if err != nil {
t.Fatalf("failed to start Monitor: %s", err)
}
defer m.Close()
err = AnnounceAlive("test:announce+alive", "usn:announce+alive", "location:announce+alive", "server:announce+alive", 600, "")
if err != nil {
t.Fatalf("failed to announce alive: %s", err)
}
time.Sleep(500 * time.Millisecond)
if len(mm) < 1 {
t.Fatal("no alives detected")
}
//t.Logf("found %d alives", len(mm))
_, port, err := net.SplitHostPort(mm[0].From.String())
if err != nil {
t.Fatalf("failed to split host and port: %s", err)
}
port = ":" + port
for i, m := range mm {
if strings.HasSuffix(port, m.From.String()) {
t.Errorf("unmatch port#%d:\nwant=%q\n got=%q", i, port, m.From.String())
}
if m.Type != "test:announce+alive" {
t.Errorf("unexpected alive#%d type: want=%q got=%q", i, "test:announce+alive", m.Type)
}
if m.USN != "usn:announce+alive" {
t.Errorf("unexpected alive#%d usn: want=%q got=%q", i, "usn:announce+alive", m.USN)
}
if m.Location != "location:announce+alive" {
t.Errorf("unexpected alive#%d location: want=%q got=%q", i, "location:announce+alive", m.Location)
}
if m.Server != "server:announce+alive" {
t.Errorf("unexpected alive#%d server: want=%q got=%q", i, "server:announce+alive", m.Server)
}
}
}
func TestAnnounceBye(t *testing.T) {
var mu sync.Mutex
var mm []*ByeMessage
m := newTestMonitor("test:announce+bye", nil, func(m *ByeMessage) {
mu.Lock()
mm = append(mm, m)
mu.Unlock()
}, nil)
err := m.Start()
if err != nil {
t.Fatalf("failed to start Monitor: %s", err)
}
defer m.Close()
err = AnnounceBye("test:announce+bye", "usn:announce+bye", "")
if err != nil {
t.Fatalf("failed to announce bye: %s", err)
}
time.Sleep(500 * time.Millisecond)
if len(mm) < 1 {
t.Fatal("no byes detected")
}
//t.Logf("found %d byes", len(mm))
_, port, err := net.SplitHostPort(mm[0].From.String())
if err != nil {
t.Fatalf("failed to split host and port: %s", err)
}
port = ":" + port
for i, m := range mm {
if strings.HasSuffix(port, m.From.String()) {
t.Errorf("unmatch port#%d:\nwant=%q\n got=%q", i, port, m.From.String())
}
if m.Type != "test:announce+bye" {
t.Errorf("unexpected bye#%d type: want=%q got=%q", i, "test:announce+bye", m.Type)
}
if m.USN != "usn:announce+bye" {
t.Errorf("unexpected bye#%d usn: want=%q got=%q", i, "usn:announce+bye", m.USN)
}
}
}