Commit 9db4dfac authored by Kirill Smelkov's avatar Kirill Smelkov

xio: Adapt Pipe code to live under xio

Just make it compile by trivially adapting the code to refer thing from
io package. Change license reference to the place where Go license lives
in go123 (LICENSE-go, added in ad78da1b "xflag: Add counting flag from
go.git internals").
parent 0bdac628
// Copyright 2009 The Go Authors. All rights reserved. // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE-go file.
// Pipe adapter to connect code expecting an io.Reader // Pipe adapter to connect code expecting an io.Reader
// with code expecting an io.Writer. // with code expecting an io.Writer.
package io package xio
import ( import (
"errors" "io"
"sync" "sync"
) )
...@@ -32,9 +32,6 @@ func (a *onceError) Load() error { ...@@ -32,9 +32,6 @@ func (a *onceError) Load() error {
return a.err return a.err
} }
// ErrClosedPipe is the error used for read or write operations on a closed pipe.
var ErrClosedPipe = errors.New("io: read/write on closed pipe")
// A pipe is the shared pipe structure underlying PipeReader and PipeWriter. // A pipe is the shared pipe structure underlying PipeReader and PipeWriter.
type pipe struct { type pipe struct {
wrMu sync.Mutex // Serializes Write operations wrMu sync.Mutex // Serializes Write operations
...@@ -69,12 +66,12 @@ func (p *pipe) readCloseError() error { ...@@ -69,12 +66,12 @@ func (p *pipe) readCloseError() error {
if werr := p.werr.Load(); rerr == nil && werr != nil { if werr := p.werr.Load(); rerr == nil && werr != nil {
return werr return werr
} }
return ErrClosedPipe return io.ErrClosedPipe
} }
func (p *pipe) CloseRead(err error) error { func (p *pipe) CloseRead(err error) error {
if err == nil { if err == nil {
err = ErrClosedPipe err = io.ErrClosedPipe
} }
p.rerr.Store(err) p.rerr.Store(err)
p.once.Do(func() { close(p.done) }) p.once.Do(func() { close(p.done) })
...@@ -108,12 +105,12 @@ func (p *pipe) writeCloseError() error { ...@@ -108,12 +105,12 @@ func (p *pipe) writeCloseError() error {
if rerr := p.rerr.Load(); werr == nil && rerr != nil { if rerr := p.rerr.Load(); werr == nil && rerr != nil {
return rerr return rerr
} }
return ErrClosedPipe return io.ErrClosedPipe
} }
func (p *pipe) CloseWrite(err error) error { func (p *pipe) CloseWrite(err error) error {
if err == nil { if err == nil {
err = EOF err = io.EOF
} }
p.werr.Store(err) p.werr.Store(err)
p.once.Do(func() { close(p.done) }) p.once.Do(func() { close(p.done) })
...@@ -135,7 +132,7 @@ func (r *PipeReader) Read(data []byte) (n int, err error) { ...@@ -135,7 +132,7 @@ func (r *PipeReader) Read(data []byte) (n int, err error) {
} }
// Close closes the reader; subsequent writes to the // Close closes the reader; subsequent writes to the
// write half of the pipe will return the error ErrClosedPipe. // write half of the pipe will return the error io.ErrClosedPipe.
func (r *PipeReader) Close() error { func (r *PipeReader) Close() error {
return r.CloseWithError(nil) return r.CloseWithError(nil)
} }
...@@ -158,7 +155,7 @@ type PipeWriter struct { ...@@ -158,7 +155,7 @@ type PipeWriter struct {
// it writes data to the pipe, blocking until one or more readers // it writes data to the pipe, blocking until one or more readers
// have consumed all the data or the read end is closed. // have consumed all the data or the read end is closed.
// If the read end is closed with an error, that err is // If the read end is closed with an error, that err is
// returned as err; otherwise err is ErrClosedPipe. // returned as err; otherwise err is io.ErrClosedPipe.
func (w *PipeWriter) Write(data []byte) (n int, err error) { func (w *PipeWriter) Write(data []byte) (n int, err error) {
return w.p.Write(data) return w.p.Write(data)
} }
......
// Copyright 2009 The Go Authors. All rights reserved. // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE-go file.
package io_test package xio_test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
. "io" "io"
. "lab.nexedi.com/kirr/go123/xio"
"sort" "sort"
"strings" "strings"
"testing" "testing"
"time" "time"
) )
func checkWrite(t *testing.T, w Writer, data []byte, c chan int) { func checkWrite(t *testing.T, w io.Writer, data []byte, c chan int) {
n, err := w.Write(data) n, err := w.Write(data)
if err != nil { if err != nil {
t.Errorf("write: %v", err) t.Errorf("write: %v", err)
...@@ -42,11 +43,11 @@ func TestPipe1(t *testing.T) { ...@@ -42,11 +43,11 @@ func TestPipe1(t *testing.T) {
w.Close() w.Close()
} }
func reader(t *testing.T, r Reader, c chan int) { func reader(t *testing.T, r io.Reader, c chan int) {
var buf = make([]byte, 64) var buf = make([]byte, 64)
for { for {
n, err := r.Read(buf) n, err := r.Read(buf)
if err == EOF { if err == io.EOF {
c <- 0 c <- 0
break break
} }
...@@ -90,7 +91,7 @@ type pipeReturn struct { ...@@ -90,7 +91,7 @@ type pipeReturn struct {
} }
// Test a large write that requires multiple reads to satisfy. // Test a large write that requires multiple reads to satisfy.
func writer(w WriteCloser, buf []byte, c chan pipeReturn) { func writer(w io.WriteCloser, buf []byte, c chan pipeReturn) {
n, err := w.Write(buf) n, err := w.Write(buf)
w.Close() w.Close()
c <- pipeReturn{n, err} c <- pipeReturn{n, err}
...@@ -108,7 +109,7 @@ func TestPipe3(t *testing.T) { ...@@ -108,7 +109,7 @@ func TestPipe3(t *testing.T) {
tot := 0 tot := 0
for n := 1; n <= 256; n *= 2 { for n := 1; n <= 256; n *= 2 {
nn, err := r.Read(rdat[tot : tot+n]) nn, err := r.Read(rdat[tot : tot+n])
if err != nil && err != EOF { if err != nil && err != io.EOF {
t.Fatalf("read: %v", err) t.Fatalf("read: %v", err)
} }
...@@ -118,7 +119,7 @@ func TestPipe3(t *testing.T) { ...@@ -118,7 +119,7 @@ func TestPipe3(t *testing.T) {
expect = 1 expect = 1
} else if n == 256 { } else if n == 256 {
expect = 0 expect = 0
if err != EOF { if err != io.EOF {
t.Fatalf("read at end: %v", err) t.Fatalf("read at end: %v", err)
} }
} }
...@@ -161,10 +162,10 @@ func (p pipeTest) String() string { ...@@ -161,10 +162,10 @@ func (p pipeTest) String() string {
var pipeTests = []pipeTest{ var pipeTests = []pipeTest{
{true, nil, false}, {true, nil, false},
{true, nil, true}, {true, nil, true},
{true, ErrShortWrite, true}, {true, io.ErrShortWrite, true},
{false, nil, false}, {false, nil, false},
{false, nil, true}, {false, nil, true},
{false, ErrShortWrite, true}, {false, io.ErrShortWrite, true},
} }
func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) { func delayClose(t *testing.T, cl closer, ch chan int, tt pipeTest) {
...@@ -195,7 +196,7 @@ func TestPipeReadClose(t *testing.T) { ...@@ -195,7 +196,7 @@ func TestPipeReadClose(t *testing.T) {
<-c <-c
want := tt.err want := tt.err
if want == nil { if want == nil {
want = EOF want = io.EOF
} }
if err != want { if err != want {
t.Errorf("read from closed pipe: %v want %v", err, want) t.Errorf("read from closed pipe: %v want %v", err, want)
...@@ -216,8 +217,8 @@ func TestPipeReadClose2(t *testing.T) { ...@@ -216,8 +217,8 @@ func TestPipeReadClose2(t *testing.T) {
go delayClose(t, r, c, pipeTest{}) go delayClose(t, r, c, pipeTest{})
n, err := r.Read(make([]byte, 64)) n, err := r.Read(make([]byte, 64))
<-c <-c
if n != 0 || err != ErrClosedPipe { if n != 0 || err != io.ErrClosedPipe {
t.Errorf("read from closed pipe: %v, %v want %v, %v", n, err, 0, ErrClosedPipe) t.Errorf("read from closed pipe: %v, %v want %v, %v", n, err, 0, io.ErrClosedPipe)
} }
} }
...@@ -232,11 +233,11 @@ func TestPipeWriteClose(t *testing.T) { ...@@ -232,11 +233,11 @@ func TestPipeWriteClose(t *testing.T) {
} else { } else {
delayClose(t, r, c, tt) delayClose(t, r, c, tt)
} }
n, err := WriteString(w, "hello, world") n, err := io.WriteString(w, "hello, world")
<-c <-c
expect := tt.err expect := tt.err
if expect == nil { if expect == nil {
expect = ErrClosedPipe expect = io.ErrClosedPipe
} }
if err != expect { if err != expect {
t.Errorf("write on closed pipe: %v want %v", err, expect) t.Errorf("write on closed pipe: %v want %v", err, expect)
...@@ -257,8 +258,8 @@ func TestPipeWriteClose2(t *testing.T) { ...@@ -257,8 +258,8 @@ func TestPipeWriteClose2(t *testing.T) {
go delayClose(t, w, c, pipeTest{}) go delayClose(t, w, c, pipeTest{})
n, err := w.Write(make([]byte, 64)) n, err := w.Write(make([]byte, 64))
<-c <-c
if n != 0 || err != ErrClosedPipe { if n != 0 || err != io.ErrClosedPipe {
t.Errorf("write to closed pipe: %v, %v want %v, %v", n, err, 0, ErrClosedPipe) t.Errorf("write to closed pipe: %v, %v want %v, %v", n, err, 0, io.ErrClosedPipe)
} }
} }
...@@ -269,7 +270,7 @@ func TestWriteEmpty(t *testing.T) { ...@@ -269,7 +270,7 @@ func TestWriteEmpty(t *testing.T) {
w.Close() w.Close()
}() }()
var b [2]byte var b [2]byte
ReadFull(r, b[0:2]) io.ReadFull(r, b[0:2])
r.Close() r.Close()
} }
...@@ -280,7 +281,7 @@ func TestWriteNil(t *testing.T) { ...@@ -280,7 +281,7 @@ func TestWriteNil(t *testing.T) {
w.Close() w.Close()
}() }()
var b [2]byte var b [2]byte
ReadFull(r, b[0:2]) io.ReadFull(r, b[0:2])
r.Close() r.Close()
} }
...@@ -301,9 +302,9 @@ func TestWriteAfterWriterClose(t *testing.T) { ...@@ -301,9 +302,9 @@ func TestWriteAfterWriterClose(t *testing.T) {
buf := make([]byte, 100) buf := make([]byte, 100)
var result string var result string
n, err := ReadFull(r, buf) n, err := io.ReadFull(r, buf)
if err != nil && err != ErrUnexpectedEOF { if err != nil && err != io.ErrUnexpectedEOF {
t.Fatalf("got: %q; want: %q", err, ErrUnexpectedEOF) t.Fatalf("got: %q; want: %q", err, io.ErrUnexpectedEOF)
} }
result = string(buf[0:n]) result = string(buf[0:n])
<-done <-done
...@@ -311,8 +312,8 @@ func TestWriteAfterWriterClose(t *testing.T) { ...@@ -311,8 +312,8 @@ func TestWriteAfterWriterClose(t *testing.T) {
if result != "hello" { if result != "hello" {
t.Errorf("got: %q; want: %q", result, "hello") t.Errorf("got: %q; want: %q", result, "hello")
} }
if writeErr != ErrClosedPipe { if writeErr != io.ErrClosedPipe {
t.Errorf("got: %q; want: %q", writeErr, ErrClosedPipe) t.Errorf("got: %q; want: %q", writeErr, io.ErrClosedPipe)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment