Skip to content

Commit 15f6613

Browse files
committed
remove timer for hc.Write
1 parent 9636c56 commit 15f6613

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

internal/transport/transport_test.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func Test(t *testing.T) {
5959
grpctest.RunSubTests(t, s{})
6060
}
6161

62+
const goAwayFrameSize = 42
63+
6264
var (
6365
expectedRequest = []byte("ping")
6466
expectedResponse = []byte("pong")
@@ -2759,18 +2761,20 @@ type hangingConn struct {
27592761

27602762
func (hc *hangingConn) Write(b []byte) (n int, err error) {
27612763
n, err = hc.Conn.Write(b)
2762-
if hc.startHanging.Load() == true {
2763-
// Hang the Write for more than goAwayLoopyWriterTimeout
2764-
timer := time.NewTimer(time.Millisecond * 5)
2765-
defer timer.Stop()
2766-
select {
2767-
case <-hc.hangConn:
2768-
case <-timer.C:
2769-
}
2764+
if n == goAwayFrameSize { // hang the conn after the goAway is received
2765+
<-hc.hangConn
27702766
}
27712767
return n, err
27722768
}
27732769

2770+
func hangingDialer(_ context.Context, addr string) (net.Conn, error) {
2771+
conn, err := net.Dial("tcp", addr)
2772+
if err != nil {
2773+
return nil, err
2774+
}
2775+
return &hangingConn{Conn: conn, hangConn: make(chan struct{})}, nil
2776+
}
2777+
27742778
// Tests the scenario where a client transport is closed and writing of the
27752779
// GOAWAY frame as part of the close does not complete because of a network
27762780
// hang. The test verifies that the client transport is closed without waiting
@@ -2786,28 +2790,7 @@ func (s) TestClientCloseReturnsEarlyWhenGoAwayWriteHangs(t *testing.T) {
27862790
}()
27872791

27882792
// Create the server set up.
2789-
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
2790-
server := setUpServerOnly(t, 0, &ServerConfig{}, normal)
2791-
addr := resolver.Address{Addr: "localhost:" + server.port}
2792-
isGreetingDone := &atomic.Bool{}
2793-
dialer := func(_ context.Context, addr string) (net.Conn, error) {
2794-
conn, err := net.Dial("tcp", addr)
2795-
if err != nil {
2796-
return nil, err
2797-
}
2798-
return &hangingConn{Conn: conn, hangConn: make(chan struct{}), startHanging: isGreetingDone}, nil
2799-
}
2800-
copts := ConnectOptions{Dialer: dialer}
2801-
copts.ChannelzParent = channelzSubChannel(t)
2802-
2803-
// Create client transport with custom dialer
2804-
ct, connErr := NewClientTransport(connectCtx, context.Background(), addr, copts, func(GoAwayReason) {})
2805-
if connErr != nil {
2806-
cancel() // Do not cancel in success path.
2807-
t.Fatalf("failed to create transport: %v", connErr)
2808-
}
2809-
isGreetingDone.Store(true)
2810-
2793+
server, ct, cancel := setUpWithOptions(t, 0, &ServerConfig{}, normal, ConnectOptions{Dialer: hangingDialer})
28112794
defer cancel()
28122795
defer server.stop()
28132796

@@ -2816,6 +2799,5 @@ func (s) TestClientCloseReturnsEarlyWhenGoAwayWriteHangs(t *testing.T) {
28162799
if _, err := ct.NewStream(ctx, &CallHdr{}); err != nil {
28172800
t.Fatalf("Failed to open stream: %v", err)
28182801
}
2819-
28202802
ct.Close(errors.New("manually closed by client"))
28212803
}

0 commit comments

Comments
 (0)