mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
portforward: Add unit test to cover stopChan usage
This commit is contained in:
parent
a9f0410385
commit
6f08ab013c
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user