forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputnats_test.go
73 lines (62 loc) · 1.75 KB
/
inputnats_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
package inputnats
import (
"context"
"strings"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
codecjson "github.com/tsaikd/gogstash/codec/json"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistInputHandler(ModuleName, InitHandler)
config.RegistCodecHandler(codecjson.ModuleName, codecjson.InitHandler)
}
func TestInputNats(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
s, err := server.NewServer(&server.Options{
Host: "127.0.0.1",
Trace: true,
Debug: true,
DisableShortFirstPing: true,
NoLog: true,
NoSigs: true,
})
require.NoError(err)
go s.Start()
defer s.Shutdown()
time.Sleep(500 * time.Millisecond)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
input:
- type: "nats"
host: "` + s.ClientURL() + `"
topic: "test.*"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
// start a publisher client
opts := []nats.Option{nats.Name("test")}
nc, err := nats.Connect(nats.DefaultURL, opts...)
require.NoError(err)
err = nc.Publish("test.1", []byte(`{"foo":"bar"}`))
require.NoError(err)
err = nc.Flush()
require.NoError(err)
// check event
if event, err := conf.TestGetOutputEvent(100 * time.Millisecond); assert.NoError(err) {
require.EqualValues(map[string]interface{}{"foo": "bar"}, event.Extra)
}
}