summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta/tgroup.go
blob: 1852baca6b7582e12547ad422366625f729dbc12 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package xdelta

import (
	"fmt"
	"io"
	"sync"
	"time"
)

type TestGroup struct {
	*Runner
	sync.Mutex
	running []Goroutine
	errors []error
	waitChan <-chan bool
}

type Goroutine struct {
	name string
	errChan chan error
}

func NewTestGroup(r *Runner) (*TestGroup, Goroutine) {
	g := Goroutine{"main", make(chan error)}
	wc := make(chan bool)
	tg := &TestGroup{Runner: r, running: []Goroutine{g}, waitChan: wc}
	go waitAll(tg, wc)
	return tg, g
}

func (g *Goroutine) String() string {
	return fmt.Sprint("[", g.name, "]")
}

func (g *Goroutine) OK() {
	if g.errChan != nil {
		g.errChan <- nil
		_ = <- g.errChan
		g.errChan = nil
	}
}

func (g *Goroutine) Panic(err error) {
	fmt.Println(g, err)
	if g.errChan != nil {
		g.errChan <- err
		_ = <- g.errChan
	}
	select {}
}

func (t *TestGroup) Go(name string, f func(Goroutine)) Goroutine {
	g := Goroutine{name, make(chan error)}
	t.Lock()
	t.running = append(t.running, g)
	t.Unlock()
	go f(g)
	return g
}

func (t *TestGroup) Wait(self Goroutine, procs... *Run) {
	self.OK()
	t.Lock()
	wc := t.waitChan
	t.waitChan = nil
	t.Unlock()
	_ = <- wc
	t.Lock()
	errs := t.errors
	t.Unlock()
	for _, p := range procs {
		if err := p.Wait(); err != nil {
			errs = append(errs, err)
		}
	}
	if len(errs) != 0 {
		for _, err := range errs {
			fmt.Println(err)
		}
		panic(fmt.Sprint(len(errs), " errors"))
	}
}

func waitAll(t *TestGroup, wc chan bool) {
	for {
		t.Lock()
		if len(t.running) == 0 {
			t.Unlock()
			break
		}
		runner := t.running[0]
		t.running = t.running[1:]
		t.Unlock()

		timeout := time.After(time.Second)

		select {
		case err := <- runner.errChan:
			runner.errChan <- err
			if err != nil {
				t.Lock()
				t.errors = append(t.errors, err)
				t.Unlock()
			}
		case <- timeout:
			t.Lock()
			t.running = append(t.running, runner)
			t.Unlock()
		}
	}
	wc <- true
}

type dualWriter struct {
	e, d chan []byte
}

func (d *dualWriter) Write(p []byte) (int, error) {
	if len(p) != 0 {
		d.e <- p
		d.d <- p
	}
	return len(p), nil
}

func (d *dualWriter) Close() error {
	d.e <- nil
	d.d <- nil
	_ = <- d.e
	_ = <- d.d
	return nil
}

func newWriter(c chan []byte, a io.WriteCloser) func (Goroutine) {
	return func (g Goroutine) {
		for {
			d := <- c
			if d == nil {
				if err := a.Close(); err != nil {
					g.Panic(err)
				}
				c <- nil
				g.OK()
				return
			}
			if num, err := a.Write(d); err != nil {
				g.Panic(err)
			} else if num != len(d) {
				g.Panic(fmt.Errorf("Invalid write: %v != %v", num, len(d)))
			}
		}
	}
}

func (t *TestGroup) NewDualWriter(a1, a2 io.WriteCloser) io.WriteCloser {
	c1 := make(chan []byte)
	c2 := make(chan []byte)
	r := &dualWriter{c1, c2}
	t.Go("writer0", newWriter(c1, a1))
	t.Go("writer1", newWriter(c2, a2))
	return r
}