Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: conditional variable broadcast channel size #3906

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions common/locks/condition_variable_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewConditionVariable(
lock: lock,

chanLock: sync.Mutex{},
channel: make(chan struct{}, 1),
channel: newCVChannel(),
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -104,3 +104,7 @@ func (c *ConditionVariableImpl) Wait(
// interrupted
}
}

func newCVChannel() chan struct{} {
return make(chan struct{}, 1)
}
30 changes: 29 additions & 1 deletion common/locks/condition_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type (
suite.Suite

lock sync.Locker
cv ConditionVariable
cv *ConditionVariableImpl
}
)

Expand All @@ -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)
Expand Down