forked from savsgio/atreugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener_test.go
132 lines (123 loc) · 2.17 KB
/
listener_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package atreugo
import (
"testing"
)
func TestAtreugo_getListener(t *testing.T) {
type args struct {
addr string
network string
reuseport bool
}
type want struct {
addr string
network string
err bool
}
tests := []struct {
name string
args args
want want
}{
{
name: "Ok",
args: args{
addr: "127.0.0.1:8000",
},
want: want{
addr: "127.0.0.1:8000",
network: "tcp",
err: false,
},
},
{
name: "Reuseport",
args: args{
addr: "127.0.0.1:8000",
network: "tcp4",
reuseport: true,
},
want: want{
addr: "127.0.0.1:8000",
network: "tcp",
err: false,
},
},
{
name: "Unix",
args: args{
addr: "/tmp/test.sock",
network: "unix",
},
want: want{
addr: "/tmp/test.sock",
network: "unix",
err: false,
},
},
// {
// name: "UnixRemoveError",
// args: args{
// addr: "/tmp/data.sock",
// network: "unix",
// },
// want: want{
// addr: "/tmp/data.sock",
// network: "unix",
// err: true,
// },
// },
{
name: "UnixChmodError",
args: args{
addr: "345&%·%&%&/%&(",
network: "unix",
},
want: want{
err: true,
},
},
{
name: "Error",
args: args{
network: "fake",
},
want: want{
err: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
Addr: tt.args.addr,
LogLevel: "fatal",
Reuseport: tt.args.reuseport,
}
if tt.args.network != "" {
cfg.Network = tt.args.network
}
defer func() {
r := recover()
if tt.want.err && r == nil {
t.Errorf("Panic expected")
} else if !tt.want.err && r != nil {
t.Errorf("Unexpected panic")
}
}()
s := New(cfg)
ln, err := s.getListener()
if err != nil {
panic(err)
}
lnAddress := ln.Addr().String()
if lnAddress != tt.want.addr {
t.Errorf("Listener address: '%s', want '%s'", lnAddress, tt.want.addr)
}
lnNetwork := ln.Addr().Network()
if lnNetwork != tt.want.network {
t.Errorf("Listener network: '%s', want '%s'", lnNetwork, tt.want.network)
}
ln.Close()
})
}
}