-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_processing_test.go
128 lines (97 loc) · 2.24 KB
/
file_processing_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
package phonelab
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TextFileProcessorTestCommon(filename string) (int, error) {
var err error
processor := NewTextFileProcessor(filename, DEFAULT_MAX_CONCURRENCY, func(e error) {
err = e
})
logChan := processor.Process()
logs := 0
for log := range logChan {
if len(log.(string)) > 0 {
logs += 1
}
}
return logs, err
}
func TestTextFileProcessor(t *testing.T) {
t.Parallel()
assert := assert.New(t)
logs, err := TextFileProcessorTestCommon("./test/test.log")
assert.Nil(err)
assert.Equal(5000, logs)
}
func TestTextFileProcessorGZ(t *testing.T) {
t.Parallel()
assert := assert.New(t)
logs, err := TextFileProcessorTestCommon("./test/test.log.gz")
assert.Nil(err)
assert.Equal(5000, logs)
}
func TestMultTextFileProcessor(t *testing.T) {
t.Parallel()
assert := assert.New(t)
files := []string{
"test/test.log",
"test/test.10000.log",
}
gen := NewTextFileSourceGenerator(files, func(e error) {
t.Log("Error: ", e)
t.FailNow()
})
sourceChan := gen.Process()
pos := 0
for sourceInst := range sourceChan {
pos += 1
assert.Equal("file", sourceInst.Info.Type())
info, ok := sourceInst.Info.(*TextFileSourceInfo)
assert.True(ok)
expected := 0
switch info.Filename {
case "test/test.log":
expected = 5000
case "test/test.10000.log":
expected = 10000
default:
t.Fatal("Unexpected file: " + info.Filename)
}
logs := 0
logChan := sourceInst.Processor.Process()
for log := range logChan {
assert.True(len(log.(string)) > 0)
logs += 1
}
assert.Equal(expected, logs)
}
assert.Equal(2, pos)
}
// Make sure we can open the file multiple times
func TestTextFileProcessorMux(t *testing.T) {
t.Parallel()
assert := assert.New(t)
const NUM_OPENS = 10
processor := NewTextFileProcessor("./test/test.log", DEFAULT_MAX_CONCURRENCY, func(e error) {
t.Log("Error: ", e)
t.FailNow()
})
done := make(chan int)
for i := 0; i < NUM_OPENS; i++ {
go func() {
logChan := processor.Process()
logs := 0
for log := range logChan {
assert.True(len(log.(string)) > 0)
logs += 1
}
done <- logs
}()
}
allLogs := 0
for i := 0; i < NUM_OPENS; i++ {
allLogs += <-done
}
assert.Equal(5000*NUM_OPENS, allLogs)
}