-
Notifications
You must be signed in to change notification settings - Fork 1
/
tailor_test.go
147 lines (126 loc) · 2.58 KB
/
tailor_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2019 Yegor Myskin. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tailor_test
import (
"context"
"io"
"io/ioutil"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/un000/tailor"
)
func TestTailFileFromStart(t *testing.T) {
const fileName = "./file_from_start"
fileData := []byte("1\n2\n3\n")
err := ioutil.WriteFile(fileName, fileData, os.ModePerm)
if err != nil {
t.Error(err)
return
}
defer os.Remove(fileName)
f := tailor.New(fileName, tailor.WithSeekOnStartup(0, io.SeekStart))
if fileName != f.FileName() {
t.Error("file name mismatch")
return
}
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
err = f.Run(ctx)
if err != nil {
t.Error(err)
return
}
var i = 1
defer func() {
if i != 4 {
t.Error("not read to the end, last line:", i)
}
}()
for ; i <= 3; i++ {
select {
case line, ok := <-f.Lines():
if !ok {
return
}
if line.StringTrimmed() != strconv.Itoa(i) {
t.Errorf("want: '%d' actual '%s'", i, line.StringTrimmed())
return
}
t.Log(line.StringTrimmed())
case err, ok := <-f.Errors():
if !ok {
return
}
t.Error(err)
return
}
}
}
func TestLogrotate(t *testing.T) {
const fileName = "./file_logrotate"
const fileNameRotated = "./file_logrotate_rotated"
fileData := []byte("1\n2\n3\n")
err := ioutil.WriteFile(fileName, fileData, os.ModePerm)
if err != nil {
t.Error(err)
return
}
defer os.Remove(fileName)
defer os.Remove(fileNameRotated)
f := tailor.New(fileName, tailor.WithSeekOnStartup(0, io.SeekStart))
if fileName != f.FileName() {
t.Error("file name mismatch")
return
}
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
err = f.Run(ctx)
if err != nil {
t.Error(err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err := os.Rename(fileName, fileNameRotated)
if err != nil {
t.Error("error renaming file")
return
}
fileData := []byte("4\n5\n6\n")
err = ioutil.WriteFile(fileName, fileData, os.ModePerm)
if err != nil {
t.Error(err)
return
}
wg.Done()
}()
var i = 1
defer func() {
if i != 7 {
t.Error("not read to the end, last line:", i)
}
}()
for ; i <= 6; i++ {
select {
case line, ok := <-f.Lines():
if !ok {
return
}
if line.StringTrimmed() != strconv.Itoa(i) {
t.Errorf("want: '%d' actual '%s'", i, line.StringTrimmed())
return
}
t.Log(line.StringTrimmed())
case err, ok := <-f.Errors():
if !ok {
return
}
t.Error(err)
return
}
}
wg.Wait()
}