-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotracing_test.go
123 lines (102 loc) · 3.01 KB
/
gotracing_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gotracing_test
import (
"fmt"
"testing"
"time"
"github.com/mnaufalhilmym/gotracing"
)
func recursionFunc(x int, fn func(...any), msg ...any) {
x--
if x >= 0 {
recursionFunc(x, fn, msg...)
} else {
fn(msg...)
}
}
func recursionFuncMaxPC(x int, fn func(uint, ...any), maxPC uint, msg ...any) {
x--
if x >= 0 {
recursionFuncMaxPC(x, fn, maxPC, msg...)
} else {
fn(maxPC, msg...)
}
}
type storage struct{}
func (storage) Insert(l gotracing.Level, s gotracing.Traces) {
fmt.Println("insert level", l)
fmt.Println("insert trace", s)
}
func TestError(t *testing.T) {
levelFilterRes := gotracing.NewLevelFilter("error")
if levelFilterRes.IsErr() {
t.Error("levelFilter must be Ok<T>")
return
}
gotracing.SetMinConsolePrintLevel(levelFilterRes.Unwrap())
gotracing.SetMaxProgramCounters(50)
recursionFunc(10, gotracing.Error, "Error", "ERROR")
}
func TestWarn(t *testing.T) {
levelFilterOpt := gotracing.NewLevelFilter("warn")
if levelFilterOpt.IsOk() {
gotracing.SetMinConsolePrintLevel(levelFilterOpt.Unwrap())
} else {
t.Error("levelFilter must be Ok<T>")
}
gotracing.SetMaxProgramCounters(10)
recursionFunc(10, gotracing.Warn, "Warn", "WARN")
}
func TestInfoWithPC(t *testing.T) {
levelFilterOpt := gotracing.NewLevelFilter("info")
if levelFilterOpt.IsOk() {
gotracing.SetMinConsolePrintLevel(levelFilterOpt.Unwrap())
} else {
t.Error("levelFilter must be Ok<T>")
}
recursionFuncMaxPC(10, gotracing.InfoPC, 1, "Info", "INFO")
}
func TestDebugWithPC(t *testing.T) {
levelFilterOpt := gotracing.NewLevelFilter("debug")
if levelFilterOpt.IsOk() {
gotracing.SetMinConsolePrintLevel(levelFilterOpt.Unwrap())
} else {
t.Error("levelFilter must be Ok<T>")
}
recursionFuncMaxPC(10, gotracing.DebugPC, 0, "debug", "DEBUG")
}
func TestSetMinStoreLevel(t *testing.T) {
levelFilterOpt := gotracing.NewLevelFilter("debug")
if levelFilterOpt.IsOk() {
gotracing.SetMinConsolePrintLevel(levelFilterOpt.Unwrap())
gotracing.SetMinStoreLevel(levelFilterOpt.Unwrap())
} else {
t.Error("levelFilter must be Ok<T>")
}
recursionFuncMaxPC(10, gotracing.DebugPC, 10, "debug", "DEBUG")
time.Sleep(1 * time.Second)
}
func TestStorage(t *testing.T) {
levelFilterOpt := gotracing.NewLevelFilter("debug")
if levelFilterOpt.IsOk() {
gotracing.SetMinConsolePrintLevel(levelFilterOpt.Unwrap())
gotracing.SetMinStoreLevel(levelFilterOpt.Unwrap())
} else {
t.Error("levelFilter must be Ok<T>")
}
storage := storage{}
gotracing.SetStorage(storage)
recursionFuncMaxPC(10, gotracing.DebugPC, 10, "debug", "DEBUG")
time.Sleep(1 * time.Second)
}
func TestLevel(t *testing.T) {
gotracing.SetMinConsolePrintLevel(gotracing.LevelFilterOff)
gotracing.InfoPC(0, "test-info")
gotracing.ErrorPC(0, "test-error")
gotracing.SetMinConsolePrintLevel(gotracing.LevelFilterInfo)
gotracing.TracePC(0, "test-trace")
gotracing.InfoPC(0, "test-info2")
gotracing.ErrorPC(0, "test-error2")
gotracing.SetMinConsolePrintLevel(gotracing.LevelFilterError)
gotracing.InfoPC(0, "test-info2")
gotracing.Error("test-error3")
}