Skip to content

Commit

Permalink
fix: create closed channel on mark
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenrinzema committed Dec 2, 2019
1 parent f8c480e commit 70338ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/circuit/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (r *Ready) Mark() {
defer r.mutex.Unlock()

if r.mark == nil {
return
r.mark = make(chan struct{}, 0)
}

close(r.mark)
Expand Down
17 changes: 10 additions & 7 deletions internal/circuit/ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func TestReadyOnce(t *testing.T) {

func TestReadyOnceMultipleMark(t *testing.T) {
ready := Ready{}
timeout, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
timeout, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()

go ready.Mark()
go ready.Mark()
ready.Mark()
ready.Mark()

select {
case <-timeout.Done():
Expand All @@ -35,18 +36,20 @@ func TestReadyOnceMultipleMark(t *testing.T) {

func TestReadyOnceMultipleListeners(t *testing.T) {
ready := Ready{}
timeout, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)

listener := func() {
timeout, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()

select {
case <-timeout.Done():
t.Error("timeout reached")
case <-ready.On():
}
}

go listener()
go listener()

ready.Mark()

listener()
listener()
}

0 comments on commit 70338ab

Please sign in to comment.