@@ -20,7 +20,12 @@ import (
20
20
"bytes"
21
21
"errors"
22
22
"io"
23
+ "net/http"
24
+ "net/http/httptest"
23
25
"testing"
26
+ "time"
27
+
28
+ netutil "k8s.io/apimachinery/pkg/util/net"
24
29
)
25
30
26
31
func TestRead (t * testing.T ) {
@@ -98,6 +103,7 @@ func TestReadLarge(t *testing.T) {
98
103
t .Fatalf ("unexpected: %v %d" , err , n )
99
104
}
100
105
}
106
+
101
107
func TestReadInvalidFrame (t * testing.T ) {
102
108
data := []byte {
103
109
0x00 , 0x00 , 0x00 , 0x04 ,
@@ -120,6 +126,46 @@ func TestReadInvalidFrame(t *testing.T) {
120
126
}
121
127
}
122
128
129
+ func TestReadClientTimeout (t * testing.T ) {
130
+ header := []byte {
131
+ 0x00 , 0x00 , 0x00 , 0x04 ,
132
+ }
133
+ data := []byte {
134
+ 0x01 , 0x02 , 0x03 , 0x04 ,
135
+ 0x00 , 0x00 , 0x00 , 0x03 ,
136
+ 0x05 , 0x06 , 0x07 ,
137
+ 0x00 , 0x00 , 0x00 , 0x00 ,
138
+ 0x00 , 0x00 , 0x00 , 0x01 ,
139
+ 0x08 ,
140
+ }
141
+
142
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143
+ w .WriteHeader (http .StatusOK )
144
+ _ , _ = w .Write (header )
145
+ if flusher , ok := w .(http.Flusher ); ok {
146
+ flusher .Flush ()
147
+ }
148
+ time .Sleep (1 * time .Second )
149
+ _ , _ = w .Write (data )
150
+ }))
151
+ defer server .Close ()
152
+
153
+ client := & http.Client {
154
+ Timeout : 500 * time .Millisecond ,
155
+ }
156
+
157
+ resp , err := client .Get (server .URL )
158
+ if err != nil {
159
+ t .Fatalf ("unexpected: %v" , err )
160
+ }
161
+
162
+ r := NewLengthDelimitedFrameReader (resp .Body )
163
+ buf := make ([]byte , 1 )
164
+ if n , err := r .Read (buf ); err == nil || ! netutil .IsTimeout (err ) {
165
+ t .Fatalf ("unexpected: %v %d" , err , n )
166
+ }
167
+ }
168
+
123
169
func TestJSONFrameReader (t * testing.T ) {
124
170
b := bytes .NewBufferString ("{\" test\" :true}\n 1\n [\" a\" ]" )
125
171
r := NewJSONFramedReader (io .NopCloser (b ))
0 commit comments