This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
92 lines (78 loc) · 2.27 KB
/
file_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
package golog
import (
"io/ioutil"
"strings"
"sync"
"testing"
"time"
)
const file_prefix string = "golog_filetest"
func readLogFile(filename string) (loglines []string, err error) {
file_data, err := ioutil.ReadFile(filename)
if err != nil {
return []string{}, err
}
log := string(file_data)
log = strings.TrimSpace(log)
return strings.Split(log, "\n"), nil
}
func TestDifferentPriorities(t *testing.T) {
tmpfile, err := ioutil.TempFile("", file_prefix)
if err != nil {
t.Fatalf("Couldn't open tmp file: %s", err.Error())
}
proc := NewProcessorFromWriter(LOG_DEBUG, tmpfile, true)
logger := NewLogger("file_test: ")
logger.AddProcessor("file", proc)
for _, p := range Priorities() {
logger.Logf(p, "Hey, listen...")
}
d, _ := time.ParseDuration("100ms")
time.Sleep(d)
loglines, err := readLogFile(tmpfile.Name())
if err != nil {
t.Fatalf("Failed to read log file: %s", err.Error())
}
if len(loglines) != len(Priorities()) {
msg := "Unexpected number of log lines ouputed. Expected %d, but was %d"
t.Fatalf(msg, len(Priorities()), len(loglines))
}
for i, p := range Priorities() {
expected := " " + p.ShortString() + ": file_test: Hey, listen..."
if !strings.HasSuffix(loglines[i], expected) {
t.Errorf("Unexpected log line.\nExpected: <date>%s\nBut was: %s", expected, loglines[i])
}
}
}
func TestConcurrentLogging(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "golog_conc_file_test")
if err != nil {
t.Fatalf("Couldn't create tmp file: %s", err.Error())
}
proc := NewProcessorFromWriter(LOG_DEBUG, tmpfile, true)
logger := NewLogger("conc_test: ")
logger.AddProcessor("file", proc)
wg := &sync.WaitGroup{}
num_routines := 5000
for i := 0; i < num_routines; i++ {
wg.Add(1)
go func() {
logger.Noticef("Help! I need somebody!")
logger.Warningf("Help! Not just anybody!")
logger.Errorf("Help! You know I need someone!")
logger.Criticalf("Heeeeeeeeeeeeelp!")
wg.Done()
}()
}
wg.Wait()
d, _ := time.ParseDuration("100ms")
time.Sleep(d)
loglines, err := readLogFile(tmpfile.Name())
if err != nil {
t.Fatalf("Error when reading tmp file: %s", err.Error())
}
if len(loglines) != (num_routines * 4) {
msg := "Unexpected number of log lines. Expected %d, but was %d"
t.Fatalf(msg, num_routines*4, len(loglines))
}
}