@@ -23,13 +23,11 @@ import (
23
23
"crypto/x509"
24
24
"encoding/json"
25
25
"fmt"
26
- "io"
27
26
"io/ioutil"
28
27
"net"
29
28
"net/http"
30
29
"net/http/httptest"
31
30
"net/http/httptrace"
32
- "os"
33
31
"reflect"
34
32
"strings"
35
33
"sync"
@@ -356,42 +354,12 @@ func TestTimeoutWithLogging(t *testing.T) {
356
354
res .Body .Close ()
357
355
}
358
356
359
- func captureStdErr () ( func () string , func (), error ) {
357
+ func TestErrConnKilled ( t * testing. T ) {
360
358
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 )
385
362
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 ()
395
363
handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
396
364
// this error must be ignored by the WithPanicRecovery handler
397
365
// 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) {
407
375
ts := httptest .NewServer (WithPanicRecovery (handler , resolver ))
408
376
defer ts .Close ()
409
377
410
- _ , err = http .Get (ts .URL )
378
+ _ , err : = http .Get (ts .URL )
411
379
if err == nil {
412
380
t .Fatal ("expected to receive an error" )
413
381
}
414
382
415
- capturedOutput := readStdErr ()
383
+ klog .Flush ()
384
+ klog .SetOutput (& bytes.Buffer {}) // prevent further writes into buf
385
+ capturedOutput := buf .String ()
416
386
417
387
// We don't expect stack trace from the panic to be included in the log.
418
388
if isStackTraceLoggedByRuntime (capturedOutput ) {
@@ -445,11 +415,11 @@ func (t *panicOnNonReuseTransport) GotConn(info httptrace.GotConnInfo) {
445
415
// TestErrConnKilledHTTP2 check if HTTP/2 connection is not closed when an HTTP handler panics
446
416
// The net/http library recovers the panic and sends an HTTP/2 RST_STREAM.
447
417
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
+
453
423
handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
454
424
// this error must be ignored by the WithPanicRecovery handler
455
425
// 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) {
503
473
t .Fatal ("expected to receive an error" )
504
474
}
505
475
506
- capturedOutput := readStdErr ()
476
+ klog .Flush ()
477
+ klog .SetOutput (& bytes.Buffer {}) // prevent further writes into buf
478
+ capturedOutput := buf .String ()
507
479
508
480
// We don't expect stack trace from the panic to be included in the log.
509
481
if isStackTraceLoggedByRuntime (capturedOutput ) {
0 commit comments