-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpostgres_test.go
More file actions
68 lines (57 loc) · 1.23 KB
/
Copy pathpostgres_test.go
File metadata and controls
68 lines (57 loc) · 1.23 KB
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
package synlock
import (
"errors"
"fmt"
"runtime"
"testing"
"time"
)
func TestPostgresMutex(t *testing.T) {
p, err := NewPostgres(DefPostgresOpts)
if err != nil {
t.Fatalf("init postgres object error: %s", err)
}
m1, err := p.NewMutex(123)
if err != nil {
t.Fatalf("making new mutex error: %s", err)
}
mu1 := m1.(*PostgresMutex)
m2, err := p.NewMutex(123)
if err != nil {
t.Fatalf("making new mutex error: %s", err)
}
mu2 := m2.(*PostgresMutex)
var critical = false
if err = mu1.lock(); err != nil {
t.Fatalf("acquiring lock error: %s", err)
}
resChan := make(chan error, 1)
go func() {
defer close(resChan)
if critical {
resChan <- errors.New("invalid critical section value")
return
}
if err := mu2.lock(); err != nil {
resChan <- fmt.Errorf("acquiring lock error: %s", err)
return
}
if !critical {
resChan <- errors.New("unexpected access to the critical section")
return
}
if err := mu2.unlock(); err != nil {
resChan <- fmt.Errorf("releasinglock error: %s", err)
return
}
}()
runtime.Gosched()
critical = true
time.Sleep(time.Second)
if err := mu1.unlock(); err != nil {
t.Fatalf("releasing lock error: %s", err)
}
for m := range resChan {
t.Fatal(m.Error())
}
}