portforward: Add unit test to cover stopChan usage

This commit is contained in:
Brian Pursley 2022-12-23 10:11:21 -05:00
parent a9f0410385
commit 6f08ab013c

View File

@ -579,7 +579,7 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) {
pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr) pf, err := New(dialer, []string{":5000"}, stopChan, readyChan, os.Stdout, os.Stderr)
if err != nil { if err != nil {
t.Fatalf("faile to create new PortForwarder: %s", err) t.Fatalf("failed to create new PortForwarder: %s", err)
} }
go func() { go func() {
@ -588,6 +588,7 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) {
<-pf.Ready <-pf.Ready
// Simulate lost pod connection by closing streamConn, which should result in pf.ForwardPorts() returning an error.
pf.streamConn.Close() pf.streamConn.Close()
err = <-errChan err = <-errChan
@ -597,3 +598,33 @@ func TestForwardPortsReturnsErrorWhenConnectionIsLost(t *testing.T) {
t.Fatalf("unexpected error from pf.ForwardPorts(): %s", err) 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)
}
}