Skip to content

Commit

Permalink
http2: remove clientTester
Browse files Browse the repository at this point in the history
All tests which use clientTester have been converted to use
testClientConn, so delete clientTester.

Change-Id: Id9a88bf7ee6760fada8442d383d5e68455c6dc3e
Reviewed-on: https://go-review.googlesource.com/c/net/+/572815
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
neild committed Mar 20, 2024
1 parent c7877ac commit 448c44f
Showing 1 changed file with 0 additions and 195 deletions.
195 changes: 0 additions & 195 deletions http2/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,53 +822,6 @@ func (fw flushWriter) Write(p []byte) (n int, err error) {
return
}

type clientTester struct {
t *testing.T
tr *Transport
sc, cc net.Conn // server and client conn
fr *Framer // server's framer
settings *SettingsFrame
client func() error
server func() error
}

func newClientTester(t *testing.T) *clientTester {
var dialOnce struct {
sync.Mutex
dialed bool
}
ct := &clientTester{
t: t,
}
ct.tr = &Transport{
TLSClientConfig: tlsConfigInsecure,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
dialOnce.Lock()
defer dialOnce.Unlock()
if dialOnce.dialed {
return nil, errors.New("only one dial allowed in test mode")
}
dialOnce.dialed = true
return ct.cc, nil
},
}

ln := newLocalListener(t)
cc, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
sc, err := ln.Accept()
if err != nil {
t.Fatal(err)
}
ln.Close()
ct.cc = cc
ct.sc = sc
ct.fr = NewFramer(sc, sc)
return ct
}

func newLocalListener(t *testing.T) net.Listener {
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err == nil {
Expand All @@ -881,154 +834,6 @@ func newLocalListener(t *testing.T) net.Listener {
return ln
}

func (ct *clientTester) greet(settings ...Setting) {
buf := make([]byte, len(ClientPreface))
_, err := io.ReadFull(ct.sc, buf)
if err != nil {
ct.t.Fatalf("reading client preface: %v", err)
}
f, err := ct.fr.ReadFrame()
if err != nil {
ct.t.Fatalf("Reading client settings frame: %v", err)
}
var ok bool
if ct.settings, ok = f.(*SettingsFrame); !ok {
ct.t.Fatalf("Wanted client settings frame; got %v", f)
}
if err := ct.fr.WriteSettings(settings...); err != nil {
ct.t.Fatal(err)
}
if err := ct.fr.WriteSettingsAck(); err != nil {
ct.t.Fatal(err)
}
}

func (ct *clientTester) readNonSettingsFrame() (Frame, error) {
for {
f, err := ct.fr.ReadFrame()
if err != nil {
return nil, err
}
if _, ok := f.(*SettingsFrame); ok {
continue
}
return f, nil
}
}

// writeReadPing sends a PING and immediately reads the PING ACK.
// It will fail if any other unread data was pending on the connection,
// aside from SETTINGS frames.
func (ct *clientTester) writeReadPing() error {
data := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
if err := ct.fr.WritePing(false, data); err != nil {
return fmt.Errorf("Error writing PING: %v", err)
}
f, err := ct.readNonSettingsFrame()
if err != nil {
return err
}
p, ok := f.(*PingFrame)
if !ok {
return fmt.Errorf("got a %v, want a PING ACK", f)
}
if p.Flags&FlagPingAck == 0 {
return fmt.Errorf("got a PING, want a PING ACK")
}
if p.Data != data {
return fmt.Errorf("got PING data = %x, want %x", p.Data, data)
}
return nil
}

func (ct *clientTester) inflowWindow(streamID uint32) int32 {
pool := ct.tr.connPoolOrDef.(*clientConnPool)
pool.mu.Lock()
defer pool.mu.Unlock()
if n := len(pool.keys); n != 1 {
ct.t.Errorf("clientConnPool contains %v keys, expected 1", n)
return -1
}
for cc := range pool.keys {
cc.mu.Lock()
defer cc.mu.Unlock()
if streamID == 0 {
return cc.inflow.avail + cc.inflow.unsent
}
cs := cc.streams[streamID]
if cs == nil {
ct.t.Errorf("no stream with id %v", streamID)
return -1
}
return cs.inflow.avail + cs.inflow.unsent
}
return -1
}

func (ct *clientTester) cleanup() {
ct.tr.CloseIdleConnections()

// close both connections, ignore the error if its already closed
ct.sc.Close()
ct.cc.Close()
}

func (ct *clientTester) run() {
var errOnce sync.Once
var wg sync.WaitGroup

run := func(which string, fn func() error) {
defer wg.Done()
if err := fn(); err != nil {
errOnce.Do(func() {
ct.t.Errorf("%s: %v", which, err)
ct.cleanup()
})
}
}

wg.Add(2)
go run("client", ct.client)
go run("server", ct.server)
wg.Wait()

errOnce.Do(ct.cleanup) // clean up if no error
}

func (ct *clientTester) readFrame() (Frame, error) {
return ct.fr.ReadFrame()
}

func (ct *clientTester) firstHeaders() (*HeadersFrame, error) {
for {
f, err := ct.readFrame()
if err != nil {
return nil, fmt.Errorf("ReadFrame while waiting for Headers: %v", err)
}
switch f.(type) {
case *WindowUpdateFrame, *SettingsFrame:
continue
}
hf, ok := f.(*HeadersFrame)
if !ok {
return nil, fmt.Errorf("Got %T; want HeadersFrame", f)
}
return hf, nil
}
}

type countingReader struct {
n *int64
}

func (r countingReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = byte(i)
}
atomic.AddInt64(r.n, int64(len(p)))
return len(p), err
}

func TestTransportReqBodyAfterResponse_200(t *testing.T) { testTransportReqBodyAfterResponse(t, 200) }
func TestTransportReqBodyAfterResponse_403(t *testing.T) { testTransportReqBodyAfterResponse(t, 403) }

Expand Down

0 comments on commit 448c44f

Please sign in to comment.