/
flow_control.rs
1799 lines (1503 loc) · 59.8 KB
/
flow_control.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use futures::future::{join, join4};
use futures::{StreamExt, TryStreamExt};
use h2_support::prelude::*;
use h2_support::util::yield_once;
// In this case, the stream & connection both have capacity, but capacity is not
// explicitly requested.
#[tokio::test]
async fn send_data_without_requesting_capacity() {
h2_support::trace_init!();
let payload = vec![0; 1024];
let mock = mock_io::Builder::new()
.handshake()
.write(&[
// POST /
0, 0, 16, 1, 4, 0, 0, 0, 1, 131, 135, 65, 139, 157, 41, 172, 75, 143, 168, 233, 25, 151,
33, 233, 132,
])
.write(&[
// DATA
0, 4, 0, 0, 1, 0, 0, 0, 1,
])
.write(&payload[..])
.write(frames::SETTINGS_ACK)
// Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89])
.build();
let (mut client, mut h2) = client::handshake(mock).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
// The capacity should be immediately allocated
assert_eq!(stream.capacity(), 0);
// Send the data
stream.send_data(payload.into(), true).unwrap();
// Get the response
let resp = h2.run(response).await.unwrap();
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
h2.await.unwrap();
}
#[tokio::test]
async fn release_capacity_sends_window_update() {
h2_support::trace_init!();
let payload = vec![0u8; 16_384];
let payload_len = payload.len();
let (io, mut srv) = mock::new();
let mock = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.send_frame(frames::data(1, &payload[..])).await;
srv.send_frame(frames::data(1, &payload[..])).await;
srv.send_frame(frames::data(1, &payload[..])).await;
srv.recv_frame(frames::window_update(0, 32_768)).await;
srv.recv_frame(frames::window_update(1, 32_768)).await;
srv.send_frame(frames::data(1, &payload[..]).eos()).await;
};
let h2 = async move {
let (mut client, h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::GET)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let req = async move {
let resp = client.send_request(request, true).unwrap().0.await.unwrap();
// Get the response
assert_eq!(resp.status(), StatusCode::OK);
let mut body = resp.into_parts().1;
// read some body to use up window size to below half
let buf = body.data().await.unwrap().unwrap();
assert_eq!(buf.len(), payload_len);
let buf = body.data().await.unwrap().unwrap();
assert_eq!(buf.len(), payload_len);
let buf = body.data().await.unwrap().unwrap();
assert_eq!(buf.len(), payload_len);
body.flow_control().release_capacity(buf.len() * 2).unwrap();
let buf = body.data().await.unwrap().unwrap();
assert_eq!(buf.len(), payload_len);
};
join(
async move {
h2.await.unwrap();
},
req,
)
.await
};
join(mock, h2).await;
}
#[tokio::test]
async fn release_capacity_of_small_amount_does_not_send_window_update() {
h2_support::trace_init!();
let payload = [0; 16];
let (io, mut srv) = mock::new();
let mock = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.send_frame(frames::data(1, &payload[..]).eos()).await;
};
let h2 = async move {
let (mut client, h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::GET)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let req = async move {
let resp = client.send_request(request, true).unwrap().0.await.unwrap();
// Get the response
assert_eq!(resp.status(), StatusCode::OK);
let mut body = resp.into_parts().1;
assert!(!body.is_end_stream());
let buf = body.data().await.unwrap().unwrap();
// read the small body and then release it
assert_eq!(buf.len(), 16);
body.flow_control().release_capacity(buf.len()).unwrap();
let buf = body.data().await;
assert!(buf.is_none());
};
join(async move { h2.await.unwrap() }, req).await;
};
join(mock, h2).await;
}
#[test]
#[ignore]
fn expand_window_sends_window_update() {}
#[test]
#[ignore]
fn expand_window_calls_are_coalesced() {}
#[tokio::test]
async fn recv_data_overflows_connection_window() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let mock = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
// fill the whole window
srv.send_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.send_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.send_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.send_frame(frames::data(1, vec![0u8; 16_383])).await;
// this frame overflows the window!
srv.send_frame(frames::data(1, vec![0u8; 128]).eos()).await;
// expecting goaway for the conn, not stream
srv.recv_frame(frames::go_away(0).flow_control()).await;
// connection is ended by client
};
let h2 = async move {
let (mut client, h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::GET)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let req = async move {
let resp = client.send_request(request, true).unwrap().0.await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = resp.into_parts().1;
let res = util::concat(body).await;
let err = res.unwrap_err();
assert_eq!(
err.to_string(),
"connection error detected: flow-control protocol violated"
);
};
// client should see a flow control error
let conn = async move {
let res = h2.await;
let err = res.unwrap_err();
assert_eq!(
err.to_string(),
"connection error detected: flow-control protocol violated"
);
};
join(conn, req).await;
};
join(mock, h2).await;
}
#[tokio::test]
async fn recv_data_overflows_stream_window() {
// this tests for when streams have smaller windows than their connection
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let mock = async move {
let _ = srv.assert_client_handshake().await;
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
// fill the whole window
srv.send_frame(frames::data(1, vec![0u8; 16_384])).await;
// this frame overflows the window!
srv.send_frame(frames::data(1, &[0; 16][..]).eos()).await;
srv.recv_frame(frames::reset(1).flow_control()).await;
};
let h2 = async move {
let (mut client, conn) = client::Builder::new()
.initial_window_size(16_384)
.handshake::<_, Bytes>(io)
.await
.unwrap();
let request = Request::builder()
.method(Method::GET)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let req = async move {
let resp = client.send_request(request, true).unwrap().0.await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = resp.into_parts().1;
let res = util::concat(body).await;
let err = res.unwrap_err();
assert_eq!(
err.to_string(),
"stream error detected: flow-control protocol violated"
);
};
join(async move { conn.await.unwrap() }, req).await;
};
join(mock, h2).await;
}
#[test]
#[ignore]
fn recv_window_update_causes_overflow() {
// A received window update causes the window to overflow.
}
#[tokio::test]
async fn stream_error_release_connection_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
// we're sending the wrong content-length
srv.send_frame(
frames::headers(1)
.response(200)
.field("content-length", &*(16_384 * 3).to_string()),
)
.await;
srv.send_frame(frames::data(1, vec![0; 16_384])).await;
srv.send_frame(frames::data(1, vec![0; 16_384])).await;
srv.send_frame(frames::data(1, vec![0; 10]).eos()).await;
// mismatched content-length is a protocol error
srv.recv_frame(frames::reset(1).protocol_error()).await;
// but then the capacity should be released automatically
srv.recv_frame(frames::window_update(0, 16_384 * 2 + 10))
.await;
};
let client = async move {
let (mut client, mut conn) = client::handshake(io).await.unwrap();
let request = Request::builder()
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let req = async {
let resp = client
.send_request(request, true)
.unwrap()
.0
.await
.expect("response");
assert_eq!(resp.status(), StatusCode::OK);
let mut body = resp.into_parts().1;
let mut cap = body.flow_control().clone();
let to_release = 16_384 * 2;
let mut should_recv_bytes = to_release;
let mut should_recv_frames = 2usize;
let err = body
.try_for_each(|bytes| async move {
should_recv_bytes -= bytes.len();
should_recv_frames -= 1;
if should_recv_bytes == 0 {
assert_eq!(should_recv_frames, 0);
}
Ok(())
})
.await
.expect_err("body");
assert_eq!(
err.to_string(),
"stream error detected: unspecific protocol error detected"
);
cap.release_capacity(to_release).expect("release_capacity");
};
conn.drive(req).await;
conn.await.expect("client");
};
join(srv, client).await;
}
#[tokio::test]
async fn stream_close_by_data_frame_releases_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let window_size = frame::DEFAULT_INITIAL_WINDOW_SIZE as usize;
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
// Send request
let (resp1, mut s1) = client.send_request(request, false).unwrap();
// This effectively reserves the entire connection window
s1.reserve_capacity(window_size);
// The capacity should be immediately available as nothing else is
// happening on the stream.
assert_eq!(s1.capacity(), window_size);
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
// Create a second stream
let (resp2, mut s2) = client.send_request(request, false).unwrap();
// Request capacity
s2.reserve_capacity(5);
// There should be no available capacity (as it is being held up by
// the previous stream
assert_eq!(s2.capacity(), 0);
// Closing the previous stream by sending an empty data frame will
// release the capacity to s2
s1.send_data("".into(), true).unwrap();
// The capacity should be available
assert_eq!(s2.capacity(), 5);
// Send the frame
s2.send_data("hello".into(), true).unwrap();
// Drive both streams to prevent the handles from being dropped
// (which will send a RST_STREAM) before the connection is closed.
h2.drive(resp1).await.unwrap();
h2.drive(resp2).await.unwrap();
};
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.recv_frame(frames::headers(3).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::headers(3).response(200)).await;
srv.recv_frame(frames::data(1, &b""[..]).eos()).await;
srv.recv_frame(frames::data(3, &b"hello"[..]).eos()).await;
};
join(srv, h2).await;
}
#[tokio::test]
async fn stream_close_by_trailers_frame_releases_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let window_size = frame::DEFAULT_INITIAL_WINDOW_SIZE as usize;
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
// Send request
let (resp1, mut s1) = client.send_request(request, false).unwrap();
// This effectively reserves the entire connection window
s1.reserve_capacity(window_size);
// The capacity should be immediately available as nothing else is
// happening on the stream.
assert_eq!(s1.capacity(), window_size);
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
// Create a second stream
let (resp2, mut s2) = client.send_request(request, false).unwrap();
// Request capacity
s2.reserve_capacity(5);
// There should be no available capacity (as it is being held up by
// the previous stream
assert_eq!(s2.capacity(), 0);
// Closing the previous stream by sending a trailers frame will
// release the capacity to s2
s1.send_trailers(Default::default()).unwrap();
// The capacity should be available
assert_eq!(s2.capacity(), 5);
// Send the frame
s2.send_data("hello".into(), true).unwrap();
// Drive both streams to prevent the handles from being dropped
// (which will send a RST_STREAM) before the connection is closed.
h2.drive(resp1).await.unwrap();
h2.drive(resp2).await.unwrap();
};
let srv = async move {
let settings = srv.assert_client_handshake().await;
// Get the first frame
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.recv_frame(frames::headers(3).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::headers(3).response(200)).await;
srv.recv_frame(frames::headers(1).eos()).await;
srv.recv_frame(frames::data(3, &b"hello"[..]).eos()).await;
};
join(srv, h2).await;
}
#[tokio::test]
async fn stream_close_by_send_reset_frame_releases_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.send_frame(frames::data(1, vec![0; 16_384])).await;
srv.send_frame(frames::data(1, vec![0; 16_384]).eos()).await;
srv.recv_frame(frames::window_update(0, 16_384 * 2)).await;
srv.recv_frame(
frames::headers(3)
.request("GET", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.send_frame(frames::headers(3).response(200).eos()).await;
};
let client = async move {
let (mut client, mut conn) = client::handshake(io).await.expect("client handshake");
{
let request = Request::builder()
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (resp, _) = client.send_request(request, true).unwrap();
let _res = conn.drive(resp).await;
// ^-- ignore the response body
}
let resp = {
let request = Request::builder()
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (resp, _) = client.send_request(request, true).unwrap();
drop(client);
resp
};
let _res = conn.drive(resp).await;
conn.await.expect("client conn");
};
join(srv, client).await;
}
#[test]
#[ignore]
fn stream_close_by_recv_reset_frame_releases_capacity() {}
#[tokio::test]
async fn recv_window_update_on_stream_closed_by_data_frame() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
// Wait for the response
let response = h2.drive(response).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
// Send a data frame, this will also close the connection
stream.send_data("hello".into(), true).unwrap();
// keep `stream` from being dropped in order to prevent
// it from sending an RST_STREAM frame.
//
// i know this is kind of evil, but it's necessary to
// ensure that the stream is closed by the EOS frame,
// and not by the RST_STREAM.
std::mem::forget(stream);
// Wait for the connection to close
h2.await.unwrap();
};
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::headers(1).response(200)).await;
srv.recv_frame(frames::data(1, "hello").eos()).await;
srv.send_frame(frames::window_update(1, 5)).await;
};
join(srv, h2).await;
}
#[tokio::test]
async fn reserved_capacity_assigned_in_multi_window_updates() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
// Consume the capacity
let payload = vec![0; frame::DEFAULT_INITIAL_WINDOW_SIZE as usize];
stream.send_data(payload.into(), false).unwrap();
// Reserve more data than we want
stream.reserve_capacity(10);
let mut stream = h2.drive(util::wait_for_capacity(stream, 5)).await;
stream.send_data("hello".into(), false).unwrap();
stream.send_data("world".into(), true).unwrap();
let response = h2.drive(response).await.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
// Wait for the connection to close
h2.await.unwrap();
};
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
srv.recv_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.recv_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.recv_frame(frames::data(1, vec![0u8; 16_384])).await;
srv.recv_frame(frames::data(1, vec![0u8; 16_383])).await;
idle_ms(100).await;
// Increase the connection window
srv.send_frame(frames::window_update(0, 10)).await;
// Incrementally increase the stream window
srv.send_frame(frames::window_update(1, 4)).await;
idle_ms(50).await;
srv.send_frame(frames::window_update(1, 1)).await;
// Receive first chunk
srv.recv_frame(frames::data(1, "hello")).await;
srv.send_frame(frames::window_update(1, 5)).await;
// Receive second chunk
srv.recv_frame(frames::data(1, "world").eos()).await;
srv.send_frame(frames::headers(1).response(204).eos()).await;
/*
.recv_frame(frames::data(1, "hello").eos())
.send_frame(frames::window_update(1, 5))
*/
};
join(srv, h2).await;
}
#[tokio::test]
async fn connection_notified_on_released_capacity() {
use tokio::sync::{mpsc, oneshot};
h2_support::trace_init!();
let (io, mut srv) = mock::new();
// We're going to run the connection on a thread in order to isolate task
// notifications. This test is here, in part, to ensure that the connection
// receives the appropriate notifications to send out window updates.
let (tx, mut rx) = mpsc::unbounded_channel();
// Because threading is fun
let (settings_tx, settings_rx) = oneshot::channel();
let (th1_tx, th1_rx) = oneshot::channel();
tokio::spawn(async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
settings_tx.send(()).unwrap();
// Get the first request
srv.recv_frame(
frames::headers(1)
.request("GET", "https://example.com/a")
.eos(),
)
.await;
// Get the second request
srv.recv_frame(
frames::headers(3)
.request("GET", "https://example.com/b")
.eos(),
)
.await;
// Send the first response
srv.send_frame(frames::headers(1).response(200)).await;
// Send the second response
srv.send_frame(frames::headers(3).response(200)).await;
// Fill the connection window
srv.send_frame(frames::data(1, vec![0u8; 16_384]).eos())
.await;
idle_ms(100).await;
srv.send_frame(frames::data(3, vec![0u8; 16_384]).eos())
.await;
// The window update is sent
srv.recv_frame(frames::window_update(0, 16_384)).await;
th1_tx.send(()).unwrap();
});
let (th2_tx, th2_rx) = oneshot::channel();
let (mut client, mut h2) = client::handshake(io).await.unwrap();
h2.drive(settings_rx).await.unwrap();
let request = Request::get("https://example.com/a").body(()).unwrap();
tx.send(client.send_request(request, true).unwrap().0)
.unwrap();
let request = Request::get("https://example.com/b").body(()).unwrap();
tx.send(client.send_request(request, true).unwrap().0)
.unwrap();
tokio::spawn(async move {
// Run the connection to completion
h2.await.unwrap();
th2_tx.send(()).unwrap();
drop(client);
});
// Get the two requests
let a = rx.recv().await.unwrap();
let b = rx.recv().await.unwrap();
// Get the first response
let response = a.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let (_, mut a) = response.into_parts();
// Get the next chunk
let chunk = a.data().await.unwrap();
assert_eq!(16_384, chunk.unwrap().len());
// Get the second response
let response = b.await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let (_, mut b) = response.into_parts();
// Get the next chunk
let chunk = b.data().await.unwrap();
assert_eq!(16_384, chunk.unwrap().len());
// Wait a bit
idle_ms(100).await;
// Release the capacity
a.flow_control().release_capacity(16_384).unwrap();
th1_rx.await.unwrap();
th2_rx.await.unwrap();
// Explicitly drop this after the joins so that the capacity doesn't get
// implicitly released before.
drop(b);
}
#[tokio::test]
async fn recv_settings_removes_available_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let mut settings = frame::Settings::default();
settings.set_initial_window_size(Some(0));
let srv = async move {
let settings = srv.assert_client_handshake_with_settings(settings).await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
idle_ms(100).await;
srv.send_frame(frames::window_update(0, 11)).await;
srv.send_frame(frames::window_update(1, 11)).await;
srv.recv_frame(frames::data(1, "hello world").eos()).await;
srv.send_frame(frames::headers(1).response(204).eos()).await;
};
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
stream.reserve_capacity(11);
let mut stream = h2.drive(util::wait_for_capacity(stream, 11)).await;
assert_eq!(stream.capacity(), 11);
stream.send_data("hello world".into(), true).unwrap();
let response = h2.drive(response).await.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
// Wait for the connection to close
// Hold on to the `client` handle to avoid sending a GO_AWAY frame.
h2.await.unwrap();
};
join(srv, h2).await;
}
#[tokio::test]
async fn recv_settings_keeps_assigned_capacity() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let (sent_settings, sent_settings_rx) = futures::channel::oneshot::channel();
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
srv.send_frame(frames::settings().initial_window_size(64))
.await;
srv.recv_frame(frames::settings_ack()).await;
sent_settings.send(()).unwrap();
srv.recv_frame(frames::data(1, "hello world").eos()).await;
srv.send_frame(frames::headers(1).response(204).eos()).await;
};
let h2 = async move {
let (mut client, h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
stream.reserve_capacity(11);
let f = async move {
let mut stream = util::wait_for_capacity(stream, 11).await;
sent_settings_rx.await.expect("rx");
stream.send_data("hello world".into(), true).unwrap();
let resp = response.await.expect("response");
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
};
join(async move { h2.await.expect("h2") }, f).await;
};
join(srv, h2).await;
}
#[tokio::test]
async fn recv_no_init_window_then_receive_some_init_window() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let mut settings = frame::Settings::default();
settings.set_initial_window_size(Some(0));
let srv = async move {
let settings = srv.assert_client_handshake_with_settings(settings).await;
assert_default_settings!(settings);
srv.recv_frame(frames::headers(1).request("POST", "https://http2.akamai.com/"))
.await;
idle_ms(100).await;
srv.send_frame(frames::settings().initial_window_size(10))
.await;
srv.recv_frame(frames::settings_ack()).await;
srv.recv_frame(frames::data(1, "hello worl")).await;
idle_ms(100).await;
srv.send_frame(frames::settings().initial_window_size(11))
.await;
srv.recv_frame(frames::settings_ack()).await;
srv.recv_frame(frames::data(1, "d").eos()).await;
srv.send_frame(frames::headers(1).response(204).eos()).await;
};
let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();
let request = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (response, mut stream) = client.send_request(request, false).unwrap();
stream.reserve_capacity(11);
let mut stream = h2.drive(util::wait_for_capacity(stream, 11)).await;
assert_eq!(stream.capacity(), 11);
stream.send_data("hello world".into(), true).unwrap();
let response = h2.drive(response).await.unwrap();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
// Wait for the connection to close
// Hold on to the `client` handle to avoid sending a GO_AWAY frame.
h2.await.unwrap();
};
join(srv, h2).await;
}
#[tokio::test]
async fn settings_lowered_capacity_returns_capacity_to_connection() {
use futures::channel::oneshot;
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
let window_size = frame::DEFAULT_INITIAL_WINDOW_SIZE as usize;
let (th1_tx, th1_rx) = oneshot::channel();
// Spawn the server on a thread
tokio::spawn(async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
tx1.send(()).unwrap();
srv.recv_frame(frames::headers(1).request("POST", "https://example.com/one"))
.await;
srv.recv_frame(frames::headers(3).request("POST", "https://example.com/two"))
.await;
idle_ms(200).await;
// Remove all capacity from streams
srv.send_frame(frames::settings().initial_window_size(0))
.await;
srv.recv_frame(frames::settings_ack()).await;
// Let stream 3 make progress
srv.send_frame(frames::window_update(3, 11)).await;
srv.recv_frame(frames::data(3, "hello world").eos()).await;
// Wait to get notified
//
// A timeout is used here to avoid blocking forever if there is a
// failure
let _ = tokio::time::timeout(Duration::from_secs(5), rx2)
.await
.unwrap();
idle_ms(500).await;
// Reset initial window size
srv.send_frame(frames::settings().initial_window_size(window_size as u32))
.await;
srv.recv_frame(frames::settings_ack()).await;
// Get data from first stream
srv.recv_frame(frames::data(1, "hello world").eos()).await;
// Send responses
srv.send_frame(frames::headers(1).response(204).eos()).await;
srv.send_frame(frames::headers(3).response(204).eos()).await;
drop(srv);
th1_tx.send(()).unwrap();
});
let (mut client, h2) = client::handshake(io).await.unwrap();
let (th2_tx, th2_rx) = oneshot::channel();
// Drive client connection
tokio::spawn(async move {
h2.await.unwrap();