From b55fcf3dc4c775ef35dce116ab23d35a5f9ff326 Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Sun, 25 Jan 2015 23:17:17 -0800 Subject: regtest.go tests a random stream and an offset copy --- xdelta3/go/src/xdelta/rstream.go | 56 +++++++++++++++++++++++++++++++++++ xdelta3/go/src/xdelta/test.go | 63 ++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 xdelta3/go/src/xdelta/rstream.go (limited to 'xdelta3/go/src/xdelta') diff --git a/xdelta3/go/src/xdelta/rstream.go b/xdelta3/go/src/xdelta/rstream.go new file mode 100644 index 0000000..1666934 --- /dev/null +++ b/xdelta3/go/src/xdelta/rstream.go @@ -0,0 +1,56 @@ +package xdelta + + +import ( + "io" + "math/rand" +) + +const ( + blocksize = 16380 // A factor of 7 +) + +func WriteRstreams(seed, offset, len int64, + first, second io.WriteCloser) { + go writeOne(seed, 0, len, first) + go writeOne(seed, offset, len, second) +} + +func writeOne(seed, offset, len int64, stream io.WriteCloser) { + if offset != 0 { + // Fill with other random data until the offset + writeRand(rand.New(rand.NewSource(^seed)), + offset, stream) + } + writeRand(rand.New(rand.NewSource(seed)), + len - offset, stream) + if err := stream.Close(); err != nil { + panic(err) + } +} + +func writeRand(r *rand.Rand, len int64, s io.Writer) { + blk := make([]byte, blocksize) + for len > 0 { + fillRand(r, blk) + c := blocksize + if len < blocksize { + c = int(len) + } + if _, err := s.Write(blk[0:c]); err != nil { + panic(err) + } + len -= int64(c) + } +} + +func fillRand(r *rand.Rand, blk []byte) { + for p := 0; p < blocksize; { + v := r.Int63() + for i := 7; i != 0; i-- { + blk[p] = byte(v) + p++ + v >>= 8 + } + } +} diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go index d586538..292f133 100644 --- a/xdelta3/go/src/xdelta/test.go +++ b/xdelta3/go/src/xdelta/test.go @@ -1,41 +1,58 @@ package xdelta import ( + "fmt" "io" "io/ioutil" "os" "os/exec" "path" + "sync/atomic" + "golang.org/x/sys/unix" ) var ( tmpDir = "/tmp" + srcSeq int64 ) type Program struct { Path string } +type Runner struct { + Testdir string +} + type Run struct { Cmd exec.Cmd - Testdir string Srcfile string Stdin io.WriteCloser Srcin io.WriteCloser Stdout io.ReadCloser Stderr io.ReadCloser -} +} -func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) { - var err error - run := &Run{} - if run.Testdir, err = ioutil.TempDir(tmpDir, "xrt"); err != nil { +func NewRunner() (*Runner, error) { + if dir, err := ioutil.TempDir(tmpDir, "xrt"); err != nil { return nil, err + } else { + return &Runner{dir}, nil } - args := []string{b.Path} +} + +func (r *Runner) Cleanup() { + os.RemoveAll(r.Testdir) +} + +func (r *Runner) Exec(p *Program, srcfifo bool, flags []string) (*Run, error) { + var err error + run := &Run{} + args := []string{p.Path} if srcfifo { - run.Srcfile = path.Join(run.Testdir, "source") + num := atomic.AddInt64(&srcSeq, 1) + run.Srcfile = path.Join(r.Testdir, fmt.Sprint("source", num)) if err = unix.Mkfifo(run.Srcfile, 0600); err != nil { return nil, err } @@ -44,18 +61,7 @@ func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) { read, write := io.Pipe() run.Srcin = write - go func() { - fifo, err := os.OpenFile(run.Srcfile, os.O_WRONLY, 0600) - if err != nil { - panic(err) - } - if _, err := io.Copy(fifo, read); err != nil { - panic(err) - } - if err := fifo.Close(); err != nil { - panic(err) - } - }() + go writeFifo(run.Srcfile, read) args = append(args, "-s") args = append(args, run.Srcfile) } @@ -69,10 +75,23 @@ func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) { return nil, err } - run.Cmd.Path = b.Path + run.Cmd.Path = p.Path run.Cmd.Args = append(args, flags...) - run.Cmd.Dir = run.Testdir + run.Cmd.Dir = r.Testdir run.Cmd.Start() return run, nil } + +func writeFifo(srcfile string, read io.Reader) { + fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600) + if err != nil { + panic(err) + } + if _, err := io.Copy(fifo, read); err != nil { + panic(err) + } + if err := fifo.Close(); err != nil { + panic(err) + } +} -- cgit v1.2.3