writesched_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 (
  6. "fmt"
  7. "math"
  8. "reflect"
  9. "testing"
  10. )
  11. func makeWriteNonStreamRequest() FrameWriteRequest {
  12. return FrameWriteRequest{writeSettingsAck{}, nil, nil}
  13. }
  14. func makeWriteHeadersRequest(streamID uint32) FrameWriteRequest {
  15. st := &stream{id: streamID}
  16. return FrameWriteRequest{&writeResHeaders{streamID: streamID, httpResCode: 200}, st, nil}
  17. }
  18. func makeHandlerPanicRST(streamID uint32) FrameWriteRequest {
  19. st := &stream{id: streamID}
  20. return FrameWriteRequest{&handlerPanicRST{StreamID: streamID}, st, nil}
  21. }
  22. func checkConsume(wr FrameWriteRequest, nbytes int32, want []FrameWriteRequest) error {
  23. consumed, rest, n := wr.Consume(nbytes)
  24. var wantConsumed, wantRest FrameWriteRequest
  25. switch len(want) {
  26. case 0:
  27. case 1:
  28. wantConsumed = want[0]
  29. case 2:
  30. wantConsumed = want[0]
  31. wantRest = want[1]
  32. }
  33. if !reflect.DeepEqual(consumed, wantConsumed) || !reflect.DeepEqual(rest, wantRest) || n != len(want) {
  34. return fmt.Errorf("got %v, %v, %v\nwant %v, %v, %v", consumed, rest, n, wantConsumed, wantRest, len(want))
  35. }
  36. return nil
  37. }
  38. func TestFrameWriteRequestNonData(t *testing.T) {
  39. wr := makeWriteNonStreamRequest()
  40. if got, want := wr.DataSize(), 0; got != want {
  41. t.Errorf("DataSize: got %v, want %v", got, want)
  42. }
  43. // Non-DATA frames are always consumed whole.
  44. if err := checkConsume(wr, 0, []FrameWriteRequest{wr}); err != nil {
  45. t.Errorf("Consume:\n%v", err)
  46. }
  47. }
  48. func TestFrameWriteRequestData(t *testing.T) {
  49. st := &stream{
  50. id: 1,
  51. sc: &serverConn{maxFrameSize: 16},
  52. }
  53. const size = 32
  54. wr := FrameWriteRequest{&writeData{st.id, make([]byte, size), true}, st, make(chan error)}
  55. if got, want := wr.DataSize(), size; got != want {
  56. t.Errorf("DataSize: got %v, want %v", got, want)
  57. }
  58. // No flow-control bytes available: cannot consume anything.
  59. if err := checkConsume(wr, math.MaxInt32, []FrameWriteRequest{}); err != nil {
  60. t.Errorf("Consume(limited by flow control):\n%v", err)
  61. }
  62. // Add enough flow-control bytes to consume the entire frame,
  63. // but we're now restricted by st.sc.maxFrameSize.
  64. st.flow.add(size)
  65. want := []FrameWriteRequest{
  66. {
  67. write: &writeData{st.id, make([]byte, st.sc.maxFrameSize), false},
  68. stream: st,
  69. done: nil,
  70. },
  71. {
  72. write: &writeData{st.id, make([]byte, size-st.sc.maxFrameSize), true},
  73. stream: st,
  74. done: wr.done,
  75. },
  76. }
  77. if err := checkConsume(wr, math.MaxInt32, want); err != nil {
  78. t.Errorf("Consume(limited by maxFrameSize):\n%v", err)
  79. }
  80. rest := want[1]
  81. // Consume 8 bytes from the remaining frame.
  82. want = []FrameWriteRequest{
  83. {
  84. write: &writeData{st.id, make([]byte, 8), false},
  85. stream: st,
  86. done: nil,
  87. },
  88. {
  89. write: &writeData{st.id, make([]byte, size-st.sc.maxFrameSize-8), true},
  90. stream: st,
  91. done: wr.done,
  92. },
  93. }
  94. if err := checkConsume(rest, 8, want); err != nil {
  95. t.Errorf("Consume(8):\n%v", err)
  96. }
  97. rest = want[1]
  98. // Consume all remaining bytes.
  99. want = []FrameWriteRequest{
  100. {
  101. write: &writeData{st.id, make([]byte, size-st.sc.maxFrameSize-8), true},
  102. stream: st,
  103. done: wr.done,
  104. },
  105. }
  106. if err := checkConsume(rest, math.MaxInt32, want); err != nil {
  107. t.Errorf("Consume(remainder):\n%v", err)
  108. }
  109. }
  110. func TestFrameWriteRequest_StreamID(t *testing.T) {
  111. const streamID = 123
  112. wr := FrameWriteRequest{write: streamError(streamID, ErrCodeNo)}
  113. if got := wr.StreamID(); got != streamID {
  114. t.Errorf("FrameWriteRequest(StreamError) = %v; want %v", got, streamID)
  115. }
  116. }