Semaphore empty check added
Sudersen Archakam
6 years ago
42 | 42 |
func (s *Semaphore) Release() {
|
43 | 43 |
<-s.sem
|
44 | 44 |
}
|
|
45 |
|
|
46 |
// IsEmpty would return true if none acquired ar that moment of time, otherwise false.
|
|
47 |
func (s *Semaphore) IsEmpty() bool {
|
|
48 |
return len(s.sem) == 0
|
|
49 |
}
|
44 | 44 |
}
|
45 | 45 |
}
|
46 | 46 |
|
|
47 |
func TestSemaphoreEmpty(t *testing.T) {
|
|
48 |
sem := New(2, 200*time.Millisecond)
|
|
49 |
|
|
50 |
if !sem.IsEmpty() {
|
|
51 |
t.Error("semaphore should be empty")
|
|
52 |
}
|
|
53 |
|
|
54 |
sem.Acquire()
|
|
55 |
|
|
56 |
if sem.IsEmpty() {
|
|
57 |
t.Error("semaphore should not be empty")
|
|
58 |
}
|
|
59 |
|
|
60 |
sem.Release()
|
|
61 |
|
|
62 |
if !sem.IsEmpty() {
|
|
63 |
t.Error("semaphore should be empty")
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
47 | 67 |
func ExampleSemaphore() {
|
48 | 68 |
sem := New(3, 1*time.Second)
|
49 | 69 |
|