- Notifications
You must be signed in to change notification settings - Fork1k
chore(scaletest): add barrier synchronization primitive#19903
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package harness | ||
import ( | ||
"sync/atomic" | ||
) | ||
type Barrier struct { | ||
count atomic.Int64 | ||
done chan struct{} | ||
} | ||
// NewBarrier creates a new barrier that will block until `size` calls to Wait | ||
// or `Cancel` have been made. It's the caller's responsibility to ensure this | ||
// eventually happens. | ||
func NewBarrier(size int) *Barrier { | ||
b := &Barrier{ | ||
done: make(chan struct{}), | ||
} | ||
b.count.Store(int64(size)) | ||
return b | ||
} | ||
// Wait blocks until the barrier count reaches zero. | ||
func (b *Barrier) Wait() { | ||
b.Cancel() | ||
<-b.done | ||
MemberAuthor
| ||
} | ||
// Cancel decrements the barrier count, unblocking other Wait calls if it | ||
// reaches zero. | ||
func (b *Barrier) Cancel() { | ||
if b.count.Add(-1) == 0 { | ||
close(b.done) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package harness_test | ||
import ( | ||
"sync" | ||
"testing" | ||
"github.com/coder/coder/v2/scaletest/harness" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
func TestBarrier_MultipleRunners(t *testing.T) { | ||
t.Parallel() | ||
const numRunners = 3 | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
barrier := harness.NewBarrier(numRunners) | ||
var wg sync.WaitGroup | ||
wg.Add(numRunners) | ||
done := make(chan struct{}) | ||
for range numRunners { | ||
go func() { | ||
defer wg.Done() | ||
barrier.Wait() | ||
}() | ||
} | ||
go func() { | ||
wg.Wait() | ||
close(done) | ||
}() | ||
select { | ||
case <-done: | ||
return | ||
case <-ctx.Done(): | ||
t.Fatal("barrier should have released all runners") | ||
} | ||
} | ||
func TestBarrier_Cancel(t *testing.T) { | ||
t.Parallel() | ||
const numRunners = 3 | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
barrier := harness.NewBarrier(numRunners) | ||
var wg sync.WaitGroup | ||
wg.Add(numRunners - 1) | ||
done := make(chan struct{}) | ||
for range numRunners - 1 { | ||
go func() { | ||
defer wg.Done() | ||
barrier.Wait() | ||
}() | ||
} | ||
barrier.Cancel() | ||
go func() { | ||
wg.Wait() | ||
close(done) | ||
}() | ||
select { | ||
case <-done: | ||
return | ||
case <-ctx.Done(): | ||
t.Fatal("barrier should have released after cancel") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. we have | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.