Skip to content

Commit a40f791

Browse files
lvan100lianghuan
authored andcommitted
test(log): add unit testing
1 parent 77af607 commit a40f791

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

gs/internal/gs_conf/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ func (p *PropertySources) getFiles(dir string, resolver conf.Properties) (_ []st
285285
files = append(files, []string{
286286
fmt.Sprintf("%s/%s-%s.properties", dir, p.configName, s),
287287
fmt.Sprintf("%s/%s-%s.yaml", dir, p.configName, s),
288+
fmt.Sprintf("%s/%s-%s.yml", dir, p.configName, s),
288289
fmt.Sprintf("%s/%s-%s.toml", dir, p.configName, s),
290+
fmt.Sprintf("%s/%s-%s.tml", dir, p.configName, s),
289291
fmt.Sprintf("%s/%s-%s.json", dir, p.configName, s),
290292
}...)
291293
}

log/log_reader.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,31 @@ func (r *XMLReader) Read(b []byte) (*Node, error) {
104104
}
105105
switch t := token.(type) {
106106
case xml.StartElement:
107-
curr := &Node{
108-
Label: t.Name.Local,
109-
Attributes: make(map[string]string),
107+
if t.Name.Local == "Property" {
108+
var name string
109+
for _, attr := range t.Attr {
110+
if attr.Name.Local == "name" {
111+
name = attr.Value
112+
break
113+
}
114+
}
115+
var value string
116+
err = d.DecodeElement(&value, &t)
117+
if err != nil {
118+
return nil, err
119+
}
120+
curr := stack[len(stack)-1]
121+
curr.Attributes[name] = value
122+
} else {
123+
curr := &Node{
124+
Label: t.Name.Local,
125+
Attributes: make(map[string]string),
126+
}
127+
for _, attr := range t.Attr {
128+
curr.Attributes[attr.Name.Local] = attr.Value
129+
}
130+
stack = append(stack, curr)
110131
}
111-
for _, attr := range t.Attr {
112-
curr.Attributes[attr.Name.Local] = attr.Value
113-
}
114-
stack = append(stack, curr)
115132
case xml.EndElement:
116133
curr := stack[len(stack)-1]
117134
parent := stack[len(stack)-2]

log/log_refresh.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path/filepath"
2525
"regexp"
26+
"strconv"
2627
"strings"
2728
"sync/atomic"
2829

@@ -163,6 +164,24 @@ func RefreshReader(input io.Reader, ext string) error {
163164
return errors.New("found no root logger")
164165
}
165166

167+
var (
168+
cfgMaxBufferSize int
169+
)
170+
171+
if node := rootNode.getChild("Properties"); node != nil {
172+
strMaxBufferSize, ok := node.Attributes["MaxBufferSize"]
173+
if ok {
174+
i, err := strconv.Atoi(strMaxBufferSize)
175+
if err != nil {
176+
return err
177+
}
178+
if i <= 0 {
179+
return errors.New("RefreshReader: MaxBufferSize must be greater than 0")
180+
}
181+
cfgMaxBufferSize = i
182+
}
183+
}
184+
166185
var (
167186
logArray []*Logger
168187
tagArray []*regexp.Regexp
@@ -199,5 +218,7 @@ func RefreshReader(input io.Reader, ext string) error {
199218
tag.SetLogger(logger)
200219
}
201220

221+
maxBufferSize.Store(int32(cfgMaxBufferSize))
222+
202223
return nil
203224
}

log/log_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323

2424
"github.com/go-spring/spring-core/log"
25+
"github.com/lvan100/go-assert"
2526
)
2627

2728
var TagRequestIn = log.GetTag("_com_request_in")
@@ -30,15 +31,13 @@ var TagRequestOut = log.GetTag("_com_request_out")
3031
func TestLog(t *testing.T) {
3132
ctx := t.Context()
3233

34+
log.StringFromContext = func(ctx context.Context) string {
35+
return ""
36+
}
37+
3338
log.FieldsFromContext = func(ctx context.Context) []log.Field {
3439
traceID, _ := ctx.Value("trace_id").(string)
35-
if traceID == "" {
36-
traceID = "<<trace_id>>"
37-
}
3840
spanID, _ := ctx.Value("span_id").(string)
39-
if spanID == "" {
40-
spanID = "<<span_id>>"
41-
}
4241
return []log.Field{
4342
log.String("trace_id", traceID),
4443
log.String("span_id", spanID),
@@ -57,6 +56,9 @@ func TestLog(t *testing.T) {
5756
xml := `
5857
<?xml version="1.0" encoding="UTF-8"?>
5958
<Configuration>
59+
<Properties>
60+
<Property name="MaxBufferSize">1048576</Property>
61+
</Properties>
6062
<Appenders>
6163
<Console name="Console_JSON">
6264
<JSONLayout/>
@@ -76,9 +78,7 @@ func TestLog(t *testing.T) {
7678
</Configuration>
7779
`
7880
err := log.RefreshReader(strings.NewReader(xml), ".xml")
79-
if err != nil {
80-
t.Fatal(err)
81-
}
81+
assert.Nil(t, err)
8282

8383
ctx = context.WithValue(ctx, "trace_id", "0a882193682db71edd48044db54cae88")
8484
ctx = context.WithValue(ctx, "span_id", "50ef0724418c0a66")

log/plugin_layout.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ import (
2121
"strconv"
2222
"strings"
2323
"sync"
24+
"sync/atomic"
2425
)
2526

2627
func init() {
2728
RegisterPlugin[TextLayout]("TextLayout", PluginTypeLayout)
2829
RegisterPlugin[JSONLayout]("JSONLayout", PluginTypeLayout)
2930
}
3031

32+
// maxBufferSize is the maximum size of a buffer in the pool.
33+
var maxBufferSize atomic.Int32
34+
3135
var bufferPool = sync.Pool{
3236
New: func() interface{} {
3337
return bytes.NewBuffer(nil)
@@ -41,10 +45,11 @@ func GetBuffer() *bytes.Buffer {
4145

4246
// PutBuffer returns a buffer to the pool after resetting it.
4347
func PutBuffer(buf *bytes.Buffer) {
44-
buf.Reset()
45-
if buf.Cap() > 1024*1024 { // 1MB
48+
size := int(maxBufferSize.Load())
49+
if size == 0 || buf.Cap() > size {
4650
return
4751
}
52+
buf.Reset()
4853
bufferPool.Put(buf)
4954
}
5055

0 commit comments

Comments
 (0)