-
Notifications
You must be signed in to change notification settings - Fork 1
/
natzap_test.go
67 lines (61 loc) · 1.52 KB
/
natzap_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
package natzap
import (
"errors"
"github.com/nats-io/nats-server/v2/server"
natstest "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"testing"
)
func newServer() *server.Server {
opts := natstest.DefaultTestOptions
opts.JetStream = true
return natstest.RunServer(&opts)
}
func TestConnection(t *testing.T) {
s := newServer()
defer s.Shutdown()
con, err := nats.Connect(nats.DefaultURL)
if err != nil {
t.Error(err)
}
con.Close()
}
func TestLoggingNoJetStream(t *testing.T) {
s := newServer()
defer s.Shutdown()
con, err := nats.Connect(nats.DefaultURL)
if err != nil {
t.Error(err)
}
encoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig())
core := NewCore(zapcore.WarnLevel, encoder, con).WithSubject("log")
logger := zap.New(core, zap.Development())
logger.Warn("This is a test")
con.Close()
}
func TestLoggingWithJetStream(t *testing.T) {
s := newServer()
defer s.Shutdown()
con, err := nats.Connect(nats.DefaultURL)
if err != nil {
t.Error(err)
}
encoder := zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig())
core, err := NewCore(zapcore.WarnLevel, encoder, con).WithSubject("log").WithJetStream("LOG")
if errors.Is(err, nats.ErrStreamNotFound) {
_, err = core.js.AddStream(&nats.StreamConfig{
Name: "LOG",
Subjects: []string{"log"},
})
if err != nil {
t.Fatal(err)
}
} else if err != nil {
t.Fatal(err)
}
logger := zap.New(core, zap.Development())
logger.Warn("This is a test")
con.Close()
}