-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapplication_test.go
150 lines (133 loc) · 3.65 KB
/
application_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package goetty
import (
"os"
"sync"
"testing"
"time"
"github.com/fagongzi/goetty/v3/codec"
"github.com/fagongzi/goetty/v3/codec/simple"
"github.com/lni/goutils/leaktest"
"github.com/stretchr/testify/assert"
)
var (
testAddr = "127.0.0.1:12345"
testUnixSocket = "unix:///tmp/goetty.sock"
testListenAddresses = []string{testAddr, testUnixSocket}
testAddresses = map[string]string{
"tcp": testAddr,
"unix": testUnixSocket,
}
)
func TestStart(t *testing.T) {
defer leaktest.AfterTest(t)()
for name := range testAddresses {
t.Run(name, func(t *testing.T) {
app := newTestApp(t, testListenAddresses, nil)
defer app.Stop()
assert.NoError(t, app.Start())
assert.NoError(t, app.Start())
})
}
}
func TestStop(t *testing.T) {
defer leaktest.AfterTest(t)()
for name, address := range testAddresses {
addr := address
t.Run(name, func(t *testing.T) {
app := newTestApp(t,
testListenAddresses,
func(i IOSession[string, string], a string, u uint64) error {
return i.Write(a, WriteOptions{Flush: true})
}).(*server[string, string])
assert.NoError(t, app.Start())
n := 10
wg := &sync.WaitGroup{}
for i := 0; i < n; i++ {
session := newTestIOSession(t)
err := session.Connect(addr, time.Second)
assert.NoError(t, err)
wg.Add(1)
go func(conn IOSession[string, string]) {
for {
_, err := conn.Read(ReadOptions{})
if err != nil {
wg.Done()
return
}
}
}(session)
}
assert.NoError(t, app.Stop())
wg.Wait()
c := 0
for _, m := range app.sessions {
m.Lock()
c += len(m.sessions)
m.Unlock()
}
assert.Equal(t, 0, c)
})
}
}
func TestStartWithTLS(t *testing.T) {
defer leaktest.AfterTest(t)()
for name := range testAddresses {
t.Run(name, func(t *testing.T) {
app := newTestApp(t,
testListenAddresses,
func(i IOSession[string, string], a string, u uint64) error {
return i.Write(a, WriteOptions{Flush: true})
},
WithAppTLSFromCertAndKey[string, string](
"./etc/server-cert.pem",
"./etc/server-key.pem",
"./etc/ca.pem",
true))
assert.NoError(t, app.Start())
defer func() {
assert.NoError(t, app.Stop())
}()
})
}
}
func newTestApp(t assert.TestingT,
addresses []string,
handleFunc func(IOSession[string, string], string, uint64) error,
opts ...AppOption[string, string]) NetApplication[string, string] {
return newTestAppWithCodec(
t,
addresses,
handleFunc,
simple.NewStringCodec(),
opts...)
}
func newTestAppWithCodec[IN string, OUT string](t assert.TestingT,
addresses []string,
handleFunc func(IOSession[string, string], string, uint64) error,
codec codec.Codec[string, string],
opts ...AppOption[string, string]) NetApplication[string, string] {
if handleFunc == nil {
handleFunc = func(i1 IOSession[string, string], i2 string, u uint64) error {
return nil
}
}
assert.NoError(t, os.RemoveAll(testUnixSocket[7:]))
opts = append(opts, WithAppSessionOptions(WithSessionCodec(codec)))
app, err := NewApplicationWithListenAddress(addresses, handleFunc, opts...)
assert.NoError(t, err)
return app
}
func newTestIOSession(t *testing.T, opts ...Option[string, string]) IOSession[string, string] {
opts = append([]Option[string, string]{WithSessionCodec(simple.NewStringCodec())}, opts...)
return NewIOSession(opts...)
}
func TestParseAddress(t *testing.T) {
network, address, err := parseAddress(testAddr)
assert.NoError(t, err)
assert.Equal(t, "tcp4", network)
assert.Equal(t, testAddr, address)
network, address, err = parseAddress(testUnixSocket)
assert.NoError(t, err)
assert.Equal(t, "unix", network)
assert.Equal(t, "/tmp/goetty.sock", address)
}