github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/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  
    18  	// Pop all frames. Should get the non-stream requests first,
    19  	// followed by the stream requests in any order.
    20  	var order []FrameWriteRequest
    21  	for {
    22  		wr, ok := ws.Pop()
    23  		if !ok {
    24  			break
    25  		}
    26  		order = append(order, wr)
    27  	}
    28  	t.Logf("got frames: %v", order)
    29  	if len(order) != 6 {
    30  		t.Fatalf("got %d frames, expected 6", len(order))
    31  	}
    32  	if order[0].StreamID() != 0 || order[1].StreamID() != 0 {
    33  		t.Fatal("expected non-stream frames first", order[0], order[1])
    34  	}
    35  	got := make(map[uint32]bool)
    36  	for _, wr := range order[2:] {
    37  		got[wr.StreamID()] = true
    38  	}
    39  	for id := uint32(1); id <= 4; id++ {
    40  		if !got[id] {
    41  			t.Errorf("frame not found for stream %d", id)
    42  		}
    43  	}
    44  }