-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutex_test.go
54 lines (46 loc) · 865 Bytes
/
mutex_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
package etcdplus
import (
"github.com/coreos/go-etcd/etcd"
"testing"
"time"
)
func TestMutex(t *testing.T) {
c := etcd.NewClient()
uuid := getUUID()
mutex := NewMutex(c, uuid)
err := mutex.Lock(1 * time.Second)
if err != nil {
t.Fatal(err)
}
fatal := make(chan bool)
go func() {
err := mutex.Lock(1 * time.Second) // should not succeed
if err == nil {
fatal <- true
} else {
fatal <- false
}
}()
if <-fatal {
t.Fatal("Should not be able to acquire a lock twice.")
}
success := make(chan bool)
go func() {
err := mutex.Lock(1 * time.Second)
if err == nil {
success <- true
} else {
success <- false
t.Fatal(err)
}
}()
wait := time.After(500 * time.Millisecond)
<-wait
err = mutex.Unlock()
if err != nil {
t.Fatal(err)
}
if !(<-success) {
t.Fatal("Fail to acquire lock after it's released.")
}
}