Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley committed Oct 16, 2018
1 parent d1afa25 commit 5727753
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6790,36 +6790,35 @@ func testLargeTimeout(t *testing.T, e env) {
te := newTest(t, e)
te.declareLogNoise("Server.processUnaryRPC failed to write status")

var maxTimeout time.Duration
ts := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
deadline, ok := ctx.Deadline()
timeout := deadline.Sub(time.Now())
minTimeout := maxTimeout - 5*time.Second
if !ok || timeout < minTimeout || timeout > maxTimeout {
t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout)
}
return &testpb.SimpleResponse{}, nil
}}
ts := &funcServer{}
te.startServer(ts)
defer te.tearDown()
tc := testpb.NewTestServiceClient(te.clientConn())

// MaxInt64 will be (correctly) converted to 2562048 hours, which overflows
// upon converting back to an int64.
maxTimeout = time.Duration(math.MaxInt64)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(math.MaxInt64))
defer cancel()

if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
t.Fatalf("UnaryCall(_) = _, %v; want _, nil", err)
}
timeouts := []time.Duration{
time.Duration(math.MaxInt64), // MaxInt64 will be (correctly)
// converted to 2562048 hours, which overflows upon converting back to
// an int64.
2562047 * time.Hour, // The largest timeout that does not overflow.
}

for i, maxTimeout := range timeouts {
ts.unaryCall = func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
deadline, ok := ctx.Deadline()
timeout := deadline.Sub(time.Now())
minTimeout := maxTimeout - 5*time.Second
if !ok || timeout < minTimeout || timeout > maxTimeout {
t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout)
return nil, status.Error(codes.OutOfRange, "deadline error")
}
return &testpb.SimpleResponse{}, nil
}

// Test with the largest timeout that does not overflow.
maxTimeout = 2562047 * time.Hour
ctx, cancel = context.WithTimeout(context.Background(), 2562047*time.Hour)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), maxTimeout)
defer cancel()

if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
t.Fatalf("UnaryCall(_) = _, %v; want _, nil", err)
if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
t.Errorf("case %v: UnaryCall(_) = _, %v; want _, nil", i, err)
}
}
}

0 comments on commit 5727753

Please sign in to comment.