diff --git a/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go b/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go index 070c8904351..3c90a3fde51 100644 --- a/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go +++ b/staging/src/k8s.io/client-go/tools/portforward/portforward_test.go @@ -579,7 +579,7 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) { pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr) if err != nil { - t.Fatalf("faile to create new PortForwarder: %s", err) + t.Fatalf("failed to create new PortForwarder: %s", err) } go func() { @@ -588,6 +588,7 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) { <-pf.Ready + // Simulate lost pod connection by closing streamConn, which should result in pf.ForwardPorts() returning an error. pf.streamConn.Close() err = <-errChan @@ -597,3 +598,33 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) { t.Fatalf("unexpected error from pf.ForwardPorts(): %s", err) } } + +func TestForwardPortsReturnsNilWhenStopChanIsClosed(t *testing.T) { + dialer := &fakeDialer{ + conn: newFakeConnection(), + } + + stopChan := make(chan struct{}) + readyChan := make(chan struct{}) + errChan := make(chan error) + + pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr) + if err != nil { + t.Fatalf("failed to create new PortForwarder: %s", err) + } + + go func() { + errChan <- pf.ForwardPorts() + }() + + <-pf.Ready + + // Closing (or sending to) stopChan indicates a stop request by the caller, which should result in pf.ForwardPorts() + // returning nil. + close(stopChan) + + err = <-errChan + if err != nil { + t.Fatalf("unexpected error from pf.ForwardPorts(): %s", err) + } +}