writesched_test.go 3.3 KB

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