Skip to content

Commit fac0c78

Browse files
Merge pull request #109257 from kerthcet/automated-cherry-pick-of-#108553-upstream-release-1.23
Automated cherry pick of #108553: fix: race detected in TestErrConnKilled Kubernetes-commit: 6f4763eafb5e2542a3ea55d6fb1fe40513404ff3
2 parents 0ed21f9 + 628bafc commit fac0c78

File tree

1 file changed

+16
-44
lines changed

1 file changed

+16
-44
lines changed

pkg/server/filters/timeout_test.go

+16-44
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ import (
2323
"crypto/x509"
2424
"encoding/json"
2525
"fmt"
26-
"io"
2726
"io/ioutil"
2827
"net"
2928
"net/http"
3029
"net/http/httptest"
3130
"net/http/httptrace"
32-
"os"
3331
"reflect"
3432
"strings"
3533
"sync"
@@ -356,42 +354,12 @@ func TestTimeoutWithLogging(t *testing.T) {
356354
res.Body.Close()
357355
}
358356

359-
func captureStdErr() (func() string, func(), error) {
357+
func TestErrConnKilled(t *testing.T) {
360358
var buf bytes.Buffer
361-
reader, writer, err := os.Pipe()
362-
if err != nil {
363-
return nil, nil, err
364-
}
365-
stderr := os.Stderr
366-
readerClosedCh := make(chan struct{})
367-
stopReadingStdErr := func() string {
368-
writer.Close()
369-
<-readerClosedCh
370-
return buf.String()
371-
}
372-
klog.LogToStderr(true)
373-
cleanUp := func() {
374-
os.Stderr = stderr
375-
klog.LogToStderr(false)
376-
stopReadingStdErr()
377-
}
378-
os.Stderr = writer
379-
go func() {
380-
io.Copy(&buf, reader)
381-
readerClosedCh <- struct{}{}
382-
close(readerClosedCh)
383-
}()
384-
klog.LogToStderr(true)
359+
klog.SetOutput(&buf)
360+
klog.LogToStderr(false)
361+
defer klog.LogToStderr(true)
385362

386-
return stopReadingStdErr, cleanUp, nil
387-
}
388-
389-
func TestErrConnKilled(t *testing.T) {
390-
readStdErr, cleanUp, err := captureStdErr()
391-
if err != nil {
392-
t.Fatalf("unable to setup the test, err %v", err)
393-
}
394-
defer cleanUp()
395363
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
396364
// this error must be ignored by the WithPanicRecovery handler
397365
// it is thrown by WithTimeoutForNonLongRunningRequests handler when a response has been already sent to the client and the handler timed out
@@ -407,12 +375,14 @@ func TestErrConnKilled(t *testing.T) {
407375
ts := httptest.NewServer(WithPanicRecovery(handler, resolver))
408376
defer ts.Close()
409377

410-
_, err = http.Get(ts.URL)
378+
_, err := http.Get(ts.URL)
411379
if err == nil {
412380
t.Fatal("expected to receive an error")
413381
}
414382

415-
capturedOutput := readStdErr()
383+
klog.Flush()
384+
klog.SetOutput(&bytes.Buffer{}) // prevent further writes into buf
385+
capturedOutput := buf.String()
416386

417387
// We don't expect stack trace from the panic to be included in the log.
418388
if isStackTraceLoggedByRuntime(capturedOutput) {
@@ -445,11 +415,11 @@ func (t *panicOnNonReuseTransport) GotConn(info httptrace.GotConnInfo) {
445415
// TestErrConnKilledHTTP2 check if HTTP/2 connection is not closed when an HTTP handler panics
446416
// The net/http library recovers the panic and sends an HTTP/2 RST_STREAM.
447417
func TestErrConnKilledHTTP2(t *testing.T) {
448-
readStdErr, cleanUp, err := captureStdErr()
449-
if err != nil {
450-
t.Fatalf("unable to setup the test, err %v", err)
451-
}
452-
defer cleanUp()
418+
var buf bytes.Buffer
419+
klog.SetOutput(&buf)
420+
klog.LogToStderr(false)
421+
defer klog.LogToStderr(true)
422+
453423
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
454424
// this error must be ignored by the WithPanicRecovery handler
455425
// it is thrown by WithTimeoutForNonLongRunningRequests handler when a response has been already sent to the client and the handler timed out
@@ -503,7 +473,9 @@ func TestErrConnKilledHTTP2(t *testing.T) {
503473
t.Fatal("expected to receive an error")
504474
}
505475

506-
capturedOutput := readStdErr()
476+
klog.Flush()
477+
klog.SetOutput(&bytes.Buffer{}) // prevent further writes into buf
478+
capturedOutput := buf.String()
507479

508480
// We don't expect stack trace from the panic to be included in the log.
509481
if isStackTraceLoggedByRuntime(capturedOutput) {

0 commit comments

Comments
 (0)