gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/net/http2/writesched_random_test.go (about)

     1  // Copyright 2016 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 TestRandomScheduler(t *testing.T) {
    10  	ws := NewRandomWriteScheduler()
    11  	ws.Push(makeWriteHeadersRequest(3))
    12  	ws.Push(makeWriteHeadersRequest(4))
    13  	ws.Push(makeWriteHeadersRequest(1))
    14  	ws.Push(makeWriteHeadersRequest(2))
    15  	ws.Push(makeWriteNonStreamRequest())
    16  	ws.Push(makeWriteNonStreamRequest())
    17  	ws.Push(makeWriteRSTStream(1))
    18  
    19  	// Pop all frames. Should get the non-stream and RST stream requests first,
    20  	// followed by the stream requests in any order.
    21  	var order []FrameWriteRequest
    22  	for {
    23  		wr, ok := ws.Pop()
    24  		if !ok {
    25  			break
    26  		}
    27  		order = append(order, wr)
    28  	}
    29  	t.Logf("got frames: %v", order)
    30  	if len(order) != 7 {
    31  		t.Fatalf("got %d frames, expected 6", len(order))
    32  	}
    33  	if order[0].StreamID() != 0 || order[1].StreamID() != 0 {
    34  		t.Fatal("expected non-stream frames first", order[0], order[1])
    35  	}
    36  	if _, ok := order[2].write.(StreamError); !ok {
    37  		t.Fatal("expected RST stream frames first", order[2])
    38  	}
    39  	got := make(map[uint32]bool)
    40  	for _, wr := range order[2:] {
    41  		got[wr.StreamID()] = true
    42  	}
    43  	for id := uint32(1); id <= 4; id++ {
    44  		if !got[id] {
    45  			t.Errorf("frame not found for stream %d", id)
    46  		}
    47  	}
    48  
    49  	// Verify that we clean up maps for empty queues in all cases (golang.org/issue/33812)
    50  	const arbitraryStreamID = 123
    51  	ws.Push(makeHandlerPanicRST(arbitraryStreamID))
    52  	rws := ws.(*randomWriteScheduler)
    53  	if got, want := len(rws.sq), 1; got != want {
    54  		t.Fatalf("len of 123 stream = %v; want %v", got, want)
    55  	}
    56  	_, ok := ws.Pop()
    57  	if !ok {
    58  		t.Fatal("expected to be able to Pop")
    59  	}
    60  	if got, want := len(rws.sq), 0; got != want {
    61  		t.Fatalf("len of 123 stream = %v; want %v", got, want)
    62  	}
    63  
    64  }