forked from kamilsk/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault_test.go
56 lines (49 loc) · 1.34 KB
/
default_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
package semaphore_test
import (
"runtime"
"testing"
"time"
. "github.com/kamilsk/semaphore/v5"
)
func TestAcquire(t *testing.T) {
rs := make([]ReleaseFunc, 0, Capacity())
do := func() {
for _, r := range rs {
r()
}
}
for i := 0; i < Capacity(); i++ {
r, _ := Acquire(nil)
rs = append(rs, r)
}
expected := "operation timeout"
if _, err := Acquire(WithTimeout(10 * time.Millisecond)); err.Error() != expected {
t.Errorf("an unexpected error. expected: %s; obtained: %v", expected, err)
}
do()
if r, err := Acquire(WithTimeout(10 * time.Millisecond)); err != nil {
t.Error("an unexpected error", err)
} else {
r()
}
}
func TestCapacity(t *testing.T) {
if obtained, expected := Capacity(), runtime.GOMAXPROCS(0); obtained != expected {
t.Errorf("an unexpected capacity. expected: %d; obtained: %d", expected, obtained)
}
}
func TestOccupied(t *testing.T) {
if obtained, expected := Occupied(), 0; obtained != expected {
t.Errorf("unexpected occupied places. expected: %d; obtained: %d", expected, obtained)
}
}
func TestRelease(t *testing.T) {
if err, expected := Release(), "semaphore is empty"; err.Error() != expected {
t.Errorf("an unexpected error. expected: %s; obtained: %v", expected, err)
}
}
func TestSignal(t *testing.T) {
if release, ok := <-Signal(nil); release == nil || !ok {
t.Error("unexpected signal")
}
}