generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
100 lines (81 loc) · 1.86 KB
/
logger_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
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
package main
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/suite"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
type testLogger struct {
t *testing.T
suiteName string
testName string
}
func (tl testLogger) Printf(format string, args ...interface{}) {
tl.t.Logf(
"[%s] [%s] "+format+"\n",
append(
[]interface{}{tl.suiteName, tl.testName},
args...,
)...,
)
}
type LoggerSuite struct {
suite.Suite
capture *bytes.Buffer
}
var _ suite.SetupTestSuite = (*LoggerSuite)(nil)
func (suite *LoggerSuite) SetupTest() {
suite.capture = new(bytes.Buffer)
}
func (suite *LoggerSuite) TestWriterLogger() {
wl := WriterLogger{Writer: suite.capture}
wl.Printf("test: %d", 123)
suite.Equal("test: 123\n", suite.capture.String())
}
func (suite *LoggerSuite) TestWriterLoggerError() {
r, w := io.Pipe()
r.Close()
w.Close()
wl := WriterLogger{Writer: w}
suite.Panics(func() {
wl.Printf("test: %d", 123)
})
}
func (suite *LoggerSuite) TestDiscardLogger() {
DiscardLogger{}.Printf("test: %d", 123)
}
func (suite *LoggerSuite) TestProvideLogger() {
var l Logger
app := fxtest.New(
suite.T(),
fx.Logger(DiscardLogger{}),
provideLogger(suite.capture),
fx.Populate(&l),
)
app.RequireStart()
app.RequireStop()
suite.Require().NotNil(l)
l.Printf("test: %d", 123)
suite.Equal("test: 123\n", suite.capture.String())
}
func (suite *LoggerSuite) TestLogServerError() {
suite.Run("ErrServerClosed", func() {
suite.capture.Reset()
l := WriterLogger{Writer: suite.capture}
logServerError(l, http.ErrServerClosed)
suite.Empty(suite.capture.String())
})
suite.Run("UnexpectedError", func() {
suite.capture.Reset()
l := WriterLogger{Writer: suite.capture}
logServerError(l, errors.New("unexpected error"))
suite.NotEmpty(suite.capture.String())
})
}
func TestLogger(t *testing.T) {
suite.Run(t, new(LoggerSuite))
}