1package dgram
2
3import (
4 "testing"
5 "time"
6)
7
8func TestCongestionControllerBoundsAndIntegerGrowth(t *testing.T) {
9 const mtu = 1200
10 c := newCongestionController(mtu)
11 if got, want := c.cwndBytes, initialCongestionPackets*mtu; got != want {
12 t.Fatalf("initial cwnd=%d, want %d", got, want)
13 }
14 if got, want := c.burstBytes(), dataBurstPackets*mtu; got != want {
15 t.Fatalf("initial burst=%d, want %d", got, want)
16 }
17 if got, want := c.bytesPerSecond(), initialCongestionPackets*mtu*int(time.Second/initialPacingRTT); got != want {
18 t.Fatalf("initial pacing rate=%d, want %d", got, want)
19 }
20
21 c.onLoss()
22 if got, want := c.cwndBytes, initialCongestionPackets*mtu/2; got != want {
23 t.Fatalf("cwnd after first loss=%d, want %d", got, want)
24 }
25 c.cwndBytes = 3 * mtu
26 c.onLoss()
27 if got, want := c.cwndBytes, minimumCongestionPackets*mtu; got != want {
28 t.Fatalf("minimum cwnd=%d, want %d", got, want)
29 }
30
31 c.cwndBytes = 10 * mtu
32 c.onACK(1)
33 if got, want := c.cwndBytes, 10*mtu+1; got != want {
34 t.Fatalf("minimum additive increase cwnd=%d, want %d", got, want)
35 }
36 c.onACK(10 * mtu)
37 if got, want := c.cwndBytes, 10*mtu+1+(mtu*10*mtu)/(10*mtu+1); got != want {
38 t.Fatalf("integer additive increase cwnd=%d, want %d", got, want)
39 }
40 c.cwndBytes = maximumCongestionBytes
41 c.onACK(maximumCongestionBytes)
42 if got := c.cwndBytes; got != maximumCongestionBytes {
43 t.Fatalf("maximum cwnd=%d, want %d", got, maximumCongestionBytes)
44 }
45
46 c.onRTT(500 * time.Microsecond)
47 if got, want := c.bytesPerSecond(), maximumCongestionBytes*1000; got != want {
48 t.Fatalf("sub-millisecond pacing rate=%d, want %d", got, want)
49 }
50}
51
52func BenchmarkCongestionControllerACK(b *testing.B) {
53 c := newCongestionController(defaultMTU)
54 b.ReportAllocs()
55 for b.Loop() {
56 c.onACK(defaultMTU)
57 if c.cwndBytes == c.maxBytes {
58 c = newCongestionController(defaultMTU)
59 }
60 }
61}