writesched_random_test.go 1.6 KB

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