-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_test.go
478 lines (374 loc) · 9.96 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package lockservice
import "testing"
import "runtime"
import "math/rand"
import "os"
import "strconv"
import "time"
import "fmt"
func tl(t *testing.T, ck *Clerk, lockname string, expected bool) {
x := ck.Lock(lockname)
if x != expected {
t.Fatalf("Lock(%v) returned %v; expected %v", lockname, x, expected)
}
}
func tu(t *testing.T, ck *Clerk, lockname string, expected bool) {
x := ck.Unlock(lockname)
if x != expected {
t.Fatalf("Unlock(%v) returned %v; expected %v", lockname, x, expected)
}
}
//
// 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 += strconv.Itoa(os.Getpid()) + "-"
s += suffix
return s
}
func TestBasic(t *testing.T) {
fmt.Printf("Test: Basic lock/unlock ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck := MakeClerk(phost, bhost)
tl(t, ck, "a", true)
tu(t, ck, "a", true)
tl(t, ck, "a", true)
tl(t, ck, "b", true)
tu(t, ck, "a", true)
tu(t, ck, "b", true)
tl(t, ck, "a", true)
tl(t, ck, "a", false)
tu(t, ck, "a", true)
tu(t, ck, "a", false)
p.kill()
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail1(t *testing.T) {
fmt.Printf("Test: Primary failure ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck := MakeClerk(phost, bhost)
tl(t, ck, "a", true)
tl(t, ck, "b", true)
tu(t, ck, "b", true)
tl(t, ck, "c", true)
tl(t, ck, "c", false)
tl(t, ck, "d", true)
tu(t, ck, "d", true)
tl(t, ck, "d", true)
p.kill()
tl(t, ck, "a", false)
tu(t, ck, "a", true)
tu(t, ck, "b", false)
tl(t, ck, "b", true)
tu(t, ck, "c", true)
tu(t, ck, "d", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail2(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #1 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tl(t, ck1, "b", true)
p.dying = true
tl(t, ck2, "c", true)
tl(t, ck1, "c", false)
tu(t, ck2, "c", true)
tl(t, ck1, "c", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail3(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #2 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tl(t, ck1, "b", true)
p.dying = true
tl(t, ck1, "b", false)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail4(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #3 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tl(t, ck1, "b", true)
p.dying = true
tl(t, ck2, "b", false)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail5(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #4 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tl(t, ck1, "b", true)
tu(t, ck1, "b", true)
p.dying = true
tu(t, ck1, "b", false)
tl(t, ck2, "b", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail6(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #5 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tu(t, ck1, "a", true)
tu(t, ck2, "a", false)
tl(t, ck1, "b", true)
p.dying = true
tu(t, ck2, "b", true)
tl(t, ck1, "b", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail7(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #6 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tu(t, ck1, "a", true)
tu(t, ck2, "a", false)
tl(t, ck1, "b", true)
p.dying = true
ch := make(chan bool)
go func() {
ok := false
defer func() { ch <- ok }()
tu(t, ck2, "b", true) // 2 second delay until retry
ok = true
}()
time.Sleep(1 * time.Second)
tl(t, ck1, "b", true)
ok := <- ch
if ok == false {
t.Fatalf("re-sent Unlock did not return true")
}
tu(t, ck1, "b", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestPrimaryFail8(t *testing.T) {
fmt.Printf("Test: Primary failure just before reply #7 ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck1 := MakeClerk(phost, bhost)
ck2 := MakeClerk(phost, bhost)
tl(t, ck1, "a", true)
tu(t, ck1, "a", true)
p.dying = true
ch := make(chan bool)
go func() {
ok := false
defer func() { ch <- ok }()
tu(t, ck2, "a", false) // 2 second delay until retry
ok = true
}()
time.Sleep(1 * time.Second)
tl(t, ck1, "a", true)
ok := <- ch
if ok == false {
t.Fatalf("re-sent Unlock did not return false")
}
tu(t, ck1, "a", true)
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestBackupFail(t *testing.T) {
fmt.Printf("Test: Backup failure ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
ck := MakeClerk(phost, bhost)
tl(t, ck, "a", true)
tl(t, ck, "b", true)
tu(t, ck, "b", true)
tl(t, ck, "c", true)
tl(t, ck, "c", false)
tl(t, ck, "d", true)
tu(t, ck, "d", true)
tl(t, ck, "d", true)
b.kill()
tl(t, ck, "a", false)
tu(t, ck, "a", true)
tu(t, ck, "b", false)
tl(t, ck, "b", true)
tu(t, ck, "c", true)
tu(t, ck, "d", true)
p.kill()
fmt.Printf(" ... Passed\n")
}
func TestMany(t *testing.T) {
fmt.Printf("Test: Multiple clients with primary failure ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
const nclients = 2
const nlocks = 10
done := false
var state [nclients][nlocks]bool
var acks [nclients]bool
for xi := 0; xi < nclients; xi++ {
go func(i int){
ck := MakeClerk(phost, bhost)
rr := rand.New(rand.NewSource(int64(os.Getpid()+i)))
for done == false {
locknum := (rr.Int() % nlocks)
lockname := strconv.Itoa(locknum + (i * 1000))
what := rr.Int() % 2
if what == 0 {
ck.Lock(lockname)
state[i][locknum] = true
} else {
ck.Unlock(lockname)
state[i][locknum] = false
}
}
acks[i] = true
}(xi)
}
time.Sleep(2 * time.Second)
p.kill()
time.Sleep(2 * time.Second)
done = true
time.Sleep(time.Second)
ck := MakeClerk(phost, bhost)
for xi := 0; xi < nclients; xi++ {
if acks[xi] == false {
t.Fatal("one client didn't complete")
}
for locknum := 0; locknum < nlocks; locknum++ {
lockname := strconv.Itoa(locknum + (xi * 1000))
locked := ! ck.Lock(lockname)
if locked != state[xi][locknum] {
t.Fatal("bad final state")
}
}
}
b.kill()
fmt.Printf(" ... Passed\n")
}
func TestConcurrentCounts(t *testing.T) {
fmt.Printf("Test: Multiple clients, single lock, primary failure ...\n")
runtime.GOMAXPROCS(4)
phost := port("p")
bhost := port("b")
p := StartServer(phost, bhost, true) // primary
b := StartServer(phost, bhost, false) // backup
const nclients = 2
const nlocks = 1
done := false
var acks [nclients]bool
var locks [nclients][nlocks] int
var unlocks [nclients][nlocks] int
for xi := 0; xi < nclients; xi++ {
go func(i int){
ck := MakeClerk(phost, bhost)
rr := rand.New(rand.NewSource(int64(os.Getpid()+i)))
for done == false {
locknum := rr.Int() % nlocks
lockname := strconv.Itoa(locknum)
what := rr.Int() % 2
if what == 0 {
if ck.Lock(lockname) {
locks[i][locknum]++
}
} else {
if ck.Unlock(lockname) {
unlocks[i][locknum]++
}
}
}
acks[i] = true
}(xi)
}
time.Sleep(2 * time.Second)
p.kill()
time.Sleep(2 * time.Second)
done = true
time.Sleep(time.Second)
for xi := 0; xi < nclients; xi++ {
if acks[xi] == false {
t.Fatal("one client didn't complete")
}
}
ck := MakeClerk(phost, bhost)
for locknum := 0; locknum < nlocks; locknum++ {
nl := 0
nu := 0
for xi := 0; xi < nclients; xi++ {
nl += locks[xi][locknum]
nu += unlocks[xi][locknum]
}
locked := ck.Unlock(strconv.Itoa(locknum))
// fmt.Printf("lock=%d nl=%d nu=%d locked=%v\n",
// locknum, nl, nu, locked)
if nl < nu || nl > nu + 1 {
t.Fatal("lock race 1")
}
if nl == nu && locked != false {
t.Fatal("lock race 2")
}
if nl != nu && locked != true {
t.Fatal("lock race 3")
}
}
b.kill()
fmt.Printf(" ... Passed\n")
}