diff --git a/common/locks/condition_variable_impl.go b/common/locks/condition_variable_impl.go index b49c1829efb..64c08e9f34a 100644 --- a/common/locks/condition_variable_impl.go +++ b/common/locks/condition_variable_impl.go @@ -46,7 +46,7 @@ func NewConditionVariable( lock: lock, chanLock: sync.Mutex{}, - channel: make(chan struct{}, 1), + channel: newCVChannel(), } } @@ -64,7 +64,7 @@ func (c *ConditionVariableImpl) Signal() { // Broadcast wakes all goroutines waiting on this condition variable. func (c *ConditionVariableImpl) Broadcast() { - newChannel := make(chan struct{}) + newChannel := newCVChannel() c.chanLock.Lock() defer c.chanLock.Unlock() @@ -104,3 +104,7 @@ func (c *ConditionVariableImpl) Wait( // interrupted } } + +func newCVChannel() chan struct{} { + return make(chan struct{}, 1) +} diff --git a/common/locks/condition_variable_test.go b/common/locks/condition_variable_test.go index 61e7c53cc16..9c7a7f9de26 100644 --- a/common/locks/condition_variable_test.go +++ b/common/locks/condition_variable_test.go @@ -40,7 +40,7 @@ type ( suite.Suite lock sync.Locker - cv ConditionVariable + cv *ConditionVariableImpl } ) @@ -67,6 +67,34 @@ func (s *conditionVariableSuite) TearDownTest() { } +func (s *conditionVariableSuite) TestChannelSize_New() { + s.testChannelSize(s.cv.channel) +} + +func (s *conditionVariableSuite) TestChannelSize_Broadcast() { + s.cv.Broadcast() + s.testChannelSize(s.cv.channel) +} + +func (s *conditionVariableSuite) testChannelSize( + channel chan struct{}, +) { + // assert channel size == 1 + select { + case channel <- struct{}{}: + // noop + default: + s.Fail("conditional variable size should be 1") + } + + select { + case channel <- struct{}{}: + s.Fail("conditional variable size should be 1") + default: + // noop + } +} + func (s *conditionVariableSuite) TestSignal() { signalWaitGroup := sync.WaitGroup{} signalWaitGroup.Add(1)