-
Notifications
You must be signed in to change notification settings - Fork 16
/
tailer_test.go
87 lines (70 loc) · 1.73 KB
/
tailer_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
package ninetail
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"sync"
"testing"
"time"
"github.com/mattn/go-colorable"
)
func TestNewTailers(t *testing.T) {
names := []string{
"/very/very/long/path/but/base/is/short",
"/path/to/message",
"/p/t/very_very_long_base",
"/p/var/log/production.log",
"/p/var/log/development.log",
"/p/var/log/test.log",
}
tailers, err := NewTailers(names)
if err != nil {
t.Fatal(err)
}
if len(tailers) != len(names) {
t.Fatalf("Incorrect: tailers count expect(%d) actual(%d)", len(names), len(tailers))
}
if tailers[0].colorCode != ansiColorCodes[0] {
t.Fatal("Incorrect color code at 1st file")
}
if tailers[1].colorCode != ansiColorCodes[1] {
t.Fatal("Incorrect color code at 2nd file")
}
if tailers[5].colorCode != ansiColorCodes[0] { // Return to first color code
t.Fatal("Incorrect color code at 6th file")
}
}
func TestTailerDo(t *testing.T) {
file, err := ioutil.TempFile("", "ninetail_tailer_do")
if err != nil {
t.Fatal(err)
}
defer file.Close()
basename := filepath.Base(file.Name())
maxLength := len(basename) + 20 // 20 = padding
tailer, err := newTailer(file.Name(), 32, maxLength) // 32 = green
if err != nil {
t.Fatal(err)
}
output := new(bytes.Buffer)
var wg sync.WaitGroup
wg.Add(1)
go func(t *Tailer, output io.Writer) {
tailer.Do(colorable.NewNonColorable(output))
wg.Done()
}(tailer, output)
// Simulate to `echo line >> ninetail_tailer_do`
interval := time.Tick(100 * time.Millisecond)
<-interval
fmt.Fprint(file, "line\n")
<-interval
tailer.Stop()
wg.Wait()
expect := fmt.Sprintf(" %s: line\n", basename)
actual := output.String()
if expect != actual {
t.Fatal("Bad padding or line text")
}
}