-
Notifications
You must be signed in to change notification settings - Fork 106
/
outputsocket_test.go
63 lines (54 loc) · 1.43 KB
/
outputsocket_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
package outputsocket
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistOutputHandler(ModuleName, InitHandler)
}
func Test_output_report_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
pc, err := net.ListenPacket("udp", ":9876")
require.NoError(err)
defer pc.Close()
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
output:
- type: socket
socket: udp
address: localhost:9876
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
var messages []logevent.LogEvent
var expected []string
for _, m := range []string{"one", "two", "three", "four", "five", "six", "seven"} {
msg := logevent.LogEvent{Message: m}
messages = append(messages, msg)
expected = append(expected, fmt.Sprintf(`"message":"%s"`, m))
}
buf := make([]byte, 1024)
for i, m := range messages {
conf.TestInputEvent(m)
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
n, _, err := pc.ReadFrom(buf)
require.NoError(err)
require.Contains(string(buf[:n]), expected[i])
}
}