forked from happyer/distributed-computing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
210 lines (188 loc) · 4.21 KB
/
test_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
package mapreduce
import (
"fmt"
"testing"
"time"
"bufio"
"log"
"os"
"sort"
"strconv"
"strings"
)
const (
nNumber = 100000
nMap = 100
nReduce = 50
)
// Create input file with N numbers
// Check if we have N numbers in output file
// Split in words
func MapFunc(file string, value string) (res []KeyValue) {
debug("Map %v\n", value)
words := strings.Fields(value)
for _, w := range words {
kv := KeyValue{w, ""}
res = append(res, kv)
}
return
}
// Just return key
func ReduceFunc(key string, values []string) string {
for _, e := range values {
debug("Reduce %s %v\n", key, e)
}
return ""
}
// Checks input file against output file: each input number should show up
// in the output file in string sorted order
func check(t *testing.T, files []string) {
output, err := os.Open("mrtmp.test")
if err != nil {
log.Fatal("check: ", err)
}
defer output.Close()
var lines []string
for _, f := range files {
input, err := os.Open(f)
if err != nil {
log.Fatal("check: ", err)
}
defer input.Close()
inputScanner := bufio.NewScanner(input)
for inputScanner.Scan() {
lines = append(lines, inputScanner.Text())
}
}
sort.Strings(lines)
outputScanner := bufio.NewScanner(output)
i := 0
for outputScanner.Scan() {
var v1 int
var v2 int
text := outputScanner.Text()
n, err := fmt.Sscanf(lines[i], "%d", &v1)
if n == 1 && err == nil {
n, err = fmt.Sscanf(text, "%d", &v2)
}
if err != nil || v1 != v2 {
t.Fatalf("line %d: %d != %d err %v\n", i, v1, v2, err)
}
i++
}
if i != nNumber {
t.Fatalf("Expected %d lines in output\n", nNumber)
}
}
// Workers report back how many RPCs they have processed in the Shutdown reply.
// Check that they processed at least 1 RPC.
func checkWorker(t *testing.T, l []int) {
for _, tasks := range l {
if tasks == 0 {
t.Fatalf("Some worker didn't do any work\n")
}
}
}
// Make input file
func makeInputs(num int) []string {
var names []string
var i = 0
for f := 0; f < num; f++ {
names = append(names, fmt.Sprintf("824-mrinput-%d.txt", f))
file, err := os.Create(names[f])
if err != nil {
log.Fatal("mkInput: ", err)
}
w := bufio.NewWriter(file)
for i < (f+1)*(nNumber/num) {
fmt.Fprintf(w, "%d\n", i)
i++
}
w.Flush()
file.Close()
}
return names
}
// Cook up a unique-ish UNIX-domain socket name
// in /var/tmp. can't use current directory since
// AFS doesn't support UNIX-domain sockets.
func port(suffix string) string {
s := "/var/tmp/824-"
s += strconv.Itoa(os.Getuid()) + "/"
os.Mkdir(s, 0777)
s += "mr"
s += strconv.Itoa(os.Getpid()) + "-"
s += suffix
return s
}
func setup() *Master {
files := makeInputs(nMap)
master := port("master")
mr := Distributed("test", files, nReduce, master)
return mr
}
func cleanup(mr *Master) {
mr.CleanupFiles()
for _, f := range mr.files {
removeFile(f)
}
}
func TestSequentialSingle(t *testing.T) {
mr := Sequential("test", makeInputs(1), 1, MapFunc, ReduceFunc)
mr.Wait()
check(t, mr.files)
checkWorker(t, mr.stats)
cleanup(mr)
}
func TestSequentialMany(t *testing.T) {
mr := Sequential("test", makeInputs(5), 3, MapFunc, ReduceFunc)
mr.Wait()
check(t, mr.files)
checkWorker(t, mr.stats)
cleanup(mr)
}
func TestBasic(t *testing.T) {
mr := setup()
for i := 0; i < 2; i++ {
go RunWorker(mr.address, port("worker"+strconv.Itoa(i)),
MapFunc, ReduceFunc, -1)
}
mr.Wait()
check(t, mr.files)
checkWorker(t, mr.stats)
cleanup(mr)
}
func TestOneFailure(t *testing.T) {
mr := setup()
// Start 2 workers that fail after 10 tasks
go RunWorker(mr.address, port("worker"+strconv.Itoa(0)),
MapFunc, ReduceFunc, 10)
go RunWorker(mr.address, port("worker"+strconv.Itoa(1)),
MapFunc, ReduceFunc, -1)
mr.Wait()
check(t, mr.files)
checkWorker(t, mr.stats)
cleanup(mr)
}
func TestManyFailures(t *testing.T) {
mr := setup()
i := 0
done := false
for !done {
select {
case done = <-mr.doneChannel:
check(t, mr.files)
cleanup(mr)
break
default:
// Start 2 workers each sec. The workers fail after 10 tasks
w := port("worker" + strconv.Itoa(i))
go RunWorker(mr.address, w, MapFunc, ReduceFunc, 10)
i++
w = port("worker" + strconv.Itoa(i))
go RunWorker(mr.address, w, MapFunc, ReduceFunc, 10)
i++
time.Sleep(1 * time.Second)
}
}
}