forked from dropbox/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portgroup_test.go
112 lines (102 loc) · 2.39 KB
/
portgroup_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
100
101
102
103
104
105
106
107
108
109
110
111
112
package llama
import (
"net"
"testing"
)
var stopChan = make(chan bool)
var cbChan = make(chan *Probe)
var sendChan = make(chan *net.UDPAddr)
func TestNewPortGroup(t *testing.T) {
// Create a new one
pg := NewPortGroup(stopChan, cbChan, sendChan)
if pg == nil {
t.Error("New didn't return a PortGroup")
}
// Type checking is handled by building
}
func TestAdd(t *testing.T) {
pg := NewPortGroup(stopChan, cbChan, sendChan)
// Create the port and chan
p := Port{}
c := make(chan *net.UDPAddr)
// Add them
pg.Add(&p, c)
// Make sure they're there
if pg.ports[&p] != c {
t.Error("Port and channel not correctly added")
}
}
func TestAddNew(t *testing.T) {
pg := NewPortGroup(stopChan, cbChan, sendChan)
// Add a new port
p, c := pg.AddNew(DefaultAddrStr, DefaultTos,
DefaultCacheTimeout, DefaultCacheCleanRate,
DefaultReadTimeout)
// Make sure it's in the PortGroup
if pg.ports[p] != c {
t.Error("New port/channel was not added correctly")
}
}
func TestDel(t *testing.T) {
pg := NewPortGroup(stopChan, cbChan, sendChan)
// Create the port and chan
p := Port{}
c := make(chan *net.UDPAddr)
// Add them
pg.Add(&p, c)
// Delete them
pg.Del(&p)
// Make sure it's gone
if pg.ports[&p] != nil {
t.Error("Port still exists after deletion")
}
}
func TestPortGroupRun(t *testing.T) {
// TODO(dmar): This needs more complex mocking
}
func TestMux(t *testing.T) {
pg := NewPortGroup(stopChan, cbChan, sendChan)
// Create the port and chan
p1 := Port{}
p2 := Port{}
// If these aren't buffered, they'll just fail later
c1 := make(chan *net.UDPAddr, 10)
c2 := make(chan *net.UDPAddr, 10)
// Add them
pg.Add(&p1, c1)
pg.Add(&p2, c2)
// Send an addr
addr, err := net.ResolveUDPAddr("udp", DefaultAddrStr)
if err != nil {
t.Error("Hit error resolving addr")
}
// And forcibly mux it
pg.mux(addr)
// Make sure we get both
select {
case addr1 := <-c1:
if addr1 != addr {
t.Error("Muxed value doesn't match what was passed in")
}
default:
t.Error("Passed in value wasn't muxed")
}
select {
case addr2 := <-c2:
if addr2 != addr {
t.Error("Muxed value doesn't match what was passed in")
}
default:
t.Error("Passed in value wasn't muxed")
}
}
func TestPortGroupStop(t *testing.T) {
pg := NewPortGroup(stopChan, cbChan, sendChan)
pg.Stop()
// Make sure stop actually closes it
select {
case <-stopChan:
default:
t.Error("Channel wasn't closed after calling Stop")
}
}