-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_test.go
243 lines (180 loc) · 4.48 KB
/
receiver_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package syslogsidecar
import (
"strconv"
"testing"
"time"
"github.com/g41797/kissngoqueue"
"github.com/g41797/sputnik"
"github.com/g41797/sputnik/sidecar"
)
// Satellite has 2 app. blocks:
var blkList []sputnik.BlockDescriptor = []sputnik.BlockDescriptor{
// events Producer
producerDescriptor(),
// syslog Receiver
receiverDescriptor(),
}
type recvTest struct {
// Config path & factory
confFolderPath string
cfact sputnik.ConfFactory
syslogconf SyslogConfiguration
// Test queue
q *kissngoqueue.Queue[sputnik.Msg]
// Launcher
launch sputnik.Launch
// ShootDown
kill sputnik.ShootDown
// Signalling channel
done chan struct{}
// ServerConnector
conntr sputnik.DummyConnector
to time.Duration
client *client
}
func newRecvTest() *recvTest {
res := new(recvTest)
res.confFolderPath = "./conf/"
res.cfact = sidecar.ConfigFactory(res.confFolderPath)
res.q = kissngoqueue.NewQueue[sputnik.Msg]()
res.conntr = sputnik.DummyConnector{}
res.to = time.Millisecond * 100
return res
}
// Registration of factories for test environment
// For this case init() isn't used
// use this pattern for the case when you don't need
// dynamic registration: all blocks (and factories) are
// known in advance.
func (rt *recvTest) factories() sputnik.BlockFactories {
res := make(sputnik.BlockFactories)
finfct, _ := sputnik.Factory(sputnik.DefaultFinisherName)
confct, _ := sputnik.Factory(sputnik.DefaultConnectorName)
RegisterMessageProducerFactory(func() sidecar.MessageProducer { return newMMP(rt.q) })
factList := []struct {
name string
fact sputnik.BlockFactory
}{
{sputnik.DefaultFinisherName, finfct},
{sputnik.DefaultConnectorName, confct},
{ProducerName, producerBlockFactory},
{ReceiverName, receiverBlockFactory},
}
for _, fd := range factList {
sputnik.RegisterBlockFactoryInner(fd.name, fd.fact, res)
}
return res
}
func recvSputnik(rt *recvTest) sputnik.Sputnik {
sp, _ := sputnik.NewSputnik(
sputnik.WithConfFactory(rt.cfact),
sputnik.WithAppBlocks(blkList),
sputnik.WithBlockFactories(rt.factories()),
sputnik.WithConnector(&rt.conntr, rt.to),
)
return *sp
}
// Run Launcher on dedicated goroutine
// Test controls execution via sputnik API
// Results received using queue
func (rt *recvTest) run(t *testing.T) {
if err := rt.cfact(ReceiverName, &rt.syslogconf); err != nil {
t.Fatalf("failed get configuration")
}
rt.client = newClientWIthConfig(rt.syslogconf)
rt.done = make(chan struct{})
if rt.launch == nil {
t.Fatalf("nil rt.launch")
}
go func(l sputnik.Launch, done chan struct{}, client *client) {
if l == nil {
panic("nil launcher")
}
l()
if client != nil {
client.finish()
}
close(done)
}(rt.launch, rt.done, rt.client)
return
}
func (rt *recvTest) exchange(t *testing.T) {
next := next()
logText := strconv.Itoa(next)
err := rt.client.log(logText)
if err != nil {
t.Errorf("send log message error %v", err)
}
msg, ok := rt.q.Get()
if !ok {
t.Errorf("failed receive from test queue")
}
msgparts, err := UnpackToMap(msg)
Put(msg)
if err != nil {
t.Errorf("unpack message error %v", err)
}
recvlog, _ := msgparts["message"]
if logText != recvlog {
t.Errorf("Expected %s Received %s", logText, recvlog)
}
}
func TestReceive_Prepare(t *testing.T) {
rt := newRecvTest()
sputnik := recvSputnik(rt)
_, kill, err := sputnik.Prepare()
if err != nil {
t.Errorf("Prepare error %v", err)
}
time.Sleep(time.Second)
kill()
return
}
func TestReceive_StartStop(t *testing.T) {
rt := newRecvTest()
sputnik := recvSputnik(rt)
launch, kill, err := sputnik.Prepare()
if err != nil {
t.Fatalf("prepare error %v", err)
}
if launch == nil {
t.Fatalf("nil launch")
}
rt.launch = launch
rt.kill = kill
rt.run(t)
time.Sleep(time.Second)
rt.conntr.SetState(true)
if err = rt.client.init(); err != nil {
t.Fatalf("failure of syslogd client %v", err)
}
time.Sleep(time.Millisecond * 100)
rt.kill()
return
}
func TestReceive_Exchange(t *testing.T) {
rt := newRecvTest()
sputnik := recvSputnik(rt)
launch, kill, err := sputnik.Prepare()
if err != nil {
t.Fatalf("prepare error %v", err)
}
if launch == nil {
t.Fatalf("nil launch")
}
rt.launch = launch
rt.kill = kill
rt.run(t)
time.Sleep(time.Second)
rt.conntr.SetState(true)
if err = rt.client.init(); err != nil {
t.Fatalf("failure of syslogd client %v", err)
}
time.Sleep(time.Millisecond * 100)
for i := 0; i < 50000; i++ {
rt.exchange(t)
}
time.Sleep(time.Millisecond * 100)
rt.kill()
return
}