-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
58 lines (51 loc) · 1.46 KB
/
server_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
package main
import (
"sort"
"strings"
"testing"
"github.com/markdingo/autoreverse/log"
"github.com/markdingo/autoreverse/mock"
)
func TestStartServersGood(t *testing.T) {
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.MajorLevel)
ar := newAutoReverse(nil, nil)
ar.cfg.listen = []string{"127.0.0.1:2056", "127.0.0.1:2057", "[::1]:2058", "[::1]:2059"}
ar.startServers()
ar.stopServers()
exp := `Listen on: udp 127.0.0.1:2056
Listen on: udp 127.0.0.1:2057
Listen on: udp [::1]:2058
Listen on: udp [::1]:2059
Listen on: tcp 127.0.0.1:2056
Listen on: tcp 127.0.0.1:2057
Listen on: tcp [::1]:2058
Listen on: tcp [::1]:2059
`
got := out.String()
// The server start up order is effectively random as each listen interface is run
// as a separate go-routine so sort the log lines to eliminate order issues when
// comparing against expected.
gar := strings.Split(got, "\n")
ear := strings.Split(exp, "\n")
sort.Strings(gar)
sort.Strings(ear)
nGot := strings.Join(gar, "\n")
nExp := strings.Join(ear, "\n")
if nGot != nExp {
t.Error("Log mismatch. Exp", nExp, "\nGot", nGot)
}
}
// Exercise the error path of starting a server
func TestStartServersBad(t *testing.T) {
out := &mock.IOWriter{}
log.SetOut(out)
log.SetLevel(log.MajorLevel)
ar := newAutoReverse(nil, nil)
srv := newServer(ar.cfg, ar.dbGetter, ar.resolver, nil, "udp", "127.0.0.1:xx")
err := ar.startServer(srv)
if err == nil {
t.Error("Expected server to fail due to bogus port number")
}
}