-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_test.go
70 lines (57 loc) · 1.27 KB
/
utilities_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
package mp
import (
"testing"
)
func TestMappedConnectionHandlerFunctionsWithNoMappings(t *testing.T) {
ch := NewMappedConnectionHandler()
ok := false
accept := func() Connection {
ok = true
return nil
}
ch.IncomingConnection("does-not-exist", accept)
if ok {
t.Fatal("???")
}
}
func TestMappedConnectionHandlerRunsInGoroutines(t *testing.T) {
incoming := make(chan int)
outgoing := make(chan int)
ch := NewMappedConnectionHandler()
ch.AddMapping("proto", func(Connection) {
if n := <-incoming; n != 1 {
t.Error("Expected incoming to give us 1, was", n)
}
outgoing <- 2
})
ok := false
accept := func() Connection {
ok = true
return nil
}
ch.IncomingConnection("proto", accept)
if !ok {
t.Fatal("Incoming connection failed")
}
incoming <- 1
if n := <-outgoing; n != 2 {
t.Error("Expected 2 from outgoing, was", n)
}
}
func TestMappedConnectionHandlerReturnsExpected(t *testing.T) {
ch := NewMappedConnectionHandler()
ch.AddMapping("exists", func(Connection) {})
ok := false
accept := func() Connection {
ok = true
return nil
}
ch.IncomingConnection("DNE", accept)
if ok {
t.Error("Expected IncomingConnection to not exist")
}
ch.IncomingConnection("exists", accept)
if !ok {
t.Error("Expected IncomingConnection to exist")
}
}