forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modetest.go
167 lines (146 loc) · 3.79 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
package modetest
import (
"testing"
"time"
"github.com/elastic/beats/libbeat/common"
"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([]common.MapStr) ([]common.MapStr, error)
CBAsyncPublish func(func([]common.MapStr, error), []common.MapStr) 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) IsConnected() bool {
return c.Connected
}
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(events []common.MapStr) ([]common.MapStr, error) {
return c.CBPublish(events)
}
func (c *MockClient) PublishEvent(event common.MapStr) error {
_, err := c.PublishEvents([]common.MapStr{event})
return err
}
func (c *MockClient) AsyncPublishEvents(cb func([]common.MapStr, error), events []common.MapStr) error {
return c.CBAsyncPublish(cb, events)
}
func (c *MockClient) AsyncPublishEvent(cb func(error), event common.MapStr) error {
return c.AsyncPublishEvents(
func(evts []common.MapStr, err error) { cb(err) },
[]common.MapStr{event})
}
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,
events []EventInfo,
expectedSignals []bool,
collectedEvents *[][]common.MapStr,
) {
defer func() {
err := mode.Close()
if err != nil {
t.Fatal(err)
}
}()
if events == nil {
return
}
numSignals := 0
for _, pubEvents := range events {
if pubEvents.Single {
numSignals += len(pubEvents.Events)
} else {
numSignals++
}
}
var expectedEvents [][]common.MapStr
ch := make(chan op.SignalResponse, numSignals)
signal := &op.SignalChannel{ch}
idx := 0
for _, pubEvents := range events {
if pubEvents.Single {
for _, event := range pubEvents.Events {
_ = mode.PublishEvent(signal, opts, event)
if expectedSignals[idx] {
expectedEvents = append(expectedEvents, []common.MapStr{event})
}
idx++
}
} else {
_ = mode.PublishEvents(signal, opts, pubEvents.Events)
if expectedSignals[idx] {
expectedEvents = append(expectedEvents, pubEvents.Events)
}
idx++
}
}
results := make([]bool, len(expectedSignals))
for i := 0; i < len(expectedSignals); i++ {
results[i] = <-ch == op.SignalCompleted
}
assert.Equal(t, expectedSignals, results)
if collectedEvents != nil {
assert.Equal(t, len(expectedEvents), len(*collectedEvents))
if len(expectedEvents) == len(*collectedEvents) {
for i := range *collectedEvents {
expected := expectedEvents[i]
actual := (*collectedEvents)[i]
assert.Equal(t, expected, actual)
}
}
}
}