github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/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 TestFlow(t *testing.T) {
    10  	var st flow
    11  	var conn flow
    12  	st.add(3)
    13  	conn.add(2)
    14  
    15  	if got, want := st.available(), int32(3); got != want {
    16  		t.Errorf("available = %d; want %d", got, want)
    17  	}
    18  	st.setConnFlow(&conn)
    19  	if got, want := st.available(), int32(2); got != want {
    20  		t.Errorf("after parent setup, available = %d; want %d", got, want)
    21  	}
    22  
    23  	st.take(2)
    24  	if got, want := conn.available(), int32(0); got != want {
    25  		t.Errorf("after taking 2, conn = %d; want %d", got, want)
    26  	}
    27  	if got, want := st.available(), int32(0); got != want {
    28  		t.Errorf("after taking 2, stream = %d; want %d", got, want)
    29  	}
    30  }
    31  
    32  func TestFlowAdd(t *testing.T) {
    33  	var f flow
    34  	if !f.add(1) {
    35  		t.Fatal("failed to add 1")
    36  	}
    37  	if !f.add(-1) {
    38  		t.Fatal("failed to add -1")
    39  	}
    40  	if got, want := f.available(), int32(0); got != want {
    41  		t.Fatalf("size = %d; want %d", got, want)
    42  	}
    43  	if !f.add(1<<31 - 1) {
    44  		t.Fatal("failed to add 2^31-1")
    45  	}
    46  	if got, want := f.available(), int32(1<<31-1); got != want {
    47  		t.Fatalf("size = %d; want %d", got, want)
    48  	}
    49  	if f.add(1) {
    50  		t.Fatal("adding 1 to max shouldn't be allowed")
    51  	}
    52  }
    53  
    54  func TestFlowAddOverflow(t *testing.T) {
    55  	var f flow
    56  	if !f.add(0) {
    57  		t.Fatal("failed to add 0")
    58  	}
    59  	if !f.add(-1) {
    60  		t.Fatal("failed to add -1")
    61  	}
    62  	if !f.add(0) {
    63  		t.Fatal("failed to add 0")
    64  	}
    65  	if !f.add(1) {
    66  		t.Fatal("failed to add 1")
    67  	}
    68  	if !f.add(1) {
    69  		t.Fatal("failed to add 1")
    70  	}
    71  	if !f.add(0) {
    72  		t.Fatal("failed to add 0")
    73  	}
    74  	if !f.add(-3) {
    75  		t.Fatal("failed to add -3")
    76  	}
    77  	if got, want := f.available(), int32(-2); got != want {
    78  		t.Fatalf("size = %d; want %d", got, want)
    79  	}
    80  	if !f.add(1<<31 - 1) {
    81  		t.Fatal("failed to add 2^31-1")
    82  	}
    83  	if got, want := f.available(), int32(1+-3+(1<<31-1)); got != want {
    84  		t.Fatalf("size = %d; want %d", got, want)
    85  	}
    86  
    87  }