golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/http2/flow_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http2 6 7 import "testing" 8 9 func TestInFlowTake(t *testing.T) { 10 var f inflow 11 f.init(100) 12 if !f.take(40) { 13 t.Fatalf("f.take(40) from 100: got false, want true") 14 } 15 if !f.take(40) { 16 t.Fatalf("f.take(40) from 60: got false, want true") 17 } 18 if f.take(40) { 19 t.Fatalf("f.take(40) from 20: got true, want false") 20 } 21 if !f.take(20) { 22 t.Fatalf("f.take(20) from 20: got false, want true") 23 } 24 } 25 26 func TestInflowAddSmall(t *testing.T) { 27 var f inflow 28 f.init(0) 29 // Adding even a small amount when there is no flow causes an immediate send. 30 if got, want := f.add(1), int32(1); got != want { 31 t.Fatalf("f.add(1) to 1 = %v, want %v", got, want) 32 } 33 } 34 35 func TestInflowAdd(t *testing.T) { 36 var f inflow 37 f.init(10 * inflowMinRefresh) 38 if got, want := f.add(inflowMinRefresh-1), int32(0); got != want { 39 t.Fatalf("f.add(minRefresh - 1) = %v, want %v", got, want) 40 } 41 if got, want := f.add(1), int32(inflowMinRefresh); got != want { 42 t.Fatalf("f.add(minRefresh) = %v, want %v", got, want) 43 } 44 } 45 46 func TestTakeInflows(t *testing.T) { 47 var a, b inflow 48 a.init(10) 49 b.init(20) 50 if !takeInflows(&a, &b, 5) { 51 t.Fatalf("takeInflows(a, b, 5) from 10, 20: got false, want true") 52 } 53 if takeInflows(&a, &b, 6) { 54 t.Fatalf("takeInflows(a, b, 6) from 5, 15: got true, want false") 55 } 56 if !takeInflows(&a, &b, 5) { 57 t.Fatalf("takeInflows(a, b, 5) from 5, 15: got false, want true") 58 } 59 } 60 61 func TestOutFlow(t *testing.T) { 62 var st outflow 63 var conn outflow 64 st.add(3) 65 conn.add(2) 66 67 if got, want := st.available(), int32(3); got != want { 68 t.Errorf("available = %d; want %d", got, want) 69 } 70 st.setConnFlow(&conn) 71 if got, want := st.available(), int32(2); got != want { 72 t.Errorf("after parent setup, available = %d; want %d", got, want) 73 } 74 75 st.take(2) 76 if got, want := conn.available(), int32(0); got != want { 77 t.Errorf("after taking 2, conn = %d; want %d", got, want) 78 } 79 if got, want := st.available(), int32(0); got != want { 80 t.Errorf("after taking 2, stream = %d; want %d", got, want) 81 } 82 } 83 84 func TestOutFlowAdd(t *testing.T) { 85 var f outflow 86 if !f.add(1) { 87 t.Fatal("failed to add 1") 88 } 89 if !f.add(-1) { 90 t.Fatal("failed to add -1") 91 } 92 if got, want := f.available(), int32(0); got != want { 93 t.Fatalf("size = %d; want %d", got, want) 94 } 95 if !f.add(1<<31 - 1) { 96 t.Fatal("failed to add 2^31-1") 97 } 98 if got, want := f.available(), int32(1<<31-1); got != want { 99 t.Fatalf("size = %d; want %d", got, want) 100 } 101 if f.add(1) { 102 t.Fatal("adding 1 to max shouldn't be allowed") 103 } 104 } 105 106 func TestOutFlowAddOverflow(t *testing.T) { 107 var f outflow 108 if !f.add(0) { 109 t.Fatal("failed to add 0") 110 } 111 if !f.add(-1) { 112 t.Fatal("failed to add -1") 113 } 114 if !f.add(0) { 115 t.Fatal("failed to add 0") 116 } 117 if !f.add(1) { 118 t.Fatal("failed to add 1") 119 } 120 if !f.add(1) { 121 t.Fatal("failed to add 1") 122 } 123 if !f.add(0) { 124 t.Fatal("failed to add 0") 125 } 126 if !f.add(-3) { 127 t.Fatal("failed to add -3") 128 } 129 if got, want := f.available(), int32(-2); got != want { 130 t.Fatalf("size = %d; want %d", got, want) 131 } 132 if !f.add(1<<31 - 1) { 133 t.Fatal("failed to add 2^31-1") 134 } 135 if got, want := f.available(), int32(1+-3+(1<<31-1)); got != want { 136 t.Fatalf("size = %d; want %d", got, want) 137 } 138 139 }