-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_writer_test.go
86 lines (79 loc) · 1.73 KB
/
file_writer_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
package main
import (
"io/ioutil"
"os"
"strings"
"testing"
"gnd.la/log"
)
func writeText(t *testing.T, w *fileWriter) {
if err := w.Write("test", []byte(strings.Repeat("A", int(w.maxSize)))); err != nil {
t.Fatal(err)
}
}
func checkFiles(t *testing.T, w *fileWriter) {
for ii := 0; ii < w.count; ii++ {
var exp string
if ii == 0 {
exp = w.logPath(ii)
} else {
exp = w.compressedLogPath(ii)
}
if _, err := os.Stat(exp); err != nil {
t.Errorf("error checking file at %s: %s", exp, err)
}
}
files, err := ioutil.ReadDir(w.dir)
if err != nil {
t.Error(err)
}
if len(files) != w.count {
names := make([]string, len(files))
for ii, v := range files {
names[ii] = v.Name()
}
t.Fatalf("expecting %d files, got %d instead: %v", w.count, len(files), names)
}
}
func openTestFileWriter(t *testing.T) (*fileWriter, string) {
if testing.Verbose() {
log.SetLevel(log.LDebug)
}
dir, err := ioutil.TempDir("", "filewriter")
if err != nil {
t.Fatal(err)
}
w := &fileWriter{dir: dir, maxSize: 128, count: 3, waitCompress: true}
if err := w.Open("test"); err != nil {
os.RemoveAll(dir)
t.Fatal(err)
}
return w, dir
}
func TestFileWriter(t *testing.T) {
w, dir := openTestFileWriter(t)
defer os.RemoveAll(dir)
defer w.Close()
for ii := 0; ii < 3; ii++ {
writeText(t, w)
}
if err := w.Write("", []byte("A")); err != nil {
t.Fatal(err)
}
checkFiles(t, w)
}
func TestFileWriterFiles(t *testing.T) {
w, dir := openTestFileWriter(t)
defer os.RemoveAll(dir)
defer w.Close()
for _, v := range []int{1, 2} {
if err := ioutil.WriteFile(w.logPath(v), []byte{}, 0644); err != nil {
t.Fatal(err)
}
}
writeText(t, w)
if err := w.Write("", []byte("A")); err != nil {
t.Fatal(err)
}
checkFiles(t, w)
}