-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelastic_semaphore_test.go
125 lines (106 loc) · 3.2 KB
/
elastic_semaphore_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
package kaburaya
import (
"sync"
"testing"
)
func TestElasticSemaphoreWaitAndSignalWithoutBlocking(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
for i := 0; i < limit; i++ {
sem.wait()
}
if sem.counter != 0 {
t.Errorf("elasticSemaphore.wait should decrement the counter to 0, but %d.", sem.counter)
}
for i := 0; i < limit; i++ {
sem.signal()
}
if sem.counter != 2 {
t.Errorf("elasticSemaphore.signal should increment the counter to 2, but %d.", sem.counter)
}
}
func TestElasticSemaphoreWaitAndSignalWithBlocking(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
var wg sync.WaitGroup
for i := 0; i < limit+1; i++ {
wg.Add(1)
go func() {
defer wg.Done()
defer sem.signal()
sem.wait()
}()
}
wg.Wait()
if sem.counter != 2 {
t.Errorf("elasticSemaphore should increment the counter to 2, but %d.", sem.counter)
}
}
func TestElasticSemaphoreIncrementLimitZero(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
newLimit := sem.incrementLimit(0)
if newLimit != limit {
t.Errorf("elasticSemaphore.incrementLimit should not change the limit, but it changes to %d.", newLimit)
}
}
func TestElasticSemaphoreIncrementLimitPositive(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
inc := 1
newLimit := sem.incrementLimit(inc)
if newLimit != limit+inc {
t.Errorf("elasticSemaphore.incrementLimit should change the limit to %d, but it changes to %d.", limit+inc, newLimit)
}
if sem.counter != limit+inc {
t.Errorf("elasticSemaphore.incrementLimit should change the counter to %d, but it changes to %d.", limit+inc, sem.counter)
}
}
func TestElasticSemaphoreIncrementLimitPositiveWithBlocking(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
for i := 0; i < limit; i++ {
sem.wait()
}
waiting := make(chan bool)
done := make(chan bool)
go func() {
waiting <- true
sem.wait()
done <- true
}()
<-waiting // start blocking
inc := 1
newLimit := sem.incrementLimit(inc) // Increment limit and signal.
<-done
if newLimit != limit+inc {
t.Errorf("elasticSemaphore.incrementLimit should change the limit to %d, but it changes to %d.", limit+inc, newLimit)
}
if sem.counter != 0 {
t.Errorf("elasticSemaphore.incrementLimit should change the counter to %d, but it changes to %d.", 0, sem.counter)
}
}
func TestElasticSemaphoreIncrementLimitNegative(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
inc := -1
newLimit := sem.incrementLimit(inc)
if newLimit != limit+inc {
t.Errorf("elasticSemaphore.incrementLimit should change the limit to %d, but it changes to %d.", limit+inc, newLimit)
}
if sem.counter != newLimit {
t.Errorf("elasticSemaphore.incrementLimit should change the counter to %d, but it changes to %d.", newLimit, sem.counter)
}
}
func TestElasticSemaphoreIncrementLimitNegativeUnderFlow(t *testing.T) {
limit := 2
sem := newElasticSemaphore(limit)
inc := -2
newLimit := sem.incrementLimit(inc)
if newLimit != 1 {
t.Errorf("elasticSemaphore.incrementLimit should change the limit to %d, but it changes to %d.", 1, newLimit)
}
if sem.counter != newLimit {
t.Errorf("elasticSemaphore.incrementLimit should change the counter to %d, but it changes to %d.", newLimit, sem.counter)
}
}