@@ -42,9 +42,10 @@ func TestServeData_HTTP(t *testing.T) {
42
42
stopCh : stopCh ,
43
43
}
44
44
testClient := & Client {
45
- connManager : newConnectionManager (),
46
- stopCh : stopCh ,
47
- cs : cs ,
45
+ connManager : newConnectionManager (),
46
+ stopCh : stopCh ,
47
+ cs : cs ,
48
+ readBlockInterval : 15 * time .Second ,
48
49
}
49
50
testClient .stream , stream = pipe ()
50
51
@@ -133,7 +134,8 @@ func TestServeData_HTTP(t *testing.T) {
133
134
waitForConnectionDeletion (t , testClient , connID )
134
135
}
135
136
136
- func TestClose_Client (t * testing.T ) {
137
+ func TestDelayedServedData_HTTP (t * testing.T ) {
138
+ var err error
137
139
var stream agent.AgentService_ConnectClient
138
140
stopCh := make (chan struct {})
139
141
cs := & ClientSet {
@@ -144,6 +146,113 @@ func TestClose_Client(t *testing.T) {
144
146
connManager : newConnectionManager (),
145
147
stopCh : stopCh ,
146
148
cs : cs ,
149
+ // Set the readBlockInterval to a short value to check if
150
+ // the agent can handle the SetReadDeadline.
151
+ readBlockInterval : 1 * time .Second ,
152
+ }
153
+ testClient .stream , stream = pipe ()
154
+
155
+ // Start agent
156
+ go testClient .Serve ()
157
+ defer close (stopCh )
158
+
159
+ // Start test http server as remote service
160
+ expectedBody := "Hello, client"
161
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
162
+ time .Sleep (4 * time .Second ) // HTTPTest times out after 5 seconds.
163
+ fmt .Fprint (w , expectedBody )
164
+ }))
165
+ defer ts .Close ()
166
+
167
+ // Simulate sending KAS DIAL_REQ to (Agent) Client
168
+ dialPacket := newDialPacket ("tcp" , ts .URL [len ("http://" ):], 111 )
169
+ err = stream .Send (dialPacket )
170
+ if err != nil {
171
+ t .Fatal (err .Error ())
172
+ }
173
+
174
+ // Expect receiving DIAL_RSP packet from (Agent) Client
175
+ pkt , err := stream .Recv ()
176
+ if err != nil {
177
+ t .Fatal (err .Error ())
178
+ }
179
+ if pkt == nil {
180
+ t .Fatal ("unexpected nil packet" )
181
+ }
182
+ if pkt .Type != client .PacketType_DIAL_RSP {
183
+ t .Errorf ("expect PacketType_DIAL_RSP; got %v" , pkt .Type )
184
+ }
185
+ dialRsp := pkt .Payload .(* client.Packet_DialResponse )
186
+ connID := dialRsp .DialResponse .ConnectID
187
+ if dialRsp .DialResponse .Random != 111 {
188
+ t .Errorf ("expect random=111; got %v" , dialRsp .DialResponse .Random )
189
+ }
190
+
191
+ // Send Data (HTTP Request) via (Agent) Client to the test http server
192
+ t .Logf ("Sending data packet at %v" , time .Now ())
193
+ dataPacket := newDataPacket (connID , []byte ("GET / HTTP/1.1\r \n Host: localhost\r \n \r \n " ))
194
+ err = stream .Send (dataPacket )
195
+ if err != nil {
196
+ t .Error (err .Error ())
197
+ }
198
+ t .Logf ("Sent data packet at %v" , time .Now ())
199
+
200
+ // Expect receiving http response via (Agent) Client
201
+ t .Logf ("Receiving http response at %v" , time .Now ())
202
+ pkt , _ = stream .Recv ()
203
+ if pkt == nil {
204
+ t .Fatalf ("unexpected nil packet at %v" , time .Now ())
205
+ }
206
+ if pkt .Type != client .PacketType_DATA {
207
+ t .Errorf ("expect PacketType_DATA; got %v" , pkt .Type )
208
+ }
209
+ data := pkt .Payload .(* client.Packet_Data ).Data .Data
210
+
211
+ // Verify response data
212
+ //
213
+ // HTTP/1.1 200 OK\r\n
214
+ // Date: Tue, 07 May 2019 06:44:57 GMT\r\n
215
+ // Content-Length: 14\r\n
216
+ // Content-Type: text/plain; charset=utf-8\r\n
217
+ // \r\n
218
+ // Hello, client
219
+ headAndBody := strings .Split (string (data ), "\r \n " )
220
+ if body := headAndBody [len (headAndBody )- 1 ]; body != expectedBody {
221
+ t .Errorf ("expect body %v; got %v" , expectedBody , body )
222
+ }
223
+
224
+ // Force close the test server which will cause remote connection gets droped
225
+ ts .Close ()
226
+
227
+ // Verify receiving CLOSE_RSP
228
+ pkt , _ = stream .Recv ()
229
+ if pkt == nil {
230
+ t .Fatal ("unexpected nil packet" )
231
+ }
232
+ if pkt .Type != client .PacketType_CLOSE_RSP {
233
+ t .Errorf ("expect PacketType_CLOSE_RSP; got %v" , pkt .Type )
234
+ }
235
+ closeErr := pkt .Payload .(* client.Packet_CloseResponse ).CloseResponse .Error
236
+ if closeErr != "" {
237
+ t .Errorf ("expect nil closeErr; got %v" , closeErr )
238
+ }
239
+
240
+ // Verify internal state is consistent
241
+ waitForConnectionDeletion (t , testClient , connID )
242
+ }
243
+
244
+ func TestClose_Client (t * testing.T ) {
245
+ var stream agent.AgentService_ConnectClient
246
+ stopCh := make (chan struct {})
247
+ cs := & ClientSet {
248
+ clients : make (map [string ]* Client ),
249
+ stopCh : stopCh ,
250
+ }
251
+ testClient := & Client {
252
+ connManager : newConnectionManager (),
253
+ stopCh : stopCh ,
254
+ cs : cs ,
255
+ readBlockInterval : 15 * time .Second ,
147
256
}
148
257
testClient .stream , stream = pipe ()
149
258
@@ -229,9 +338,10 @@ func TestConnectionMismatch(t *testing.T) {
229
338
stopCh : stopCh ,
230
339
}
231
340
testClient := & Client {
232
- connManager : newConnectionManager (),
233
- stopCh : stopCh ,
234
- cs : cs ,
341
+ connManager : newConnectionManager (),
342
+ stopCh : stopCh ,
343
+ cs : cs ,
344
+ readBlockInterval : 15 * time .Second ,
235
345
}
236
346
testClient .stream , stream = pipe ()
237
347
@@ -291,9 +401,10 @@ func TestFailedSend_DialResp_GRPC(t *testing.T) {
291
401
stopCh : stopCh ,
292
402
}
293
403
testClient := & Client {
294
- connManager : newConnectionManager (),
295
- stopCh : stopCh ,
296
- cs : cs ,
404
+ connManager : newConnectionManager (),
405
+ stopCh : stopCh ,
406
+ cs : cs ,
407
+ readBlockInterval : 15 * time .Second ,
297
408
}
298
409
defer func () {
299
410
close (stopCh )
@@ -353,10 +464,11 @@ func TestDrain(t *testing.T) {
353
464
stopCh : stopCh ,
354
465
}
355
466
testClient := & Client {
356
- connManager : newConnectionManager (),
357
- drainCh : drainCh ,
358
- stopCh : stopCh ,
359
- cs : cs ,
467
+ connManager : newConnectionManager (),
468
+ drainCh : drainCh ,
469
+ stopCh : stopCh ,
470
+ cs : cs ,
471
+ readBlockInterval : 15 * time .Second ,
360
472
}
361
473
testClient .stream , stream = pipe ()
362
474
0 commit comments