Skip to content

Commit afe1947

Browse files
znullCopilot
andcommitted
Add test for ReaderFrom hidden by nopWriteCloser
When using WithStdout, the writer is wrapped in nopWriteCloser, which hides the ReaderFrom interface of the underlying writer. This prevents io.CopyBuffer from dispatching to ReadFrom for potential zero-copy paths (e.g., when the destination is a network connection or has a custom ReadFrom implementation). This test currently FAILS, demonstrating the problem. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c5839cb commit afe1947

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

pipe/iocopier_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package pipe
22

33
import (
44
"bytes"
5+
"io"
56
"os"
67
"runtime"
8+
"sync/atomic"
79
"testing"
810
)
911

@@ -73,3 +75,47 @@ func TestIOCopierPoolBufferUsed(t *testing.T) {
7375
allocBytes, maxBytes)
7476
}
7577
}
78+
79+
// readFromWriter is a test writer that implements io.ReaderFrom and
80+
// records whether ReadFrom was called.
81+
type readFromWriter struct {
82+
bytes.Buffer
83+
readFromCalled atomic.Bool
84+
}
85+
86+
func (w *readFromWriter) ReadFrom(r io.Reader) (int64, error) {
87+
w.readFromCalled.Store(true)
88+
return w.Buffer.ReadFrom(r)
89+
}
90+
91+
func (w *readFromWriter) Close() error { return nil }
92+
93+
// TestIOCopierUsesReadFrom verifies that ioCopier dispatches to
94+
// ReaderFrom when the destination writer supports it, even when
95+
// wrapped in nopWriteCloser (as happens with WithStdout).
96+
func TestIOCopierUsesReadFrom(t *testing.T) {
97+
const payload = "hello readfrom\n"
98+
99+
pr, pw, err := os.Pipe()
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
go func() {
104+
pw.Write([]byte(payload))
105+
pw.Close()
106+
}()
107+
108+
w := &readFromWriter{}
109+
c := newIOCopier(nopWriteCloser{w})
110+
c.Start(nil, Env{}, pr)
111+
c.Wait()
112+
113+
if w.Buffer.String() != payload {
114+
t.Fatalf("unexpected output: %q", w.Buffer.String())
115+
}
116+
117+
if !w.readFromCalled.Load() {
118+
t.Error("ioCopier did not call ReadFrom on destination; " +
119+
"nopWriteCloser may be hiding the ReaderFrom interface")
120+
}
121+
}

0 commit comments

Comments
 (0)