-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocker_test.go
58 lines (52 loc) · 901 Bytes
/
locker_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
package locker
import (
"bytes"
"fmt"
"sync"
"testing"
"time"
)
func newKey(values ...interface{}) string {
var b bytes.Buffer
for _, v := range values {
b.WriteString(fmt.Sprintf("%v", v))
}
return b.String()
}
func TestNewEntityLocker(t *testing.T) {
el := NewEntityLocker()
k1 := newKey("abc", 456)
k2 := newKey("abcd", 456)
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
el.Lock(k1)
fmt.Println(k1, 1)
time.Sleep(time.Second * 4)
fmt.Println("trying to unlock now")
el.Unlock(k1)
}()
go func() {
defer wg.Done()
el.Lock(k1)
fmt.Println(k1, 2)
time.Sleep(time.Second * 4)
el.Unlock(k1)
}()
go func() {
defer wg.Done()
el.Lock(k1)
fmt.Println(k1, 3)
time.Sleep(time.Second * 4)
el.Unlock(k1)
}()
go func() {
defer wg.Done()
el.Lock(k1)
fmt.Println(k2, 1)
time.Sleep(time.Second * 4)
el.Unlock(k1)
}()
wg.Wait()
}