From 70338ab41860691287917bb63297f297d89fcdaa Mon Sep 17 00:00:00 2001 From: Jeroen Rinzema Date: Mon, 2 Dec 2019 15:25:04 +0100 Subject: [PATCH] fix: create closed channel on mark --- internal/circuit/ready.go | 2 +- internal/circuit/ready_test.go | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/circuit/ready.go b/internal/circuit/ready.go index cacbeda..e7ffbd5 100644 --- a/internal/circuit/ready.go +++ b/internal/circuit/ready.go @@ -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) diff --git a/internal/circuit/ready_test.go b/internal/circuit/ready_test.go index 7b0b1c2..f4ac79b 100644 --- a/internal/circuit/ready_test.go +++ b/internal/circuit/ready_test.go @@ -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(): @@ -35,9 +36,11 @@ 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") @@ -45,8 +48,8 @@ func TestReadyOnceMultipleListeners(t *testing.T) { } } - go listener() - go listener() - ready.Mark() + + listener() + listener() }