-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrier_test.go
More file actions
52 lines (44 loc) · 952 Bytes
/
Copy pathbarrier_test.go
File metadata and controls
52 lines (44 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package syncx
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func TestBarrierSynchronizes(t *testing.T) {
b := NewBarrier(3)
var wg sync.WaitGroup
wg.Add(3)
errCh := make(chan error, 3)
for i := 0; i < 3; i++ {
go func() {
defer wg.Done()
errCh <- b.Wait(context.Background())
}()
}
wg.Wait()
close(errCh)
for err := range errCh {
if err != nil {
t.Fatalf("unexpected barrier err: %v", err)
}
}
}
func TestBarrierCancellationBreaksRound(t *testing.T) {
b := NewBarrier(3)
errOther := make(chan error, 1)
go func() {
errOther <- b.Wait(context.Background())
}()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
err := b.Wait(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected cancellation error, got %v", err)
}
err2 := <-errOther
if !errors.Is(err2, ErrBarrierBroken) {
t.Fatalf("expected broken barrier, got %v", err2)
}
}