-
Notifications
You must be signed in to change notification settings - Fork 41
/
driver_test.go
219 lines (178 loc) · 4.64 KB
/
driver_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package corral
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewDriver(t *testing.T) {
j := &Job{}
driver := NewDriver(
j,
WithSplitSize(100),
WithMapBinSize(200),
WithReduceBinSize(300),
WithWorkingLocation("s3://foo"),
)
assert.Equal(t, j, driver.jobs[0])
assert.Equal(t, int64(100), driver.config.SplitSize)
assert.Equal(t, int64(200), driver.config.MapBinSize)
assert.Equal(t, int64(300), driver.config.ReduceBinSize)
assert.Equal(t, "s3://foo", driver.config.WorkingLocation)
}
type testWCJob struct{}
func (testWCJob) Map(key, value string, emitter Emitter) {
for _, word := range strings.Fields(value) {
emitter.Emit(word, "1")
}
}
func (testWCJob) Reduce(key string, values ValueIterator, emitter Emitter) {
count := 0
for range values.Iter() {
count++
}
emitter.Emit(key, fmt.Sprintf("%d", count))
}
type testFilterJob struct {
prefix string
}
func (j *testFilterJob) Map(key, value string, emitter Emitter) {
if strings.HasPrefix(key, j.prefix) {
emitter.Emit(key, value)
}
}
func (j *testFilterJob) Reduce(key string, values ValueIterator, emitter Emitter) {
// Identity reducer
for value := range values.Iter() {
emitter.Emit(key, value)
}
}
func testOutputToKeyValues(output string) []keyValue {
lines := strings.Split(output, "\n")
keyVals := make([]keyValue, 0, len(lines))
for _, line := range lines {
split := strings.Split(line, "\t")
if len(split) != 2 {
continue
}
keyVals = append(keyVals, keyValue{
Key: split[0],
Value: split[1],
})
}
return keyVals
}
func TestLocalMapReduce(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
assert.Nil(t, err)
defer os.RemoveAll(tmpdir)
inputPath := filepath.Join(tmpdir, "test_input")
ioutil.WriteFile(inputPath, []byte("the test input\nthe input test\nfoo bar baz"), 0700)
job := NewJob(testWCJob{}, testWCJob{})
driver := NewDriver(
job,
WithInputs(tmpdir),
WithWorkingLocation(tmpdir),
)
driver.Main()
output, err := ioutil.ReadFile(filepath.Join(tmpdir, "output-part-0"))
assert.Nil(t, err)
keyVals := testOutputToKeyValues(string(output))
assert.Len(t, keyVals, 6)
correct := []keyValue{
{"the", "2"},
{"test", "2"},
{"input", "2"},
{"foo", "1"},
{"bar", "1"},
{"baz", "1"},
}
for _, kv := range correct {
assert.Contains(t, keyVals, kv)
}
}
func TestLocalMultiJob(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
assert.Nil(t, err)
defer os.RemoveAll(tmpdir)
inputPath := filepath.Join(tmpdir, "test_input")
ioutil.WriteFile(inputPath, []byte("the test input\nthe input test\nfoo bar baz"), 0700)
mr1 := testWCJob{}
job1 := NewJob(mr1, mr1)
// Second job filters out any keys that don't start with 't'
mr2 := &testFilterJob{prefix: "t"}
job2 := NewJob(mr2, mr2)
driver := NewMultiStageDriver([]*Job{job1, job2},
WithInputs(tmpdir),
WithWorkingLocation(tmpdir),
)
driver.Main()
output, err := ioutil.ReadFile(filepath.Join(tmpdir, "job1", "output-part-0"))
assert.Nil(t, err)
keyVals := testOutputToKeyValues(string(output))
assert.Len(t, keyVals, 2)
correct := []keyValue{
{"the", "2"},
{"test", "2"},
}
for _, kv := range correct {
assert.Contains(t, keyVals, kv)
}
}
func TestLocalNoCrashOnNoResolvedInputFiles(t *testing.T) {
job := NewJob(testWCJob{}, testWCJob{})
driver := NewDriver(
job,
WithInputs("does_not_exist"),
WithWorkingLocation("some_file"),
)
driver.Main()
}
type statefulJob struct {
filterWords *[]string
}
func (s statefulJob) Map(key, value string, emitter Emitter) {
for _, word := range strings.Fields(value) {
for _, filterWord := range *s.filterWords {
if filterWord == word {
emitter.Emit(word, "1")
}
}
}
}
func (statefulJob) Reduce(key string, values ValueIterator, emitter Emitter) {
count := 0
for range values.Iter() {
count++
}
emitter.Emit(key, fmt.Sprintf("%d", count))
}
func TestLocalStructFieldMapReduce(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
assert.Nil(t, err)
defer os.RemoveAll(tmpdir)
inputPath := filepath.Join(tmpdir, "test_input")
ioutil.WriteFile(inputPath, []byte("the test input\nthe input test\nfoo bar baz"), 0700)
jobStruct := statefulJob{filterWords: &[]string{"foo", "bar"}}
job := NewJob(jobStruct, jobStruct)
driver := NewDriver(
job,
WithInputs(tmpdir),
WithWorkingLocation(tmpdir),
)
driver.Main()
output, err := ioutil.ReadFile(filepath.Join(tmpdir, "output-part-0"))
assert.Nil(t, err)
keyVals := testOutputToKeyValues(string(output))
assert.Len(t, keyVals, 2)
correct := []keyValue{
{"foo", "1"},
{"bar", "1"},
}
for _, kv := range correct {
assert.Contains(t, keyVals, kv)
}
}