summaryrefslogtreecommitdiffstats
path: root/modules/log/buffer_test.go
blob: 8ec74bc78d22df5ae875d28157d94b7070e2916a (plain)
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
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package log

import (
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestBufferLogger(t *testing.T) {
	logger := NewBufferLogger()
	bufferLogger := logger.(*BufferLogger)
	assert.NotNil(t, bufferLogger)

	err := logger.Init("")
	assert.NoError(t, err)

	location, _ := time.LoadLocation("EST")
	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)

	msg := "TEST MSG"
	event := Event{
		level:    INFO,
		msg:      msg,
		caller:   "CALLER",
		filename: "FULL/FILENAME",
		line:     1,
		time:     date,
	}
	logger.LogEvent(&event)
	content, err := bufferLogger.Content()
	assert.NoError(t, err)
	assert.Contains(t, content, msg)
	logger.Close()
}

func TestBufferLoggerContent(t *testing.T) {
	level := INFO
	logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))

	logger.SetLogger("buffer", "buffer", "{}")
	defer logger.DelLogger("buffer")

	msg := "A UNIQUE MESSAGE"
	Error(msg)

	found := false
	for i := 0; i < 30000; i++ {
		content, err := logger.GetLoggerProviderContent("buffer")
		assert.NoError(t, err)
		if strings.Contains(content, msg) {
			found = true
			break
		}
		time.Sleep(1 * time.Millisecond)
	}
	assert.True(t, found)
}