Skip to content

Commit

Permalink
Undo refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Jul 8, 2024
1 parent b8b3cc9 commit 2f123d4
Showing 1 changed file with 83 additions and 9 deletions.
92 changes: 83 additions & 9 deletions internal/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2660,10 +2660,90 @@ func TestConnectionError_Unwrap(t *testing.T) {
// clientTransport.Close(), client sends a goaway to the server with the correct
// error code and debug data.
func (s) TestClientSendsAGoAwayFrame(t *testing.T) {
errorCh := createClientServerLoggingGoAway(t)
err := <-errorCh
// Create a server.
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Error while listening: %v", err)
}
defer lis.Close()
// greetDone is used to notify when server is done greeting the client.
greetDone := make(chan struct{})
// errorCh verifies that desired GOAWAY not received by server
errorCh := make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
// Launch the server.
go func() {
sconn, err := lis.Accept()
if err != nil {
t.Errorf("Error while accepting: %v", err)
}
defer sconn.Close()
if _, err := io.ReadFull(sconn, make([]byte, len(clientPreface))); err != nil {
t.Errorf("Error while writing settings ack: %v", err)
return
}
sfr := http2.NewFramer(sconn, sconn)
if err := sfr.WriteSettings(); err != nil {
t.Errorf("Error while writing settings %v", err)
return
}
fr, _ := sfr.ReadFrame()
if _, ok := fr.(*http2.SettingsFrame); !ok {
t.Errorf("Expected settings frame, got %v", fr)
}
fr, _ = sfr.ReadFrame()
if fr, ok := fr.(*http2.SettingsFrame); !ok || !fr.IsAck() {
t.Errorf("Expected settings ACK frame, got %v", fr)
}
fr, _ = sfr.ReadFrame()
if fr, ok := fr.(*http2.HeadersFrame); !ok || !fr.Flags.Has(http2.FlagHeadersEndHeaders) {
t.Errorf("Expected Headers frame with END_HEADERS frame, got %v", fr)
}
close(greetDone)

frame, err := sfr.ReadFrame()
if err != nil {
return
}
switch fr := frame.(type) {
case *http2.GoAwayFrame:
// Records that the server successfully received a GOAWAY frame.
goAwayFrame := fr
if goAwayFrame.ErrCode == http2.ErrCodeNo {
t.Logf("Received goAway frame from client")
close(errorCh)
} else {
errorCh <- fmt.Errorf("received unexpected goAway frame: %v", err)
close(errorCh)
}
return
default:
errorCh <- fmt.Errorf("server received a frame other than GOAWAY: %v", err)
close(errorCh)
return
}
}()

ct, err := NewClientTransport(ctx, context.Background(), resolver.Address{Addr: lis.Addr().String()}, ConnectOptions{}, func(GoAwayReason) {})
if err != nil {
t.Fatalf("Error while creating client transport: %v", err)
}
_, err = ct.NewStream(ctx, &CallHdr{})
if err != nil {
t.Errorf("Error receiving the GOAWAY frame: %v", err)
t.Fatalf("failed to open stream: %v", err)
}
// Wait until server receives the headers and settings frame as part of greet.
<-greetDone
ct.Close(errors.New("manually closed by client"))
t.Logf("Closed the client connection")
select {
case err := <-errorCh:
if err != nil {
t.Errorf("Error receiving the GOAWAY frame: %v", err)
}
case <-ctx.Done():
t.Errorf("Context timed out")
}
}

Expand All @@ -2677,12 +2757,7 @@ func (s) TestClientCloseTimeoutOnHang(t *testing.T) {
defer func() {
GoAwayLoopyWriterTimeout = origGoAwayLoopyTimeout
}()
createClientServerLoggingGoAway(t)
}

// createClientServerLoggingGoAway sets up a server(that expects a GOAWAY frame
// from the client.), and creates a ClientTransport .
func createClientServerLoggingGoAway(t *testing.T) chan error {
// Create a server.
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
Expand Down Expand Up @@ -2762,5 +2837,4 @@ func createClientServerLoggingGoAway(t *testing.T) chan error {
case <-ctx.Done():
t.Errorf("Context timed out")
}
return errorCh
}

0 comments on commit 2f123d4

Please sign in to comment.