forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeated_wait_group_test.go
72 lines (64 loc) · 1.49 KB
/
repeated_wait_group_test.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfssync
import (
"sync"
"testing"
"golang.org/x/net/context"
)
func testRepeatedWaitGroupSimpleWait(t *testing.T, rwg *RepeatedWaitGroup) {
// Start 10 tasks, wait for them all to complete
rwg.Add(10)
errChan := make(chan error)
go func() {
errChan <- rwg.Wait(context.Background())
}()
for i := 0; i < 10; i++ {
rwg.Done()
}
err := <-errChan
if err != nil {
t.Fatalf("Error on wait: %v", err)
}
}
func TestRepeatedWaitGroupSimpleWait(t *testing.T) {
var rwg RepeatedWaitGroup
testRepeatedWaitGroupSimpleWait(t, &rwg)
}
func TestRepeatedWaitGroupCanceledWait(t *testing.T) {
// Start 10 tasks, wait for them all to complete
var rwg RepeatedWaitGroup
rwg.Add(10)
errChan := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
go func() {
errChan <- rwg.Wait(ctx)
}()
// Only finish half the tasks
for i := 0; i < 5; i++ {
rwg.Done()
}
cancel()
err := <-errChan
if err != context.Canceled {
t.Fatalf("Unexpected error on wait: %v", err)
}
}
func TestRepeatedWaitGroupMultiWait(t *testing.T) {
var rwg RepeatedWaitGroup
// Three in serial
for i := 0; i < 3; i++ {
testRepeatedWaitGroupSimpleWait(t, &rwg)
}
// Three in parallel!
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
defer wg.Done()
testRepeatedWaitGroupSimpleWait(t, &rwg)
}()
}
wg.Wait()
}