Skip to content

Commit

Permalink
Merge pull request #6 from kylrth/patch-1
Browse files Browse the repository at this point in the history
allow limit to equal zero
  • Loading branch information
marusama committed Mar 28, 2021
2 parents ee99b20 + 20ec211 commit 41c532b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
8 changes: 4 additions & 4 deletions semaphore.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ type semaphore struct {

// New initializes a new instance of the Semaphore, specifying the maximum number of concurrent entries.
func New(limit int) Semaphore {
if limit <= 0 {
panic("semaphore limit must be greater than 0")
if limit < 0 {
panic("semaphore limit must not be negative")
}
broadcastCh := make(chan struct{})
return &semaphore{
Expand Down Expand Up @@ -189,8 +189,8 @@ func (s *semaphore) Release(n int) int {
}

func (s *semaphore) SetLimit(limit int) {
if limit <= 0 {
panic("semaphore limit must be greater than 0")
if limit < 0 {
panic("semaphore limit must not be negative")
}
for {
state := atomic.LoadUint64(&s.state)
Expand Down
19 changes: 3 additions & 16 deletions semaphore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func TestNew(t *testing.T) {
checkLimitAndCount(t, sem, 1, 0)
},
func() {
defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
_ = New(0)
sem := New(0)
checkLimitAndCount(t, sem, 0, 0)
},
func() {
defer func() {
Expand Down Expand Up @@ -359,18 +355,9 @@ func TestSemaphore_SetLimit(t *testing.T) {

sem.SetLimit(1)
checkLimitAndCount(t, sem, 1, 0)
}

func TestSemaphore_SetLimit_zero_limit_panic_expected(t *testing.T) {
sem := New(1)
checkLimitAndCount(t, sem, 1, 0)

defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
sem.SetLimit(0)
checkLimitAndCount(t, sem, 0, 0)
}

func TestSemaphore_SetLimit_negative_limit_panic_expected(t *testing.T) {
Expand Down

0 comments on commit 41c532b

Please sign in to comment.