Source file src/net/http/internal/http2/flow_test.go

     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 connOutflow
    64  	st.conn = &conn
    65  	st.add(3)
    66  	conn.add(2)
    67  
    68  	if got, ok := st.available(); !ok || got != 2 {
    69  		t.Errorf("available = %d, %v; want 2, true", got, ok)
    70  	}
    71  
    72  	st.take(2)
    73  	if got, want := conn.n, int32(0); got != want {
    74  		t.Errorf("after taking 2, conn = %d; want %d", got, want)
    75  	}
    76  	if got, ok := st.available(); !ok || got != 0 {
    77  		t.Errorf("after taking 2, stream = %d, %v; want 0, true", got, ok)
    78  	}
    79  }
    80  
    81  func TestOutFlowAdd(t *testing.T) {
    82  	var f outflow
    83  	f.conn = &connOutflow{}
    84  	f.conn.add(1<<31 - 1)
    85  	if !f.add(1) {
    86  		t.Fatal("failed to add 1")
    87  	}
    88  	if !f.add(-1) {
    89  		t.Fatal("failed to add -1")
    90  	}
    91  	if got, ok := f.available(); !ok || got != 0 {
    92  		t.Fatalf("size = %d, %v; want 0, true", got, ok)
    93  	}
    94  	if !f.add(1<<31 - 1) {
    95  		t.Fatal("failed to add 2^31-1")
    96  	}
    97  	if got, ok := f.available(); !ok || got != 1<<31-1 {
    98  		t.Fatalf("size = %d, %v; want %d, true", got, ok, 1<<31-1)
    99  	}
   100  	if f.add(1) {
   101  		t.Fatal("adding 1 to max shouldn't be allowed")
   102  	}
   103  }
   104  
   105  func TestOutFlowAddOverflow(t *testing.T) {
   106  	var f outflow
   107  	f.conn = &connOutflow{}
   108  	f.conn.add(1<<31 - 1)
   109  	if !f.add(0) {
   110  		t.Fatal("failed to add 0")
   111  	}
   112  	if !f.add(-1) {
   113  		t.Fatal("failed to add -1")
   114  	}
   115  	if !f.add(0) {
   116  		t.Fatal("failed to add 0")
   117  	}
   118  	if !f.add(1) {
   119  		t.Fatal("failed to add 1")
   120  	}
   121  	if !f.add(1) {
   122  		t.Fatal("failed to add 1")
   123  	}
   124  	if !f.add(0) {
   125  		t.Fatal("failed to add 0")
   126  	}
   127  	if !f.add(-3) {
   128  		t.Fatal("failed to add -3")
   129  	}
   130  	if got, ok := f.available(); !ok || got != -2 {
   131  		t.Fatalf("size = %d, %v; want -2, true", got, ok)
   132  	}
   133  	if !f.add(1<<31 - 1) {
   134  		t.Fatal("failed to add 2^31-1")
   135  	}
   136  	if got, ok := f.available(); !ok || got != 1+-3+(1<<31-1) {
   137  		t.Fatalf("size = %d, %v; want %d, true", got, ok, 1+-3+(1<<31-1))
   138  	}
   139  
   140  }
   141  

View as plain text