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.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/xdelta3/go/src/xdelta/tgroup.go b/xdelta3/go/src/xdelta/tgroup.go
index c7337c6..1852bac 100644
--- a/xdelta3/go/src/xdelta/tgroup.go
+++ b/xdelta3/go/src/xdelta/tgroup.go
@@ -2,6 +2,7 @@ package xdelta
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
5 "io"
5 "sync" 6 "sync"
6 "time" 7 "time"
7) 8)
@@ -109,3 +110,53 @@ func waitAll(t *TestGroup, wc chan bool) {
109 } 110 }
110 wc <- true 111 wc <- true
111} 112}
113
114type dualWriter struct {
115 e, d chan []byte
116}
117
118func (d *dualWriter) Write(p []byte) (int, error) {
119 if len(p) != 0 {
120 d.e <- p
121 d.d <- p
122 }
123 return len(p), nil
124}
125
126func (d *dualWriter) Close() error {
127 d.e <- nil
128 d.d <- nil
129 _ = <- d.e
130 _ = <- d.d
131 return nil
132}
133
134func newWriter(c chan []byte, a io.WriteCloser) func (Goroutine) {
135 return func (g Goroutine) {
136 for {
137 d := <- c
138 if d == nil {
139 if err := a.Close(); err != nil {
140 g.Panic(err)
141 }
142 c <- nil
143 g.OK()
144 return
145 }
146 if num, err := a.Write(d); err != nil {
147 g.Panic(err)
148 } else if num != len(d) {
149 g.Panic(fmt.Errorf("Invalid write: %v != %v", num, len(d)))
150 }
151 }
152 }
153}
154
155func (t *TestGroup) NewDualWriter(a1, a2 io.WriteCloser) io.WriteCloser {
156 c1 := make(chan []byte)
157 c2 := make(chan []byte)
158 r := &dualWriter{c1, c2}
159 t.Go("writer0", newWriter(c1, a1))
160 t.Go("writer1", newWriter(c2, a2))
161 return r
162}