summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta/tgroup.go
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3/go/src/xdelta/tgroup.go')
-rw-r--r--xdelta3/go/src/xdelta/tgroup.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/xdelta3/go/src/xdelta/tgroup.go b/xdelta3/go/src/xdelta/tgroup.go
new file mode 100644
index 0000000..bb34258
--- /dev/null
+++ b/xdelta3/go/src/xdelta/tgroup.go
@@ -0,0 +1,93 @@
1package xdelta
2
3import (
4 "fmt"
5 "sync"
6 "time"
7)
8
9type TestGroup struct {
10 *Runner
11 sync.Mutex
12 running []Goroutine
13 waitChan <-chan bool
14}
15
16type Goroutine struct {
17 name string
18 errChan chan error
19}
20
21func NewTestGroup(r *Runner) (*TestGroup, Goroutine) {
22 g := Goroutine{"main", make(chan error)}
23 wc := make(chan bool)
24 tg := &TestGroup{Runner: r, running: []Goroutine{g}, waitChan: wc}
25 go waitAll(tg, wc)
26 return tg, g
27}
28
29func (g *Goroutine) String() string {
30 return fmt.Sprint("[", g.name, "]")
31}
32
33func (g *Goroutine) OK() {
34 g.errChan <- nil
35 _ = <- g.errChan
36}
37
38func (g *Goroutine) Panic(err error) {
39 g.errChan <- err
40 _ = <- g.errChan
41 select {}
42}
43
44func (t *TestGroup) Go(name string, f func(Goroutine)) {
45 g := Goroutine{name, make(chan error)}
46 t.Lock()
47 t.running = append(t.running, g)
48 t.Unlock()
49 go f(g)
50}
51
52func (t *TestGroup) Wait(g Goroutine) {
53 g.OK()
54 t.Lock()
55 t.waitChan = nil
56 wc := t.waitChan
57 t.Unlock()
58 _ = <- wc
59}
60
61func waitAll(t *TestGroup, wc chan bool) {
62 for {
63 t.Lock()
64 if len(t.running) == 0 {
65 t.Unlock()
66 break
67 }
68 runner := t.running[0]
69 t.running = t.running[1:]
70 t.Unlock()
71
72 timeout := make(chan bool, 1)
73 go func() {
74 time.Sleep(1 * time.Second)
75 timeout <- true
76 }()
77 fmt.Println("Waiting on", runner)
78 select {
79 case err := <- runner.errChan:
80 runner.errChan <- err
81 if err != nil {
82 fmt.Println("[G]", runner, err)
83 } else {
84 fmt.Println("[G]", runner, "OK")
85 }
86 case <- timeout:
87 t.Lock()
88 t.running = append(t.running, runner)
89 t.Unlock()
90 }
91 }
92 wc <- true
93}