Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc906248

Browse files
committed
chore(scaletest): add barrier synchronization primitive
1 parente13fcaf commitc906248

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

‎scaletest/harness/barrier.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package harness
2+
3+
import (
4+
"sync/atomic"
5+
)
6+
7+
typeBarrierstruct {
8+
count atomic.Int64
9+
donechanstruct{}
10+
}
11+
12+
// NewBarrier creates a new barrier that will block until `size` calls to Wait
13+
// or `Cancel` have been made. It's the caller's responsibility to ensure this
14+
// eventually happens.
15+
funcNewBarrier(sizeint)*Barrier {
16+
b:=&Barrier{
17+
done:make(chanstruct{}),
18+
}
19+
b.count.Store(int64(size))
20+
returnb
21+
}
22+
23+
// Wait blocks until the barrier count reaches zero.
24+
func (b*Barrier)Wait() {
25+
b.Cancel()
26+
<-b.done
27+
}
28+
29+
// Cancel decrements the barrier count, unblocking other Wait calls if it
30+
// reaches zero.
31+
func (b*Barrier)Cancel() {
32+
ifb.count.Add(-1)==0 {
33+
close(b.done)
34+
}
35+
}

‎scaletest/harness/barrier_test.go‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package harness_test
2+
3+
import (
4+
"sync"
5+
"testing"
6+
7+
"github.com/coder/coder/v2/scaletest/harness"
8+
"github.com/coder/coder/v2/testutil"
9+
)
10+
11+
funcTestBarrier_MultipleRunners(t*testing.T) {
12+
t.Parallel()
13+
constnumRunners=3
14+
15+
ctx:=testutil.Context(t,testutil.WaitShort)
16+
barrier:=harness.NewBarrier(numRunners)
17+
18+
varwg sync.WaitGroup
19+
wg.Add(numRunners)
20+
21+
done:=make(chanstruct{})
22+
23+
forrangenumRunners {
24+
gofunc() {
25+
deferwg.Done()
26+
barrier.Wait()
27+
}()
28+
}
29+
30+
gofunc() {
31+
wg.Wait()
32+
close(done)
33+
}()
34+
35+
select {
36+
case<-done:
37+
return
38+
case<-ctx.Done():
39+
t.Fatal("barrier should have released all runners")
40+
}
41+
}
42+
43+
funcTestBarrier_Cancel(t*testing.T) {
44+
t.Parallel()
45+
constnumRunners=3
46+
47+
ctx:=testutil.Context(t,testutil.WaitShort)
48+
barrier:=harness.NewBarrier(numRunners)
49+
50+
varwg sync.WaitGroup
51+
wg.Add(numRunners-1)
52+
53+
done:=make(chanstruct{})
54+
55+
forrangenumRunners-1 {
56+
gofunc() {
57+
deferwg.Done()
58+
barrier.Wait()
59+
}()
60+
}
61+
62+
barrier.Cancel()
63+
64+
gofunc() {
65+
wg.Wait()
66+
close(done)
67+
}()
68+
69+
select {
70+
case<-done:
71+
return
72+
case<-ctx.Done():
73+
t.Fatal("barrier should have released after cancel")
74+
}
75+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp