summaryrefslogtreecommitdiffstats
path: root/modules/test/logchecker_test.go
blob: 0f410fed1242cfd45dcf2807ec97095f91ed2f31 (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
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package test

import (
	"testing"
	"time"

	"code.gitea.io/gitea/modules/log"

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

func TestLogCheckerInfo(t *testing.T) {
	lc, cleanup := NewLogChecker(log.DEFAULT, log.INFO)
	defer cleanup()

	lc.Filter("First", "Third").StopMark("End")
	log.Info("test")

	filtered, stopped := lc.Check(100 * time.Millisecond)
	assert.ElementsMatch(t, []bool{false, false}, filtered)
	assert.False(t, stopped)

	log.Info("First")
	log.Debug("Third")
	filtered, stopped = lc.Check(100 * time.Millisecond)
	assert.ElementsMatch(t, []bool{true, false}, filtered)
	assert.False(t, stopped)

	log.Info("Second")
	log.Debug("Third")
	filtered, stopped = lc.Check(100 * time.Millisecond)
	assert.ElementsMatch(t, []bool{true, false}, filtered)
	assert.False(t, stopped)

	log.Info("Third")
	filtered, stopped = lc.Check(100 * time.Millisecond)
	assert.ElementsMatch(t, []bool{true, true}, filtered)
	assert.False(t, stopped)

	log.Info("End")
	filtered, stopped = lc.Check(100 * time.Millisecond)
	assert.ElementsMatch(t, []bool{true, true}, filtered)
	assert.True(t, stopped)
}

func TestLogCheckerDebug(t *testing.T) {
	lc, cleanup := NewLogChecker(log.DEFAULT, log.DEBUG)
	defer cleanup()

	lc.StopMark("End")

	log.Debug("End")
	_, stopped := lc.Check(100 * time.Millisecond)
	assert.True(t, stopped)
}