-
Notifications
You must be signed in to change notification settings - Fork 4
/
sem_test.go
214 lines (184 loc) · 4.19 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
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
package semaphore
import (
"fmt"
"testing"
"time"
)
func Test_Semaphore(t *testing.T) {
var sem Semaphore
if err := sem.Open("/testsem", 0644, 1); err != nil {
t.Fatalf("Failed to open: %v", err)
}
if err := sem.Close(); err != nil {
t.Fatalf("Failed to close: %v", err)
}
if err := sem.Unlink(); err != nil {
t.Fatalf("Failed to unlink: %v", err)
}
}
func Test_SemGetValue(t *testing.T) {
var sem Semaphore
if err := sem.Open("/testsem", 0644, 1); err != nil {
t.Fatalf("Failed to open: %v", err)
}
val, err := sem.GetValue()
if err != nil {
t.Fatalf("Failed to get value: %v", err)
}
if val != 1 {
t.Error("Value incorrect")
}
if err := sem.Close(); err != nil {
t.Fatalf("Failed to close: %v", err)
}
}
func Test_SemWait(t *testing.T) {
var sem Semaphore
sem.Open("/testsem", 0644, 1)
if err := sem.Wait(); err != nil {
t.Fatalf("Failed to wait: %v", err)
}
val, _ := sem.GetValue()
if val != 0 {
t.Fatal("Value incorrect")
}
if err := sem.Post(); err != nil {
t.Fatalf("Failed to post: %v", err)
}
sem.Close()
}
func Test_SemTryWait(t *testing.T) {
var sem Semaphore
sem.Open("/testsem", 0644, 1)
if err := sem.TryWait(); err != nil {
t.Fatalf("Failed to wait: %v", err)
}
sem.Post()
sem.Close()
}
func Test_SemTryWaitFail(t *testing.T) {
var sem Semaphore
sem.Open("/testsem", 0644, 1)
sem.Wait()
err := sem.TryWait()
if err == nil {
t.Fatal("TryWait should have failed")
}
sem.Post()
sem.Close()
}
func Test_SemPostFail(t *testing.T) {
var sem Semaphore
err := sem.Post()
if err == nil {
t.Fatal("Post should have failed")
}
}
func Test_SemTimedWait_nowait(t *testing.T) {
var sem Semaphore
sem.Open("/testsem", 0644, 1)
if err := sem.TimedWait(1 * time.Second); err != nil {
t.Fatalf("Should not have timedout: %v", err)
}
sem.Close()
}
func Test_SemTimedWait_wait(t *testing.T) {
var sem Semaphore
sem.Open("/testsem_wait", 0644, 1)
sem.Wait()
v, _ := sem.GetValue()
fmt.Println(v)
end := make(chan error, 1)
go func() {
var sem2 Semaphore
sem2.Open("/testsem_wait", 0644, 1)
end <- sem2.TimedWait(2 * time.Second)
sem2.Close()
}()
time.Sleep(500 * time.Millisecond)
sem.Post()
err := <-end
sem.Close()
sem.Unlink()
if err != nil {
t.Fatalf("Should not have timedout: %v", err)
}
}
func Test_SemTimedWait_timeout(t *testing.T) {
var sem Semaphore
sem.Open("/testsem_wait", 0644, 1)
sem.Wait()
v, _ := sem.GetValue()
fmt.Println(v)
end := make(chan error, 1)
go func() {
var sem2 Semaphore
sem2.Open("/testsem_wait", 0644, 1)
end <- sem2.TimedWait(1 * time.Second)
sem2.Close()
}()
time.Sleep(2 * time.Second)
sem.Post()
err := <-end
sem.Close()
sem.Unlink()
if err == nil {
t.Fatalf("Should have timedout: %v", err)
}
}
func Test_SemDoubleClose(t *testing.T) {
var sem Semaphore
if err := sem.Open("/testsem", 0644, 1); err != nil {
t.Fatalf("Failed to open: %v", err)
}
if err := sem.Close(); err != nil {
t.Fatalf("Failed to close: %v", err)
}
if err := sem.Close(); err == nil {
t.Fatalf("Should have received error: %v", err)
}
}
func Test_SemDoubleUnlink(t *testing.T) {
var sem Semaphore
if err := sem.Open("/testsem", 0644, 1); err != nil {
t.Fatalf("Failed to open: %v", err)
}
if err := sem.Close(); err != nil {
t.Fatalf("Failed to close: %v", err)
}
if err := sem.Unlink(); err != nil {
t.Fatalf("Failed to unlink: %v", err)
}
if err := sem.Unlink(); err == nil {
t.Fatalf("Should have received error: %v", err)
}
}
func Test_isSemaphoreInitialized(t *testing.T) {
var sem Semaphore
if err := sem.Close(); err == nil {
t.Fatalf("Should have recived error: %v", err)
}
if _, err := sem.GetValue(); err == nil {
t.Fatalf("Should have recived error: %v", err)
}
if err := sem.Post(); err == nil {
t.Fatalf("Should have recived error: %v", err)
}
if err := sem.Wait(); err == nil {
t.Fatalf("Should have recived error: %v", err)
}
if err := sem.TryWait(); err == nil {
t.Fatalf("Should have recived error: %v", err)
}
}
func Test_longName(t *testing.T) {
var sem Semaphore
name := make([]byte, 256)
name[0] = '/'
for i := 1; i < 256; i++ {
name[i] = 'X'
}
if err := sem.Open(string(name), 0644, 1); err == nil {
t.Fatalf("Failed to open: %v", err)
}
}