From b65269806e9b4c01f22614e93782ff4686eea72d Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Mon, 7 Dec 2015 22:53:32 -0800 Subject: Run offset tests up to the 31bit source window (see --- xdelta3/go/src/xdelta/rstream.go | 7 ++--- xdelta3/go/src/xdelta/run.go | 49 +++++++++++++++++++++++++++++++++++ xdelta3/go/src/xdelta/test.go | 55 ++++++++++++++++------------------------ xdelta3/go/src/xdelta/tgroup.go | 17 +++++-------- 4 files changed, 79 insertions(+), 49 deletions(-) (limited to 'xdelta3/go/src/xdelta') diff --git a/xdelta3/go/src/xdelta/rstream.go b/xdelta3/go/src/xdelta/rstream.go index 9481f22..99c3d17 100644 --- a/xdelta3/go/src/xdelta/rstream.go +++ b/xdelta3/go/src/xdelta/rstream.go @@ -3,7 +3,6 @@ package xdelta import ( "io" - "fmt" "math/rand" ) @@ -14,10 +13,10 @@ const ( func (t *TestGroup) WriteRstreams(desc string, seed, offset, len int64, src, tgt io.WriteCloser) { t.Go("src-write:"+desc, func (g *Goroutine) { - writeOne(g, seed, 0, len, src, false) + writeOne(g, seed, 0, len, tgt, false) }) t.Go("tgt-write:"+desc, func (g *Goroutine) { - writeOne(g, seed, offset, len, tgt, true) + writeOne(g, seed, offset, len, src, true) }) } @@ -30,12 +29,10 @@ func writeOne(g *Goroutine, seed, offset, len int64, stream io.WriteCloser, read } if offset != 0 { // Fill with other random data until the offset - fmt.Println(g, "pre-offset case", offset) if err := writeRand(g, rand.New(rand.NewSource(^seed)), offset, stream); err != nil { g.Panic(err) } } - fmt.Println(g, "offset case", len - offset) if err := writeRand(g, rand.New(rand.NewSource(seed)), len - offset, stream); err != nil { g.Panic(err) diff --git a/xdelta3/go/src/xdelta/run.go b/xdelta3/go/src/xdelta/run.go index f9b4185..3b07178 100644 --- a/xdelta3/go/src/xdelta/run.go +++ b/xdelta3/go/src/xdelta/run.go @@ -1,14 +1,34 @@ package xdelta import ( + "fmt" + "io" "io/ioutil" "os" + "os/exec" ) +type Program struct { + Path string +} + +type Run struct { + Cmd exec.Cmd + Srcfile string + Stdin io.WriteCloser + Srcin io.WriteCloser + Stdout io.ReadCloser + Stderr io.ReadCloser +} + type Runner struct { Testdir string } +func (r *Run) Wait() error { + return r.Cmd.Wait() +} + func NewRunner() (*Runner, error) { if dir, err := ioutil.TempDir(tmpDir, "xrt"); err != nil { return nil, err @@ -17,6 +37,35 @@ func NewRunner() (*Runner, error) { } } +func (r *Runner) newTestGroup(name string) (*TestGroup) { + tg := &TestGroup{Runner: r} + tg.WaitGroup.Add(1) + g0 := &Goroutine{tg, name, false} + tg.running = append(tg.running, g0) + tg.main = g0 + return tg +} + func (r *Runner) Cleanup() { os.RemoveAll(r.Testdir) } + +func (r *Runner) RunTest(name string, f func (t *TestGroup)) { + t := r.newTestGroup(name) + var rec interface{} + defer func() { + if r := recover(); r != nil { + fmt.Println("PANIC in ", name, ": ", r) + rec = r + } else { + // Goexit + } + }() + fmt.Println("Testing", name, "...") + f(t) + if t.errors == nil && rec == nil { + fmt.Println("Success:", name) + } else { + fmt.Println("FAILED:", name, t.errors, rec) + } +} diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go index 00f3dd7..bab66f3 100644 --- a/xdelta3/go/src/xdelta/test.go +++ b/xdelta3/go/src/xdelta/test.go @@ -8,7 +8,6 @@ import ( "io" "io/ioutil" "os" - "os/exec" "path" "sync/atomic" @@ -20,19 +19,6 @@ var ( srcSeq int64 ) -type Program struct { - Path string -} - -type Run struct { - Cmd exec.Cmd - Srcfile string - Stdin io.WriteCloser - Srcin io.WriteCloser - Stdout io.ReadCloser - Stderr io.ReadCloser -} - func (t *TestGroup) Drain(f io.ReadCloser, desc string) <-chan []byte { c := make(chan []byte) t.Go(desc, func(g *Goroutine) { @@ -71,9 +57,9 @@ func TestWrite(what string, f io.WriteCloser, b []byte) error { return nil } -func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) *Goroutine { +func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser, written *int64) *Goroutine { return t.Go("copy", func(g *Goroutine) { - _, err := io.Copy(w, r) + nwrite, err := io.Copy(w, r) if err != nil { g.Panic(err) } @@ -86,6 +72,7 @@ func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) *Goroutine { g.Panic(err) } g.OK() + *written = nwrite }) } @@ -127,12 +114,9 @@ func (t *TestGroup) Exec(desc string, p *Program, srcfifo bool, flags []string) if err = unix.Mkfifo(run.Srcfile, 0600); err != nil { return nil, err } - // Because OpenFile blocks on the Fifo until the reader - // arrives, a pipe to defer open read, write := io.Pipe() + t.writeFifo(run.Srcfile, read) run.Srcin = write - - go writeFifo(run.Srcfile, read) args = append(args, "-s") args = append(args, run.Srcfile) } @@ -156,19 +140,24 @@ func (t *TestGroup) Exec(desc string, p *Program, srcfifo bool, flags []string) return run, nil } -func (r *Run) Wait() error { - return r.Cmd.Wait() +func (t *TestGroup) Fail(v ...interface{}) { + panic(fmt.Sprintln(v...)) } -func writeFifo(srcfile string, read io.Reader) error { - fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600) - if err != nil { - fifo.Close() - return err - } - if _, err := io.Copy(fifo, read); err != nil { - fifo.Close() - return err - } - return fifo.Close() +func (t *TestGroup) writeFifo(srcfile string, read io.Reader) *Goroutine { + return t.Go("compare", func(g *Goroutine) { + fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600) + if err != nil { + fifo.Close() + g.Panic(err) + } + if _, err := io.Copy(fifo, read); err != nil { + fifo.Close() + g.Panic(err) + } + if err := fifo.Close(); err != nil { + g.Panic(err) + } + g.OK() + }) } diff --git a/xdelta3/go/src/xdelta/tgroup.go b/xdelta3/go/src/xdelta/tgroup.go index 7f6c875..0a3b41b 100644 --- a/xdelta3/go/src/xdelta/tgroup.go +++ b/xdelta3/go/src/xdelta/tgroup.go @@ -2,11 +2,13 @@ package xdelta import ( "fmt" + "runtime" "sync" ) type TestGroup struct { *Runner + main *Goroutine sync.Mutex sync.WaitGroup running []*Goroutine @@ -20,14 +22,6 @@ type Goroutine struct { done bool } -func NewTestGroup(r *Runner) (*TestGroup, *Goroutine) { - tg := &TestGroup{Runner: r} - tg.WaitGroup.Add(1) - g0 := &Goroutine{tg, "main", false} - tg.running = append(tg.running, g0) - return tg, g0 -} - func (g *Goroutine) String() string { return fmt.Sprint("[", g.name, "]") } @@ -59,9 +53,11 @@ func (g *Goroutine) OK() { func (g *Goroutine) Panic(err error) { g.finish(err) - select {} + runtime.Goexit() } +func (t *TestGroup) Main() *Goroutine { return t.main } + func (t *TestGroup) Go(name string, f func(*Goroutine)) *Goroutine { g := &Goroutine{t, name, false} t.Lock() @@ -87,7 +83,6 @@ func (t *TestGroup) Wait(self *Goroutine, procs... *Run) { fmt.Println("(ERROR)", err) } if len(t.errors) != 0 { - panic(fmt.Sprintf("Test failed with", len(t.errors), "errors")) + t.Fail("Test failed with", len(t.errors), "errors") } } - -- cgit v1.2.3