-
Notifications
You must be signed in to change notification settings - Fork 5
/
sem_test.go
75 lines (72 loc) · 1.41 KB
/
sem_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
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package sem
import (
"github.com/hslam/ftok"
"testing"
"time"
)
func TestSem(t *testing.T) {
done := make(chan struct{})
semnum := 0
nsems := 1
go func() {
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
semid, err := Get(key, nsems, 0666)
if err != nil {
semid, err = Get(key, nsems, IPC_CREAT|IPC_EXCL|0666)
if err != nil {
panic(err)
}
defer Remove(semid)
for semnum := 0; semnum < nsems; semnum++ {
_, err := SetValue(semid, semnum, 1)
if err != nil {
panic(err)
}
}
}
ok, err := P(semid, semnum, IPC_NOWAIT|SEM_UNDO)
if err != nil {
t.Error(err)
}
if !ok {
t.Error("P failed!\n")
}
time.Sleep(time.Millisecond * 200)
ok, err = V(semid, semnum, IPC_NOWAIT|SEM_UNDO)
if err != nil {
t.Error(err)
}
if !ok {
t.Error("V failed!\n")
}
time.Sleep(time.Millisecond * 100)
close(done)
}()
time.Sleep(time.Millisecond * 100)
key, err := ftok.Ftok("/tmp", 0x22)
if err != nil {
panic(err)
}
semid, err := Get(key, nsems, 0666)
if err != nil {
t.Error()
}
if r, err := GetValue(semid, semnum); err != nil {
panic(err)
} else if r > 0 {
t.Error()
}
ok, err := P(semid, semnum, IPC_NOWAIT|SEM_UNDO)
if err != nil {
t.Error(err)
}
if ok {
t.Error()
}
<-done
}