This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logcabin_test.go
111 lines (97 loc) · 2.5 KB
/
logcabin_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
package logcabin
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"regexp"
"testing"
)
func TestAllLoggerOutput(t *testing.T) {
svc, artifact, msg := "test_service", "test_artifact", "msg"
Init(svc, artifact)
testLoggerOutput(svc, artifact, traceLevel, msg, Trace, t)
testLoggerOutput(svc, artifact, infoLevel, msg, Info, t)
testLoggerOutput(svc, artifact, warnLevel, msg, Warning, t)
testLoggerOutput(svc, artifact, errorLevel, msg, Error, t)
}
func testLoggerOutput(expectedSvc string, expectedArtifact string, expectedLvl string, expectedMsg string, logger *log.Logger, t *testing.T) {
original := os.Stdout
restore := func() {
os.Stdout = original
}
defer restore()
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
os.Stdout = w
logger.Println(expectedMsg)
w.Close()
actualBytes, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
var msg logMessage
err = json.Unmarshal(actualBytes, &msg)
if err != nil {
t.Error(err)
}
actual := msg.Service
expected := expectedSvc
if expected != actual {
t.Errorf("msg.Service was %s instead of %s", actual, expected)
}
actual = msg.Artifact
expected = expectedArtifact
if actual != expected {
t.Errorf("msg.Artifact was %s instead of %s", actual, expected)
}
actual = msg.Group
expected = "org.iplantc"
if actual != expected {
t.Errorf("msg.Group was %s instead of %s", actual, expected)
}
actual = msg.Level
expected = expectedLvl
if actual != expected {
t.Errorf("msg.Level was %s instead of %s", actual, expected)
}
actual = msg.Message
expected = expectedMsg
if match, _ := regexp.MatchString(expectedMsg, actual); !match {
t.Errorf("msg.Message was \"%s\", and did not contain %s", actual, expected)
}
}
func BenchmarkTrace(b *testing.B) {
svc, artifact, msg := "test_service", "test_artifact", "msg"
Init(svc, artifact)
Trace.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
Trace.Println(msg)
}
}
func BenchmarkInfo(b *testing.B) {
svc, artifact, msg := "test_service", "test_artifact", "msg"
Init(svc, artifact)
Info.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
Info.Println(msg)
}
}
func BenchmarkWarn(b *testing.B) {
svc, artifact, msg := "test_service", "test_artifact", "msg"
Init(svc, artifact)
Warning.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
Warning.Println(msg)
}
}
func BenchmarkErr(b *testing.B) {
svc, artifact, msg := "test_service", "test_artifact", "msg"
Init(svc, artifact)
Error.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
Error.Println(msg)
}
}