Skip to content

Commit 79150d1

Browse files
committed
add unit tests to verify the fix
1 parent 42d3716 commit 79150d1

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package remotecommand
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"io"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
"go.uber.org/goleak"
12+
"k8s.io/client-go/tools/remotecommand"
13+
)
14+
15+
func TestHandleResizeEvents(t *testing.T) {
16+
var testTerminalSize remotecommand.TerminalSize
17+
rawTerminalSize, err := json.Marshal(&testTerminalSize)
18+
require.NoError(t, err)
19+
20+
testCases := []struct {
21+
name string
22+
resizeStreamData []byte
23+
cancelContext bool
24+
readFromChannel bool
25+
}{
26+
{
27+
name: "data attempted to be sent on the channel; channel not read; context canceled",
28+
resizeStreamData: rawTerminalSize,
29+
cancelContext: true,
30+
},
31+
{
32+
name: "data attempted to be sent on the channel; channel read; context not canceled",
33+
resizeStreamData: rawTerminalSize,
34+
readFromChannel: true,
35+
},
36+
{
37+
name: "no data attempted to be sent on the channel; context canceled",
38+
cancelContext: true,
39+
},
40+
{
41+
name: "no data attempted to be sent on the channel; context not canceled",
42+
},
43+
}
44+
for _, testCase := range testCases {
45+
t.Run(testCase.name, func(t *testing.T) {
46+
ctx, cancel := context.WithCancel(context.TODO())
47+
connCtx := connectionContext{
48+
resizeStream: io.NopCloser(bytes.NewReader(testCase.resizeStreamData)),
49+
resizeChan: make(chan remotecommand.TerminalSize),
50+
}
51+
52+
go handleResizeEvents(ctx, connCtx.resizeStream, connCtx.resizeChan)
53+
if testCase.readFromChannel {
54+
gotTerminalSize := <-connCtx.resizeChan
55+
require.Equal(t, gotTerminalSize, testTerminalSize)
56+
}
57+
if testCase.cancelContext {
58+
cancel()
59+
}
60+
61+
goleak.VerifyNone(t)
62+
cancel()
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)