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