Skip to content

Commit

Permalink
test: clean up deadlines set in tests (grpc#6506)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Aug 8, 2023
1 parent 9c46304 commit 4832deb
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 160 deletions.
5 changes: 2 additions & 3 deletions test/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"strings"
"sync"
"testing"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -84,7 +83,7 @@ func runUnixTest(t *testing.T, address, target, expectedAuthority string, dialer
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
_, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
if err != nil {
Expand Down Expand Up @@ -202,7 +201,7 @@ func (s) TestColonPortAuthority(t *testing.T) {
t.Fatalf("grpc.Dial(%q) = %v", ss.Target, err)
}
defer cc.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
_, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{})
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions test/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (s) TestCredsBundleFromBalancer(t *testing.T) {

cc := te.clientConn()
tc := testgrpc.NewTestServiceClient(cc)
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("Test failed. Reason: %v", err)
}
}
Expand Down Expand Up @@ -244,7 +246,7 @@ func testDoneInfo(t *testing.T, e env) {
cc := te.clientConn()
tc := testgrpc.NewTestServiceClient(cc)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
wantErr := detailedError
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !testutils.StatusErrEqual(err, wantErr) {
Expand Down Expand Up @@ -321,7 +323,7 @@ func testDoneLoads(t *testing.T) {

tc := testgrpc.NewTestServiceClient(ss.CC)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, nil)
Expand Down Expand Up @@ -438,7 +440,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
t.Log("Created a ClientConn...")

// The first RPC should fail because there's no address.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err == nil || status.Code(err) != codes.DeadlineExceeded {
t.Fatalf("EmptyCall() = _, %v, want _, DeadlineExceeded", err)
Expand All @@ -450,7 +452,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
t.Logf("Pushing resolver state update: %v through the manual resolver", state)

// The second RPC should succeed.
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("EmptyCall() = _, %v, want _, <nil>", err)
Expand Down Expand Up @@ -519,7 +521,7 @@ func (s) TestMetadataInAddressAttributes(t *testing.T) {
defer ss.Stop()

// The RPC should succeed with the expected md.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("EmptyCall() = _, %v, want _, <nil>", err)
Expand All @@ -536,7 +538,7 @@ func (s) TestMetadataInAddressAttributes(t *testing.T) {
// TestServersSwap creates two servers and verifies the client switches between
// them when the name resolver reports the first and then the second.
func (s) TestServersSwap(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

// Initialize servers
Expand Down Expand Up @@ -592,7 +594,7 @@ func (s) TestServersSwap(t *testing.T) {
}

func (s) TestWaitForReady(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

// Initialize server
Expand Down Expand Up @@ -1035,7 +1037,7 @@ func (s) TestBalancerProducerHonorsContext(t *testing.T) {
// rpcErrChan is given to the LB policy to report the status of the
// producer's one RPC.
ctxChan := make(chan context.Context, 1)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
ctxChan <- ctx

rpcErrChan := make(chan error)
Expand Down
49 changes: 28 additions & 21 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,9 @@ func (s) TestCZChannelMetrics(t *testing.T) {
cc := te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
tc := testgrpc.NewTestServiceClient(cc)
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
}

Expand All @@ -540,11 +542,11 @@ func (s) TestCZChannelMetrics(t *testing.T) {
Payload: largePayload,
}

if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
if _, err := tc.UnaryCall(ctx, req); err == nil || status.Code(err) != codes.ResourceExhausted {
t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
}

stream, err := tc.FullDuplexCall(context.Background())
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
}
Expand Down Expand Up @@ -603,7 +605,9 @@ func (s) TestCZServerMetrics(t *testing.T) {
defer te.tearDown()
cc := te.clientConn()
tc := testgrpc.NewTestServiceClient(cc)
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
}

Expand All @@ -619,11 +623,11 @@ func (s) TestCZServerMetrics(t *testing.T) {
ResponseSize: int32(smallSize),
Payload: largePayload,
}
if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
if _, err := tc.UnaryCall(ctx, req); err == nil || status.Code(err) != codes.ResourceExhausted {
t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
}

stream, err := tc.FullDuplexCall(context.Background())
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
}
Expand Down Expand Up @@ -746,7 +750,7 @@ func doServerSideFailedUnaryCall(tc testgrpc.TestServiceClient, t *testing.T) {
}

func doClientSideInitiatedFailedStream(tc testgrpc.TestServiceClient, t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
Expand Down Expand Up @@ -779,7 +783,9 @@ func doClientSideInitiatedFailedStream(tc testgrpc.TestServiceClient, t *testing

// This func is to be used to test client side counting of failed streams.
func doServerSideInitiatedFailedStreamWithRSTStream(tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
stream, err := tc.FullDuplexCall(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
}
Expand Down Expand Up @@ -816,10 +822,10 @@ func doServerSideInitiatedFailedStreamWithRSTStream(tc testgrpc.TestServiceClien
}

// this func is to be used to test client side counting of failed streams.
func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
func doServerSideInitiatedFailedStreamWithGoAway(ctx context.Context, tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
// This call is just to keep the transport from shutting down (socket will be deleted
// in this case, and we will not be able to get metrics).
s, err := tc.FullDuplexCall(context.Background())
s, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
}
Expand All @@ -834,7 +840,7 @@ func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient,
t.Fatalf("s.Recv() failed with error: %v", err)
}

s, err = tc.FullDuplexCall(context.Background())
s, err = tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
}
Expand All @@ -859,7 +865,7 @@ func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient,
}

func doIdleCallToInvokeKeepAlive(tc testgrpc.TestServiceClient, t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
_, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
Expand All @@ -870,6 +876,9 @@ func doIdleCallToInvokeKeepAlive(tc testgrpc.TestServiceClient, t *testing.T) {
}

func (s) TestCZClientSocketMetricsStreamsAndMessagesCount(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

czCleanup := channelz.NewChannelzStorageForTesting()
defer czCleanupWrapper(czCleanup, t)
e := tcpClearRREnv
Expand Down Expand Up @@ -951,7 +960,7 @@ func (s) TestCZClientSocketMetricsStreamsAndMessagesCount(t *testing.T) {
t.Fatal(err)
}

doServerSideInitiatedFailedStreamWithGoAway(tc, t, rcw)
doServerSideInitiatedFailedStreamWithGoAway(ctx, tc, t, rcw)
if err := verifyResultWithDelay(func() (bool, error) {
skt := channelz.GetSocket(skID)
sktData := skt.SocketData
Expand Down Expand Up @@ -988,7 +997,7 @@ func (s) TestCZClientAndServerSocketMetricsStreamsCountFlowControlRSTStream(t *t
cc, dw := te.clientConnWithConnControl()
tc := &testServiceClientWrapper{TestServiceClient: testgrpc.NewTestServiceClient(cc)}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
stream, err := tc.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
Expand Down Expand Up @@ -1534,7 +1543,7 @@ func (s) TestCZSubChannelTraceCreationDeletion(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
awaitState(ctx, t, te.cc, connectivity.Ready)
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "fake address"}}})
Expand Down Expand Up @@ -1694,7 +1703,7 @@ func (s) TestCZSubChannelPickedNewAddress(t *testing.T) {
defer te.tearDown()
tc := testgrpc.NewTestServiceClient(cc)
// make sure the connection is up
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
Expand All @@ -1707,9 +1716,7 @@ func (s) TestCZSubChannelPickedNewAddress(t *testing.T) {
defer close(done)
go func() {
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
tc.EmptyCall(ctx, &testpb.Empty{})
cancel()
select {
case <-time.After(10 * time.Millisecond):
case <-done:
Expand Down Expand Up @@ -1763,7 +1770,7 @@ func (s) TestCZSubChannelConnectivityState(t *testing.T) {
defer te.tearDown()
tc := testgrpc.NewTestServiceClient(cc)
// make sure the connection is up
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
Expand Down Expand Up @@ -1862,7 +1869,7 @@ func (s) TestCZChannelConnectivityState(t *testing.T) {
defer te.tearDown()
tc := testgrpc.NewTestServiceClient(cc)
// make sure the connection is up
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
Expand Down Expand Up @@ -2010,7 +2017,7 @@ func (s) TestCZTraceOverwriteSubChannelDeletion(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
awaitState(ctx, t, te.cc, connectivity.Ready)
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "fake address"}}})
Expand Down
Loading

0 comments on commit 4832deb

Please sign in to comment.