From b43d6c57dee2e02a173796c1bc11840de3681dcf Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Fri, 24 May 2024 23:41:22 +0700 Subject: [PATCH] io: add test for Pipe constructor allocations Updates #67633 Change-Id: If3da9317ba36cb8a7868db94b45c402e1793e018 Reviewed-on: https://go-review.googlesource.com/c/go/+/588219 Auto-Submit: Cuong Manh Le Auto-Submit: Ian Lance Taylor Reviewed-by: Mateusz Poliwczak Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Than McIntosh --- src/io/pipe_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/io/pipe_test.go b/src/io/pipe_test.go index a8c4e306cc..fcf94d52d2 100644 --- a/src/io/pipe_test.go +++ b/src/io/pipe_test.go @@ -421,3 +421,21 @@ func sortBytesInGroups(b []byte, n int) []byte { slices.SortFunc(groups, bytes.Compare) return bytes.Join(groups, nil) } + +var ( + rSink *PipeReader + wSink *PipeWriter +) + +func TestPipeAllocations(t *testing.T) { + numAllocs := testing.AllocsPerRun(10, func() { + rSink, wSink = Pipe() + }) + + // go.dev/cl/473535 claimed Pipe() should only do 2 allocations, + // plus the 2 escaping to heap for simulating real world usages. + expectedAllocs := 4 + if int(numAllocs) > expectedAllocs { + t.Fatalf("too many allocations for io.Pipe() call: %f", numAllocs) + } +} -- 2.48.1