forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modetest.go
200 lines (175 loc) · 4.38 KB
/
modetest.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
package modetest
import (
"testing"
"time"
"github.com/elastic/beats/libbeat/common/op"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/mode"
"github.com/stretchr/testify/assert"
)
type MockClient struct {
Connected bool
CBPublish func([]outputs.Data) ([]outputs.Data, error)
CBAsyncPublish func(func([]outputs.Data, error), []outputs.Data) error
CBClose func() error
CBConnect func(time.Duration) error
}
func NewMockClient(template *MockClient) *MockClient {
mc := &MockClient{
Connected: true,
CBConnect: ConnectOK,
CBClose: CloseOK,
CBPublish: PublishIgnore,
CBAsyncPublish: AsyncPublishIgnore,
}
if template != nil {
mc.Connected = template.Connected
if template.CBPublish != nil {
mc.CBPublish = template.CBPublish
}
if template.CBAsyncPublish != nil {
mc.CBAsyncPublish = template.CBAsyncPublish
}
if template.CBClose != nil {
mc.CBClose = template.CBClose
}
if template.CBConnect != nil {
mc.CBConnect = template.CBConnect
}
}
return mc
}
func (c *MockClient) Connect(timeout time.Duration) error {
err := c.CBConnect(timeout)
c.Connected = err == nil
return err
}
func (c *MockClient) Close() error {
err := c.CBClose()
c.Connected = false
return err
}
func (c *MockClient) PublishEvents(data []outputs.Data) ([]outputs.Data, error) {
return c.CBPublish(data)
}
func (c *MockClient) PublishEvent(data outputs.Data) error {
_, err := c.PublishEvents([]outputs.Data{data})
return err
}
func (c *MockClient) AsyncPublishEvents(cb func([]outputs.Data, error), data []outputs.Data) error {
return c.CBAsyncPublish(cb, data)
}
func (c *MockClient) AsyncPublishEvent(cb func(error), data outputs.Data) error {
return c.AsyncPublishEvents(
func(evts []outputs.Data, err error) { cb(err) },
[]outputs.Data{data})
}
func SyncClients(n int, tmpl *MockClient) []mode.ProtocolClient {
cl := make([]mode.ProtocolClient, n)
for i := 0; i < n; i++ {
cl[i] = NewMockClient(tmpl)
}
return cl
}
func AsyncClients(n int, tmpl *MockClient) []mode.AsyncProtocolClient {
cl := make([]mode.AsyncProtocolClient, n)
for i := 0; i < n; i++ {
cl[i] = NewMockClient(tmpl)
}
return cl
}
func TestMode(
t *testing.T,
mode mode.ConnectionMode,
opts outputs.Options,
data []EventInfo,
expectedSignals []bool,
collected *[][]outputs.Data,
) {
defer func() {
err := mode.Close()
if err != nil {
t.Fatal(err)
}
}()
if data == nil {
return
}
results, expectedData := PublishWith(t, mode, opts, data, expectedSignals)
assert.Equal(t, expectedSignals, results)
if collected != nil {
assert.Equal(t, len(expectedData), len(*collected))
if len(expectedData) == len(*collected) {
for i := range *collected {
expected := expectedData[i]
actual := (*collected)[i]
assert.Equal(t, expected, actual)
}
}
}
}
func PublishWith(
t *testing.T,
mode mode.ConnectionMode,
opts outputs.Options,
data []EventInfo,
expectedSignals []bool,
) ([]bool, [][]outputs.Data) {
return doPublishWith(t, mode, opts, data, func(i int) bool {
return expectedSignals[i]
})
}
func PublishAllWith(
t *testing.T,
mode mode.ConnectionMode,
data []EventInfo,
) ([]bool, [][]outputs.Data) {
opts := outputs.Options{Guaranteed: true}
expectSignal := func(_ int) bool { return true }
return doPublishWith(t, mode, opts, data, expectSignal)
}
func doPublishWith(
t *testing.T,
mode mode.ConnectionMode,
opts outputs.Options,
data []EventInfo,
expectedSignals func(int) bool,
) ([]bool, [][]outputs.Data) {
if data == nil {
return nil, nil
}
numSignals := 0
for _, pubEvents := range data {
if pubEvents.Single {
numSignals += len(pubEvents.Data)
} else {
numSignals++
}
}
var expectedData [][]outputs.Data
ch := make(chan op.SignalResponse, numSignals)
signal := &op.SignalChannel{ch}
idx := 0
for _, pubEvents := range data {
if pubEvents.Single {
for _, event := range pubEvents.Data {
_ = mode.PublishEvent(signal, opts, event)
if expectedSignals(idx) {
expectedData = append(expectedData, []outputs.Data{event})
}
idx++
}
} else {
_ = mode.PublishEvents(signal, opts, pubEvents.Data)
if expectedSignals(idx) {
expectedData = append(expectedData, pubEvents.Data)
}
idx++
}
}
var signals []bool
for i := 0; i < idx; i++ {
signals = append(signals, <-ch == op.SignalCompleted)
}
return signals, expectedData
}