Skip to content

Commit 74efc67

Browse files
committed
Revert the atomic boolean for hangingConn.Write()
1 parent aebe562 commit 74efc67

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

internal/transport/transport_test.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"strconv"
3333
"strings"
3434
"sync"
35+
"sync/atomic"
3536
"testing"
3637
"time"
3738

@@ -58,8 +59,6 @@ func Test(t *testing.T) {
5859
grpctest.RunSubTests(t, s{})
5960
}
6061

61-
const goAwayFrameSize = 42
62-
6362
var (
6463
expectedRequest = []byte("ping")
6564
expectedResponse = []byte("pong")
@@ -2754,25 +2753,18 @@ func (s) TestClientSendsAGoAwayFrame(t *testing.T) {
27542753
// signaled or a timeout occurs.
27552754
type hangingConn struct {
27562755
net.Conn
2757-
hangConn chan struct{}
2756+
hangConn chan struct{}
2757+
startHanging *atomic.Bool
27582758
}
27592759

27602760
func (hc *hangingConn) Write(b []byte) (n int, err error) {
27612761
n, err = hc.Conn.Write(b)
2762-
if n == goAwayFrameSize { // hang the conn after the goAway is received
2762+
if hc.startHanging.Load() == true {
27632763
<-hc.hangConn
27642764
}
27652765
return n, err
27662766
}
27672767

2768-
func hangingDialer(_ context.Context, addr string) (net.Conn, error) {
2769-
conn, err := net.Dial("tcp", addr)
2770-
if err != nil {
2771-
return nil, err
2772-
}
2773-
return &hangingConn{Conn: conn, hangConn: make(chan struct{})}, nil
2774-
}
2775-
27762768
// Tests the scenario where a client transport is closed and writing of the
27772769
// GOAWAY frame as part of the close does not complete because of a network
27782770
// hang. The test verifies that the client transport is closed without waiting
@@ -2788,7 +2780,27 @@ func (s) TestClientCloseReturnsEarlyWhenGoAwayWriteHangs(t *testing.T) {
27882780
}()
27892781

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

@@ -2797,5 +2809,8 @@ func (s) TestClientCloseReturnsEarlyWhenGoAwayWriteHangs(t *testing.T) {
27972809
if _, err := ct.NewStream(ctx, &CallHdr{}); err != nil {
27982810
t.Fatalf("Failed to open stream: %v", err)
27992811
}
2812+
2813+
isGreetingDone.Store(true)
28002814
ct.Close(errors.New("manually closed by client"))
2815+
close(hangConn)
28012816
}

0 commit comments

Comments
 (0)