mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Make util/wsstream/stream_test.go not flaky
It's hard to manage connection buffers in a deterministic test, ensure that the error is always consistent and the output is always a subset of the input
This commit is contained in:
parent
236193a26d
commit
f79c74e311
@ -110,6 +110,7 @@ func TestStreamSurvivesPanic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStreamClosedDuringRead(t *testing.T) {
|
||||
for i := 0; i < 25; i++ {
|
||||
ch := make(chan struct{})
|
||||
input := "some random text"
|
||||
errs := &errorReader{
|
||||
@ -124,15 +125,17 @@ func TestStreamClosedDuringRead(t *testing.T) {
|
||||
|
||||
data, err := readWebSocket(r, t, func(c *websocket.Conn) {
|
||||
c.Close()
|
||||
time.Sleep(time.Millisecond)
|
||||
close(ch)
|
||||
})
|
||||
if !reflect.DeepEqual(data, []byte(input)) {
|
||||
t.Errorf("unexpected server read: %v", data)
|
||||
}
|
||||
// verify that the data returned by the server on an early close always has a specific error
|
||||
if err == nil || !strings.Contains(err.Error(), "use of closed network connection") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// verify that the data returned is a strict subset of the input
|
||||
if !bytes.HasPrefix([]byte(input), data) && len(data) != 0 {
|
||||
t.Fatalf("unexpected server read: %q", string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type errorReader struct {
|
||||
|
Loading…
Reference in New Issue
Block a user