-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathCubicTest.cpp
More file actions
2313 lines (1898 loc) · 95.9 KB
/
CubicTest.cpp
File metadata and controls
2313 lines (1898 loc) · 95.9 KB
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
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
Unit tests for CUBIC congestion control.
--*/
#include "main.h"
#ifdef QUIC_CLOG
#include "CubicTest.cpp.clog.h"
#endif
extern "C" uint32_t CubeRoot(uint32_t Radicand);
//
// Test: CubeRoot - Integer Cube Root Function
// Parametric test covering perfect cubes, non-perfect cubes (floor behavior),
// boundary values (0, 1, UINT32_MAX), large inputs, and values used by CUBIC's
// KCubic computation.
//
struct CubeRootTestParam {
uint32_t Input;
uint32_t Expected;
};
class CubeRootTest : public ::testing::TestWithParam<CubeRootTestParam> {};
TEST_P(CubeRootTest, ReturnsExpectedValue)
{
auto param = GetParam();
ASSERT_EQ(CubeRoot(param.Input), param.Expected);
}
INSTANTIATE_TEST_SUITE_P(
CubeRootCases,
CubeRootTest,
::testing::Values(
// Boundary values
CubeRootTestParam{0, 0},
CubeRootTestParam{1, 1},
CubeRootTestParam{UINT32_MAX, 1625},
// Perfect cubes
CubeRootTestParam{8, 2},
CubeRootTestParam{27, 3},
CubeRootTestParam{64, 4},
CubeRootTestParam{125, 5},
CubeRootTestParam{1000, 10},
CubeRootTestParam{4096, 16},
// Non-perfect cubes: floors to integer cube root
CubeRootTestParam{2, 1}, // between 1^3 and 2^3
CubeRootTestParam{7, 1}, // just below 2^3
CubeRootTestParam{9, 2}, // just above 2^3
CubeRootTestParam{26, 2}, // just below 3^3
CubeRootTestParam{100, 4}, // between 4^3(64) and 5^3(125)
// Large values
CubeRootTestParam{1000000, 100},
CubeRootTestParam{1000000000, 1000}
));
//
// Helper to create a minimal valid connection for testing CUBIC initialization.
// Uses a real QUIC_CONNECTION structure to ensure proper memory layout when
// QuicCongestionControlGetConnection() does CXPLAT_CONTAINING_RECORD pointer arithmetic.
//
static void InitializeMockConnection(
QUIC_CONNECTION& Connection,
uint16_t Mtu)
{
Connection.Paths[0].Mtu = Mtu;
Connection.Paths[0].IsActive = TRUE;
Connection.Send.NextPacketNumber = 0;
Connection.Settings.PacingEnabled = FALSE;
Connection.Settings.HyStartEnabled = FALSE;
Connection.Paths[0].GotFirstRttSample = FALSE;
Connection.Paths[0].SmoothedRtt = 0;
}
//
// Helper to construct a QUIC_ACK_EVENT with common defaults.
// Fields not specified here (IsImplicit, HasLoss, IsLargestAckedPacketAppLimited,
// AckedPackets) default to 0/FALSE/NULL via {} initialization.
//
static QUIC_ACK_EVENT MakeAckEvent(
uint64_t TimeNow,
uint64_t LargestAck,
uint64_t LargestSentPacketNumber,
uint32_t BytesAcked,
uint64_t SmoothedRtt = 50000,
uint64_t MinRtt = 45000,
BOOLEAN MinRttValid = TRUE)
{
QUIC_ACK_EVENT Ack{};
Ack.TimeNow = TimeNow;
Ack.LargestAck = LargestAck;
Ack.LargestSentPacketNumber = LargestSentPacketNumber;
Ack.NumRetransmittableBytes = BytesAcked;
Ack.NumTotalAckedRetransmittableBytes = BytesAcked;
Ack.SmoothedRtt = SmoothedRtt;
Ack.MinRtt = MinRtt;
Ack.MinRttValid = MinRttValid;
Ack.AdjustedAckTime = TimeNow;
return Ack;
}
//
// Helper to construct a QUIC_LOSS_EVENT with common defaults.
//
static QUIC_LOSS_EVENT MakeLossEvent(
uint32_t LostBytes,
uint64_t LargestPacketNumberLost,
uint64_t LargestSentPacketNumber,
BOOLEAN PersistentCongestion = FALSE)
{
QUIC_LOSS_EVENT Loss{};
Loss.NumRetransmittableBytes = LostBytes;
Loss.LargestPacketNumberLost = LargestPacketNumberLost;
Loss.LargestSentPacketNumber = LargestSentPacketNumber;
Loss.PersistentCongestion = PersistentCongestion;
return Loss;
}
//
// GoogleTest fixture for CUBIC congestion control tests.
// Provides Connection, Settings, and Cubic members with a parameterized
// InitializeWithDefaults() helper that covers the common setup variations.
//
class CubicTest : public ::testing::Test {
protected:
static constexpr uint16_t kIPv6UdpOverhead = 48; // IPv6 (40) + UDP (8) header bytes
// HyStart++ RTT constants for delay increase/decrease detection tests.
// Activation threshold = kHyStartBaselineRtt + kHyStartBaselineRtt/N_SAMPLING = 45000.
static constexpr uint64_t kHyStartBaselineRtt = 40000; // MinRttInLastRound baseline
static constexpr uint64_t kHyStartRttAboveThreshold = 46000; // > activation threshold (45000)
static constexpr uint64_t kHyStartRttBelowThreshold = 42000; // < activation threshold (45000)
static constexpr uint64_t kHyStartRttDecrease = 38000; // < CssBaselineMinRtt → triggers ACTIVE → NOT_STARTED
QUIC_CONNECTION Connection{};
QUIC_SETTINGS_INTERNAL Settings{};
QUIC_CONGESTION_CONTROL_CUBIC* Cubic;
QUIC_CONGESTION_CONTROL* CC;
//
// Common setup: initializes mock connection, configures settings, and
// calls CubicCongestionControlInitialize. Covers all recurring variations.
//
void InitializeWithDefaults(
uint32_t WindowPackets = 10,
bool HyStart = false,
uint16_t Mtu = 1280,
uint32_t IdleTimeoutMs = 1000,
bool GotRttSample = false,
uint64_t SmoothedRtt = 50000,
bool SetMinRtt = false,
uint64_t MinRtt = 40000,
uint64_t RttVariance = 5000
)
{
Settings.InitialWindowPackets = WindowPackets;
Settings.SendIdleTimeoutMs = IdleTimeoutMs;
Settings.HyStartEnabled = HyStart ? TRUE : FALSE;
InitializeMockConnection(Connection, Mtu);
Connection.Settings.HyStartEnabled = HyStart ? TRUE : FALSE;
if (GotRttSample) {
Connection.Paths[0].GotFirstRttSample = TRUE;
Connection.Paths[0].SmoothedRtt = SmoothedRtt;
}
if (SetMinRtt) {
Connection.Paths[0].MinRtt = MinRtt;
Connection.Paths[0].RttVariance = RttVariance;
}
CC = &Connection.CongestionControl;
CubicCongestionControlInitialize(CC, &Settings);
Cubic = &CC->Cubic;
}
void InitializeDefaultWithRtt(uint32_t WindowPackets = 10, bool HyStart = true) {
InitializeWithDefaults(/*WindowPackets=*/WindowPackets, /*HyStart=*/HyStart, /*Mtu=*/1280, /*IdleTimeoutMs=*/1000, /*GotRttSample=*/true, /*SmoothedRtt=*/50000);
}
//
// Helper to enter congestion avoidance from slow start.
// Sends full window, triggers loss, sends additional data, then exits
// recovery via an ACK. After this call:
// - IsInRecovery = FALSE, HasHadCongestionEvent = TRUE
// - CongestionWindow = SlowStartThreshold = pre-loss window * 7/10
// - TimeOfCongAvoidStart = 1050000
// - Connection.Send.NextPacketNumber = 15
// Returns the post-loss CongestionWindow.
//
uint32_t EnterCongestionAvoidance(uint32_t LostBytes = 2400) {
CC->QuicCongestionControlOnDataSent(CC, Cubic->CongestionWindow);
Connection.Send.NextPacketNumber = 10;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(LostBytes, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
Connection.Send.NextPacketNumber = 15;
CC->QuicCongestionControlOnDataSent(CC, 5000);
QUIC_ACK_EVENT ExitAck = MakeAckEvent(1050000, 11, 20, 1200);
CC->QuicCongestionControlOnDataAcknowledged(CC, &ExitAck);
return Cubic->CongestionWindow;
}
//
// Helper to transition HyStart++ from NOT_STARTED to ACTIVE state via
// delay increase detection. Requires InitializeDefaultWithRtt(..., HyStart=true)
// to have been called first.
//
// Mechanism: Sets MinRttInLastRound to kHyStartBaselineRtt, then sends
// N_SAMPLING (8) ACKs with MinRtt=kHyStartRttAboveThreshold to complete
// sampling. The 9th ACK triggers delay increase detection because
// MinRttInCurrentRound >= kHyStartBaselineRtt + Eta (= activation threshold).
//
// After this call:
// - HyStartState = HYSTART_ACTIVE
// - CWndSlowStartGrowthDivisor = 4
// - ConservativeSlowStartRounds = 5 (QUIC_CONSERVATIVE_SLOW_START_DEFAULT_ROUNDS)
// - CssBaselineMinRtt = kHyStartRttAboveThreshold
// - MinRttInLastRound = kHyStartBaselineRtt
// - Connection.Send.NextPacketNumber = 100, HyStartRoundEnd = 100
//
void EnterHyStartActive() {
Connection.Send.NextPacketNumber = 100;
Cubic->HyStartRoundEnd = 100;
Cubic->MinRttInLastRound = kHyStartBaselineRtt;
for (uint32_t i = 0; i < QUIC_HYSTART_DEFAULT_N_SAMPLING; i++) {
CC->QuicCongestionControlOnDataSent(CC, 1200);
QUIC_ACK_EVENT AckEvent = MakeAckEvent(
1000000 + (i * 10000), 10 + i, 15 + i, 1200,
50000, kHyStartRttAboveThreshold);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
}
CC->QuicCongestionControlOnDataSent(CC, 1200);
QUIC_ACK_EVENT TriggerAck = MakeAckEvent(1100000, 20, 25, 1200, 50000, kHyStartRttAboveThreshold + 1000);
CC->QuicCongestionControlOnDataAcknowledged(CC, &TriggerAck);
}
};
//====================================================================
//
// Specification-conformance tests
//
// These tests validate behavior defined by RFC 8312 (CUBIC),
// RFC 9002 (QUIC Loss Detection), and RFC 9406 (HyStart++).
// They would remain valid across any conforming implementation.
//
// References:
// https://www.rfc-editor.org/rfc/rfc8312
// https://www.rfc-editor.org/rfc/rfc9002
// https://www.rfc-editor.org/rfc/rfc9406
//
//====================================================================
//
// Test: OnDataAcknowledged - Basic ACK Processing in Slow Start
// Scenario: Tests CubicCongestionControlOnDataAcknowledged for basic slow-start
// window growth. Sends data via OnDataSent (properly tracking BytesInFlightMax),
// then acknowledges a portion. The window should grow by BytesAcked since we are
// in slow start (CongestionWindow < SlowStartThreshold) with HyStart disabled
// (CWndSlowStartGrowthDivisor = 1).
//
TEST_F(CubicTest, OnDataAcknowledged_BasicAck)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ false);
uint32_t InitialWindow = Cubic->CongestionWindow;
// Send data via OnDataSent to properly track BytesInFlightMax.
// Must send enough that BytesInFlightMax > (InitialWindow + BytesAcked) / 2
// to avoid the 2*BytesInFlightMax clamping guard.
uint32_t BytesSent = 10000;
CC->QuicCongestionControlOnDataSent(CC, BytesSent);
uint32_t BytesAcked = 5000;
QUIC_ACK_EVENT AckEvent = MakeAckEvent(1050000, 5, 10, BytesAcked);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
// Slow start growth: window increases by BytesAcked (divisor=1, HyStart disabled)
// NewWindow = 12320 + 5000 = 17320, clamped by 2*BytesInFlightMax = 2*10000 = 20000 (no clamp)
ASSERT_EQ(Cubic->CongestionWindow, InitialWindow + BytesAcked);
ASSERT_EQ(Cubic->BytesInFlight, BytesSent - BytesAcked);
}
//
// Test: OnDataLost - Packet Loss Handling and Window Reduction
// Scenario: Tests CUBIC's response to packet loss. When packets are declared lost,
// the congestion window should be reduced by beta (0.7) and the connection enters
// recovery. Verifies window reduction, threshold update, and recovery state flags.
//
TEST_F(CubicTest, OnDataLost_WindowReduction)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
uint32_t InitialWindow = Cubic->CongestionWindow;
// Send data via OnDataSent to properly track BytesInFlightMax
CC->QuicCongestionControlOnDataSent(CC, 10000);
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(3600, 10, 15);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// Verify window was reduced (CUBIC multiplicative decrease)
// New window = InitialWindow * 0.7 = InitialWindow * 7 / 10
// InitialWindow = (1280 - 48) * 20 = 24640 (IPv6 formula: MTU - 40 - 8)
// Expected = 24640 * 7 / 10 = 17248
uint32_t ExpectedWindow = InitialWindow * 7 / 10;
ASSERT_EQ(Cubic->CongestionWindow, ExpectedWindow);
ASSERT_EQ(Cubic->SlowStartThreshold, ExpectedWindow);
// Verify recovery state transitions
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_TRUE(Cubic->HasHadCongestionEvent);
}
//
// Test: OnEcn - ECN Marking Handling
// Scenario: Tests Explicit Congestion Notification (ECN) handling. When ECN-marked packets
// are received, CUBIC should treat it as a congestion signal and reduce the window.
// Unlike loss, ECN does NOT save previous state (`if (!Ecn)` guard),
// so spurious congestion rollback cannot undo an ECN event.
//
TEST_F(CubicTest, OnEcn_CongestionSignal)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
uint32_t InitialWindow = Cubic->CongestionWindow;
// Send data via OnDataSent to properly track BytesInFlightMax
CC->QuicCongestionControlOnDataSent(CC, 10000);
QUIC_ECN_EVENT EcnEvent{};
EcnEvent.LargestPacketNumberAcked = 10;
EcnEvent.LargestSentPacketNumber = 15;
CC->QuicCongestionControlOnEcn(CC, &EcnEvent);
// Verify window was reduced due to ECN congestion signal (same as loss: 0.7x)
// InitialWindow = (1280 - 48) * 20 = 24640 (IPv6 formula: MTU - 40 - 8)
// Expected = 24640 * 7 / 10 = 17248
uint32_t ExpectedWindow = InitialWindow * 7 / 10;
ASSERT_EQ(Cubic->CongestionWindow, ExpectedWindow);
// Verify recovery state transitions
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_TRUE(Cubic->HasHadCongestionEvent);
}
//
// Test: Fast Convergence - Window Reduction Path
// Scenario: Tests CUBIC's fast convergence algorithm. When a new congestion event occurs
// before reaching the previous WindowMax, CUBIC applies an additional reduction factor
// to converge faster with other flows. This tests the WindowLastMax > WindowMax path.
//
TEST_F(CubicTest, FastConvergence_AdditionalReduction)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 30, /*HyStart = */ true);
// Simulate first congestion event to establish WindowMax
// Send data to fill the window
uint32_t InitialWindow = Cubic->CongestionWindow;
CC->QuicCongestionControlOnDataSent(CC, InitialWindow);
// Trigger first loss event
QUIC_LOSS_EVENT FirstLoss = MakeLossEvent(3000, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &FirstLoss);
// ACK while still in recovery (LargestAck == RecoverySentPacketNumber, so
// recovery doesn't exit). Window stays at post-loss value.
QUIC_ACK_EVENT AckEvent = MakeAckEvent(1000000, 10, 15, 5000);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
// Send more data
CC->QuicCongestionControlOnDataSent(CC, 3000);
// Trigger second loss event (before reaching previous WindowMax)
// Must use LargestPacketNumberLost > RecoverySentPacketNumber (which is 10 from first loss)
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(3000, 15, 20);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// Fast convergence fires on the second loss because WindowLastMax (36960, set
// during first loss) > WindowMax (25872, the current CW at second loss entry).
// The code sets WindowLastMax = WindowMax = 25872, then applies:
// WindowMax = WindowMax * (10 + BETA) / 20 = 25872 * 17 / 20 = 21991
uint32_t WindowAfterFirstLoss = InitialWindow * 7 / 10; // 25872
uint32_t ExpectedWindowMax = WindowAfterFirstLoss * 17 / 20; // 21991
ASSERT_EQ(Cubic->WindowMax, ExpectedWindowMax);
}
//
// Test: Recovery Exit Path
// Scenario: Tests exiting from recovery state when an ACK is received for a packet
// sent after recovery started (LargestAck > RecoverySentPacketNumber). Uses a
// non-persistent loss to enter recovery cleanly.
//
TEST_F(CubicTest, Recovery_ExitOnNewAck)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
// Enter recovery via non-persistent loss
CC->QuicCongestionControlOnDataSent(CC, 5000);
Connection.Send.NextPacketNumber = 10;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(1200, 8, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// Now in recovery state
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_FALSE(Cubic->IsInPersistentCongestion);
// Send new packet after recovery started
Connection.Send.NextPacketNumber = 15;
QUIC_ACK_EVENT AckEvent = MakeAckEvent(1000000, 15, 20, 1200);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
// Should exit recovery
ASSERT_FALSE(Cubic->IsInRecovery);
ASSERT_FALSE(Cubic->IsInPersistentCongestion);
}
//
// Test: ACK During Recovery Stays in Recovery
// Scenario: When an ACK is received with LargestAck <= RecoverySentPacketNumber,
// the connection remains in recovery. Only an ACK for a packet sent after
// recovery started (LargestAck > RecoverySentPacketNumber) exits recovery.
//
TEST_F(CubicTest, Recovery_StayOnOldAck)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ false);
CC->QuicCongestionControlOnDataSent(CC, 5000);
Connection.Send.NextPacketNumber = 10;
// Enter recovery via loss. RecoverySentPacketNumber = 10.
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(1200, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
ASSERT_TRUE(Cubic->IsInRecovery);
uint32_t WindowInRecovery = Cubic->CongestionWindow;
// ACK with LargestAck == RecoverySentPacketNumber (10): should stay in recovery
QUIC_ACK_EVENT StayAck = MakeAckEvent(1050000, 10, 15, 1200);
CC->QuicCongestionControlOnDataAcknowledged(CC, &StayAck);
ASSERT_TRUE(Cubic->IsInRecovery);
// Window should not grow during recovery (goes to Exit label)
ASSERT_EQ(Cubic->CongestionWindow, WindowInRecovery);
// ACK with LargestAck < RecoverySentPacketNumber (8): also stays
QUIC_ACK_EVENT OlderAck = MakeAckEvent(1060000, 8, 15, 600);
CC->QuicCongestionControlOnDataAcknowledged(CC, &OlderAck);
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_EQ(Cubic->CongestionWindow, WindowInRecovery);
// ACK with LargestAck > RecoverySentPacketNumber: exits recovery
Connection.Send.NextPacketNumber = 20;
QUIC_ACK_EVENT ExitAck = MakeAckEvent(1100000, 11, 25, 600);
CC->QuicCongestionControlOnDataAcknowledged(CC, &ExitAck);
ASSERT_FALSE(Cubic->IsInRecovery);
}
//
// Test: Double Loss During Recovery - No Additional Window Reduction
// Scenario: When a second loss occurs during recovery with LargestPacketNumberLost
// <= RecoverySentPacketNumber, the guard at cubic.c prevents a second window
// reduction. Only BytesInFlight is decremented; the congestion window stays at
// its post-first-loss value.
//
TEST_F(CubicTest, Recovery_DoubleLossNoDoubleReduction)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ false);
CC->QuicCongestionControlOnDataSent(CC, 10000);
Connection.Send.NextPacketNumber = 10;
// First loss: enters recovery, reduces window
QUIC_LOSS_EVENT FirstLoss = MakeLossEvent(1200, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &FirstLoss);
ASSERT_TRUE(Cubic->IsInRecovery);
uint32_t WindowAfterFirstLoss = Cubic->CongestionWindow;
uint32_t BytesInFlightAfterFirstLoss = Cubic->BytesInFlight;
// Second loss during recovery: LargestPacketNumberLost (8) <= RecoverySentPacketNumber (10)
// Guard prevents window reduction; only BytesInFlight decreases.
uint32_t SecondLostBytes = 600;
QUIC_LOSS_EVENT SecondLoss = MakeLossEvent(SecondLostBytes, 8, 10);
CC->QuicCongestionControlOnDataLost(CC, &SecondLoss);
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_EQ(Cubic->CongestionWindow, WindowAfterFirstLoss);
ASSERT_EQ(Cubic->BytesInFlight, BytesInFlightAfterFirstLoss - SecondLostBytes);
}
//
// Test: Minimum Window Floor After Loss
// Scenario: When the congestion window is very small and loss occurs, the
// multiplicative decrease (CW * 0.7) would produce a value below the minimum
// window (2 * DatagramPayloadLength). The CXPLAT_MAX guard ensures the window
// never drops below this floor.
//
TEST_F(CubicTest, MinimumWindowFloor_AfterLoss)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 2, /*HyStart = */ false);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
uint32_t InitialWindow = Cubic->CongestionWindow;
// CW = 2 * 1232 = 2464. CW * 7/10 = 1724.
// MinimumWindow = 2 * 1232 = 2464. 1724 < 2464 → floor kicks in.
ASSERT_EQ(InitialWindow, (uint32_t)DatagramPayloadLength * 2);
CC->QuicCongestionControlOnDataSent(CC, 1200);
Connection.Send.NextPacketNumber = 10;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(600, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// Window should be clamped to the minimum, not the 0.7× value
uint32_t MinimumWindow = DatagramPayloadLength * QUIC_PERSISTENT_CONGESTION_WINDOW_PACKETS;
uint32_t BetaReduced = InitialWindow * 7 / 10;
ASSERT_LT(BetaReduced, MinimumWindow); // Verify floor actually matters
ASSERT_EQ(Cubic->CongestionWindow, MinimumWindow);
ASSERT_EQ(Cubic->SlowStartThreshold, MinimumWindow);
}
//
// Test: Slow Start 2×BytesInFlightMax Clamp
// Scenario: When slow start grows the congestion window beyond 2×BytesInFlightMax,
// the window is clamped to prevent growth without network feedback. This fires
// when the application doesn't fully utilize the window (BytesInFlightMax stays
// small relative to CW).
//
TEST_F(CubicTest, SlowStart_BytesInFlightMaxClamp)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ false);
uint32_t InitialWindow = Cubic->CongestionWindow; // 12320
// Artificially lower BytesInFlightMax to simulate app-limited sending
Cubic->BytesInFlightMax = 2000;
// Send and ACK 1200 bytes. BytesInFlight peaks at 1200 < 2000 so max unchanged.
CC->QuicCongestionControlOnDataSent(CC, 1200);
QUIC_ACK_EVENT AckEvent = MakeAckEvent(1050000, 5, 10, 1200);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
// Slow start growth: CW = 12320 + 1200 = 13520, but 2*BytesInFlightMax = 4000.
// Since 13520 > 4000, CW is clamped to 4000.
ASSERT_EQ(Cubic->CongestionWindow, 2 * 2000u);
ASSERT_LT(Cubic->CongestionWindow, InitialWindow + 1200);
}
//
// Test: Congestion Avoidance AIMD vs CUBIC Window Selection
// Scenario: After loss triggers recovery and an ACK exits recovery, a subsequent
// ACK in congestion avoidance exercises the CUBIC formula and AIMD accumulator.
// The max(CUBIC, AIMD) selection determines the new window.
//
TEST_F(CubicTest, CongestionAvoidance_AIMDvsCubicSelection)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
EnterCongestionAvoidance();
// ACK in congestion avoidance: CUBIC/AIMD selection runs
CC->QuicCongestionControlOnDataSent(CC, 3000);
QUIC_ACK_EVENT CongAvoidAck = MakeAckEvent(1100000, 15, 25, 1200);
uint32_t WindowBeforeCongAvoid = Cubic->CongestionWindow;
CC->QuicCongestionControlOnDataAcknowledged(CC, &CongAvoidAck);
// Congestion avoidance ran: CW >= SSThresh so slow start is skipped,
// CUBIC formula and AIMD both execute, max() selects the winner.
ASSERT_FALSE(Cubic->IsInRecovery);
// Reproduce the CUBIC window formula using state already computed by the
// production code during EnterCongestionAvoidance (KCubic, WindowMax, etc.).
uint32_t KCubic = Cubic->KCubic; // Computed by production code on loss
uint32_t WindowMax = Cubic->WindowMax;
uint64_t TimeInCongAvoidUs = CongAvoidAck.TimeNow - Cubic->TimeOfCongAvoidStart;
int64_t DeltaT =
((int64_t)TimeInCongAvoidUs -
(int64_t)KCubic * 1000 + // MS_TO_US
(int64_t)CongAvoidAck.SmoothedRtt) / 1000; // US_TO_MS
int64_t CubicWindow =
((((DeltaT * DeltaT) >> 10) * DeltaT *
(int64_t)(DatagramPayloadLength * 4 / 10)) >> 20) +
(int64_t)WindowMax;
// CUBIC > AIMD → bounded growth path selected
ASSERT_GT(CubicWindow, (int64_t)Cubic->AimdWindow);
uint64_t TargetWindow = CXPLAT_MAX(
WindowBeforeCongAvoid,
CXPLAT_MIN((uint64_t)CubicWindow, (uint64_t)WindowBeforeCongAvoid + (WindowBeforeCongAvoid >> 1)));
uint32_t ExpectedGrowth =
(uint32_t)(((TargetWindow - WindowBeforeCongAvoid) * DatagramPayloadLength) / WindowBeforeCongAvoid);
ASSERT_GT(Cubic->CongestionWindow, WindowBeforeCongAvoid);
ASSERT_EQ(Cubic->CongestionWindow, WindowBeforeCongAvoid + ExpectedGrowth);
}
//
// Test: AIMD Sequential Linear Convergence
// Scenario: Verifies that multiple sequential ACKs produce linear AIMD window
// growth across several rounds. After entering congestion avoidance with
// AimdWindow >= WindowPrior (full-rate accumulation), each ACK of BytesAcked
// bytes adds BytesAcked to AimdAccumulator. When AimdAccumulator exceeds
// AimdWindow, the window grows by 1 DatagramPayloadLength and the accumulator
// wraps by subtracting the new AimdWindow. Over N ACKs, AimdWindow should
// grow by exactly floor(totalBytesAcked / averageAimdWindow) * DPL,
// demonstrating the linear AIMD convergence required by RFC 8312 §4.3.
//
TEST_F(CubicTest, AIMD_SequentialLinearConvergence)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ true);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
EnterCongestionAvoidance();
// Set up for full-rate AIMD (AimdWindow >= WindowPrior).
// Use a small AimdWindow so each ACK triggers exactly one growth.
// BytesAcked per ACK = AimdWindow + DPL + 100 ensures:
// 1. Accumulator exceeds AimdWindow → triggers growth
// 2. Remainder is small → no double-growth in a single ACK
uint32_t InitialAimdWindow = 1000;
Cubic->AimdWindow = InitialAimdWindow;
Cubic->WindowPrior = InitialAimdWindow; // full-rate path
Cubic->AimdAccumulator = 0;
// Send data to cover upcoming ACKs
CC->QuicCongestionControlOnDataSent(CC, 20000);
// Track expected state manually across multiple ACKs
uint32_t ExpectedAimdWindow = InitialAimdWindow;
uint32_t ExpectedAccumulator = 0;
uint64_t TimeUs = 1100000;
uint32_t PacketNum = 21;
const int NumAcks = 5;
for (int i = 0; i < NumAcks; i++) {
uint32_t BytesAcked = ExpectedAimdWindow + DatagramPayloadLength + 100;
QUIC_ACK_EVENT Ack = MakeAckEvent(TimeUs, PacketNum, PacketNum + 1, BytesAcked);
CC->QuicCongestionControlOnDataAcknowledged(CC, &Ack);
// Full-rate: accumulator += BytesAcked
ExpectedAccumulator += BytesAcked;
// Accumulator exceeds AimdWindow → grow by 1 DPL
ExpectedAimdWindow += DatagramPayloadLength;
ExpectedAccumulator -= ExpectedAimdWindow;
ASSERT_EQ(Cubic->AimdWindow, ExpectedAimdWindow)
<< "AimdWindow mismatch after ACK " << (i + 1);
ASSERT_EQ(Cubic->AimdAccumulator, ExpectedAccumulator)
<< "AimdAccumulator mismatch after ACK " << (i + 1);
TimeUs += 50000; // advance 50ms per ACK
PacketNum += 2;
}
// After 5 ACKs, AimdWindow should have grown by exactly 5 * DPL
ASSERT_EQ(Cubic->AimdWindow, InitialAimdWindow + NumAcks * DatagramPayloadLength);
}
//
// Test: Reno-Friendly Region (CW = AimdWindow path)
// Scenario: After a loss with a small initial window (3 packets), a large ACK
// in congestion avoidance grows AimdWindow past CubicWindow via the AIMD
// accumulator. Since AimdWindow > CubicWindow, the Reno-friendly region
// CW is set to AimdWindow directly, instead of using the
// bounded growth formula from the concave/convex region.
//
TEST_F(CubicTest, CongestionAvoidance_RenoFriendlyRegion)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 3, /*HyStart = */ false);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
uint32_t WindowAfterLoss = EnterCongestionAvoidance(/*LostBytes=*/1200);
// Large ACK in congestion avoidance.
// BytesAcked=10000 grows AimdAccumulator by 5000 (half-rate: AimdWindow < WindowPrior).
// 5000 > AimdWindow(2587) → AimdWindow += 1232 = 3819, accumulator = 5000 - 3819 = 1181.
//
// KCubic = CubeRoot((3696/1232 * 3 << 9) / 4) = CubeRoot(1152) = 10
// KCubic = S_TO_MS(10) = 10000; >>3 = 1250
// TimeInCongAvoid = 1051000 - 1050000 = 1000 µs
// DeltaT = US_TO_MS(1000 - 1250000 + 50000) = -1199
// CubicWindow = ((-1199²>>10) * -1199 * 492 >> 20) + 3696 = -790 + 3696 = 2906
//
// AimdWindow(3819) > CubicWindow(2906) → Reno-friendly region: CW = AimdWindow
CC->QuicCongestionControlOnDataSent(CC, 10000);
QUIC_ACK_EVENT CongAvoidAck = MakeAckEvent(1051000, 21, 25, 10000);
CC->QuicCongestionControlOnDataAcknowledged(CC, &CongAvoidAck);
// Verify CW was set from AimdWindow (Reno-friendly), not bounded growth.
uint32_t ExpectedAimdWindow = WindowAfterLoss + DatagramPayloadLength; // 2587 + 1232 = 3819
ASSERT_EQ(Cubic->AimdWindow, ExpectedAimdWindow);
ASSERT_EQ(Cubic->CongestionWindow, ExpectedAimdWindow);
}
//
// Test: Congestion Avoidance - Bounded Growth Clamp
// Scenario: When the CUBIC formula produces a window far exceeding the current
// CongestionWindow (e.g., due to large DeltaT with KCubic=0), the bounded
// growth clamp constrains TargetWindow to at most 1.5×CW.
// This prevents unrealistic single-ACK window jumps. The growth per ACK is
// then (TargetWindow - CW) * DatagramPayloadLength / CW.
//
TEST_F(CubicTest, CongestionAvoidance_BoundedGrowthClamp)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ false);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
// Force into congestion avoidance
uint32_t CW = 20000;
Cubic->SlowStartThreshold = 10000;
Cubic->CongestionWindow = CW;
// Set WindowMax to maximum value. With KCubic=0 and long TimeInCongAvoid,
// DeltaT is capped to 2.5M, producing a huge positive
// CubicWindow. The bounded growth clamp limits TargetWindow to 1.5*CW.
Cubic->WindowMax = UINT32_MAX;
Cubic->BytesInFlightMax = 50000;
uint64_t TimeNowUs = 30000000000ULL; // 30000 seconds
Cubic->TimeOfCongAvoidStart = 1000000;
Cubic->KCubic = 0;
Cubic->TimeOfLastAckValid = TRUE;
Cubic->TimeOfLastAck = TimeNowUs - 100000;
uint32_t BytesToSend = 1200;
CC->QuicCongestionControlOnDataSent(CC, BytesToSend);
QUIC_ACK_EVENT AckEvent = MakeAckEvent(TimeNowUs, 10, 15, BytesToSend, 50000, 45000, FALSE);
CC->QuicCongestionControlOnDataAcknowledged(CC, &AckEvent);
// CubicWindow is huge positive (DeltaT³ term dominates), exceeding 1.5*CW.
// Bounded growth clamp: TargetWindow = max(CW, min(CubicWindow, CW + CW/2))
// = max(20000, min(huge, 30000)) = 30000
// Growth = (TargetWindow - CW) * DatagramPayloadLength / CW
// = (30000 - 20000) * 1232 / 20000 = 616
// NewCW = 20000 + 616 = 20616
uint32_t TargetWindow = CW + CW / 2;
uint32_t ExpectedGrowth = (uint32_t)(((uint64_t)(TargetWindow - CW) * DatagramPayloadLength) / CW);
ASSERT_EQ(Cubic->CongestionWindow, CW + ExpectedGrowth);
}
//
// Test: Spurious Congestion Event Rollback
// Scenario: Tests the spurious congestion event handling. When a congestion event
// is determined to be spurious (false positive), CUBIC should restore the previous
// state before the congestion event occurred.
//
TEST_F(CubicTest, SpuriousCongestion_StateRollback)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
uint32_t InitialWindow = DatagramPayloadLength * Settings.InitialWindowPackets;
// Trigger a congestion event first
CC->QuicCongestionControlOnDataSent(CC, 10000);
uint32_t WindowBeforeLoss = Cubic->CongestionWindow;
ASSERT_EQ(WindowBeforeLoss, InitialWindow);
Connection.Send.NextPacketNumber = 15;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(2400, 10, 15);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// After loss: Window = InitialWindow * 7 / 10
uint32_t ExpectedWindowAfterLoss = WindowBeforeLoss * 7 / 10;
uint32_t WindowAfterLoss = Cubic->CongestionWindow;
ASSERT_EQ(WindowAfterLoss, ExpectedWindowAfterLoss);
// Now declare it spurious — returns TRUE if blocked state changed
BOOLEAN SpuriousResult = CC->QuicCongestionControlOnSpuriousCongestionEvent(CC);
// State should be restored
ASSERT_EQ(Cubic->CongestionWindow, WindowBeforeLoss);
ASSERT_FALSE(Cubic->IsInRecovery);
ASSERT_FALSE(Cubic->HasHadCongestionEvent);
// Before rollback: CanSend = TRUE (BIF=7600 < CW=17248).
// After rollback: CanSend = TRUE (BIF=7600 < CW=24640). No state change → FALSE.
ASSERT_FALSE(SpuriousResult);
}
// ==========================================================================================================================================
// HyStart++ State Transition Tests
//
// State Transition Table
// | From State | To State | Condition | Tested By |
// |---------------------|---------------------|------------------------------------------|-------------------------------------------------|
// | HYSTART_NOT_STARTED | HYSTART_ACTIVE | RTT increase detected (delay increase) | DelayIncreaseDetection_TriggerActiveTransition |
// | HYSTART_NOT_STARTED | HYSTART_DONE | Loss, ECN, or persistent congestion | NotStartedToDone_ViaLoss, _ViaECN, |
// | | | | AnyToDone_ViaPersistentCongestion |
// | HYSTART_ACTIVE | HYSTART_DONE | Conservative slow start rounds completed | ConservativeSlowStartRounds_TransitionToDone |
// | HYSTART_ACTIVE | HYSTART_DONE | Loss, ECN, or persistent congestion | ActiveToDone_ViaLoss, _ViaECN |
// | HYSTART_ACTIVE | HYSTART_NOT_STARTED | RTT decrease detected (spurious exit) | RttDecreaseDetection_ReturnToNotStarted |
// | HYSTART_DONE | (no transitions) | Terminal / absorbing state | TerminalState_DoneIsAbsorbing |
// ==========================================================================================================================================
//
//
// Test: HyStart++ NOT_STARTED → DONE via Loss
// Scenario: Tests direct transition from NOT_STARTED to DONE when packet loss
// occurs before HyStart++ detection logic activates. Window reduction mechanics
// are verified by OnDataLost_WindowReduction; this test focuses on state transition.
//
TEST_F(CubicTest, HyStart_NotStartedToDone_ViaLoss)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
// Precondition: Verify in NOT_STARTED state
ASSERT_EQ(Cubic->HyStartState, HYSTART_NOT_STARTED);
// Send data to have bytes in flight
CC->QuicCongestionControlOnDataSent(CC, 8000);
Connection.Send.NextPacketNumber = 10;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(2400, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 1u);
}
//
// Test: HyStart++ NOT_STARTED → DONE via ECN
// Scenario: Tests direct transition from NOT_STARTED to DONE when ECN marking
// is received. Window reduction mechanics are verified by OnEcn_CongestionSignal;
// this test focuses on state transition.
//
TEST_F(CubicTest, HyStart_NotStartedToDone_ViaECN)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
// Precondition: Verify in NOT_STARTED state
ASSERT_EQ(Cubic->HyStartState, HYSTART_NOT_STARTED);
// Send data
CC->QuicCongestionControlOnDataSent(CC, 8000);
Connection.Send.NextPacketNumber = 15;
QUIC_ECN_EVENT EcnEvent{};
EcnEvent.LargestPacketNumberAcked = 10;
EcnEvent.LargestSentPacketNumber = 15;
CC->QuicCongestionControlOnEcn(CC, &EcnEvent);
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 1u);
}
//
// Test: HyStart++ ACTIVE → DONE via Loss
// Scenario: Tests transition from ACTIVE to DONE when loss occurs during
// conservative slow start. This is a different entry point than
// NOT_STARTED → DONE and verifies the CWndSlowStartGrowthDivisor is
// reset from 4 back to 1.
//
TEST_F(CubicTest, HyStart_ActiveToDone_ViaLoss)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ true);
EnterHyStartActive();
ASSERT_EQ(Cubic->HyStartState, HYSTART_ACTIVE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 4u);
// Trigger loss while in ACTIVE state
CC->QuicCongestionControlOnDataSent(CC, 5000);
Connection.Send.NextPacketNumber = 30;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(2400, 25, 30);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
// Should transition ACTIVE → DONE
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 1u); // Reset from 4 to 1
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_TRUE(Cubic->HasHadCongestionEvent);
}
//
// Test: HyStart++ ACTIVE → DONE via ECN
// Scenario: Same as ACTIVE → DONE via Loss, but triggered by an ECN
// congestion signal.
//
TEST_F(CubicTest, HyStart_ActiveToDone_ViaECN)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 10, /*HyStart = */ true);
EnterHyStartActive();
ASSERT_EQ(Cubic->HyStartState, HYSTART_ACTIVE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 4u);
// Trigger ECN while in ACTIVE state
Connection.Send.NextPacketNumber = 30;
QUIC_ECN_EVENT EcnEvent{};
EcnEvent.LargestPacketNumberAcked = 25;
EcnEvent.LargestSentPacketNumber = 30;
CC->QuicCongestionControlOnEcn(CC, &EcnEvent);
// Should transition ACTIVE → DONE
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 1u); // Reset from 4 to 1
ASSERT_TRUE(Cubic->IsInRecovery);
}
//
// Test: HyStart++ Any State → DONE via Persistent Congestion
// Scenario: Tests transition from any state to DONE when persistent congestion
// is detected. This is the most severe congestion signal, causing drastic
// window reduction to minimum (2 packets).
//
TEST_F(CubicTest, HyStart_AnyToDone_ViaPersistentCongestion)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 30, /*HyStart = */ true);
const uint16_t DatagramPayloadLength = QuicPathGetDatagramPayloadSize(&Connection.Paths[0]);
// Precondition: Can be in any state (we'll test from NOT_STARTED)
ASSERT_EQ(Cubic->HyStartState, HYSTART_NOT_STARTED);
// Send data
CC->QuicCongestionControlOnDataSent(CC, 15000);
Connection.Send.NextPacketNumber = 20;
// Trigger persistent congestion
QUIC_LOSS_EVENT PersistentLoss = MakeLossEvent(8000, 15, 20, TRUE);
CC->QuicCongestionControlOnDataLost(CC, &PersistentLoss);
// Postcondition: Drastic reduction to minimum window
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
ASSERT_TRUE(Cubic->IsInPersistentCongestion);
ASSERT_TRUE(Cubic->IsInRecovery);
ASSERT_EQ(Cubic->CWndSlowStartGrowthDivisor, 1u);
// Window should be reduced to minimum (2 packets)
uint32_t ExpectedMinWindow = DatagramPayloadLength * QUIC_PERSISTENT_CONGESTION_WINDOW_PACKETS;
ASSERT_EQ(Cubic->CongestionWindow, ExpectedMinWindow);
}
//
// Test: HyStart++ Terminal State - DONE is Absorbing
// Scenario: Once in DONE, neither ACK-based detection nor loss/ECN events can
// change the state. Two focused patterns cover both code paths.
//
TEST_F(CubicTest, HyStart_TerminalState_DoneIsAbsorbing)
{
InitializeDefaultWithRtt(/*WindowPackets = */ 20, /*HyStart = */ true);
// Transition to DONE state via loss
CC->QuicCongestionControlOnDataSent(CC, 5000);
Connection.Send.NextPacketNumber = 10;
QUIC_LOSS_EVENT LossEvent = MakeLossEvent(2400, 5, 10);
CC->QuicCongestionControlOnDataLost(CC, &LossEvent);
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
// Exit recovery to enable ACK processing
Connection.Send.NextPacketNumber = 20;
QUIC_ACK_EVENT RecoveryExitAck = MakeAckEvent(1500000, 20, 25, 0, 50000, 48000);
CC->QuicCongestionControlOnDataAcknowledged(CC, &RecoveryExitAck);
ASSERT_FALSE(Cubic->IsInRecovery);
// Pattern 1: ACK with high RTT (would trigger NOT_STARTED → ACTIVE if not in DONE)
CC->QuicCongestionControlOnDataSent(CC, 1200);
QUIC_ACK_EVENT HighRttAck = MakeAckEvent(2000000, 25, 30, 1200, 50000, kHyStartRttAboveThreshold);
CC->QuicCongestionControlOnDataAcknowledged(CC, &HighRttAck);
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
// Pattern 2: Another loss (would transition non-DONE states)
CC->QuicCongestionControlOnDataSent(CC, 3000);
Connection.Send.NextPacketNumber = 40;
QUIC_LOSS_EVENT SecondLoss = MakeLossEvent(1200, 35, 40);
CC->QuicCongestionControlOnDataLost(CC, &SecondLoss);
ASSERT_EQ(Cubic->HyStartState, HYSTART_DONE);
}