Skip to content

Commit b1efff6

Browse files
authored
rpc: improve cancel test (#20752)
This is supposed to fix the occasional failures in TestCancel* on Travis CI.
1 parent 0bdb21f commit b1efff6

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

rpc/client_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func testClientCancel(transport string, t *testing.T) {
179179
var (
180180
wg sync.WaitGroup
181181
nreqs = 10
182-
ncallers = 6
182+
ncallers = 10
183183
)
184184
caller := func(index int) {
185185
defer wg.Done()
@@ -200,14 +200,16 @@ func testClientCancel(transport string, t *testing.T) {
200200
// deadline.
201201
ctx, cancel = context.WithTimeout(context.Background(), timeout)
202202
}
203+
203204
// Now perform a call with the context.
204205
// The key thing here is that no call will ever complete successfully.
205-
sleepTime := maxContextCancelTimeout + 20*time.Millisecond
206-
err := client.CallContext(ctx, nil, "test_sleep", sleepTime)
207-
if err != nil {
208-
log.Debug(fmt.Sprint("got expected error:", err))
209-
} else {
210-
t.Errorf("no error for call with %v wait time", timeout)
206+
err := client.CallContext(ctx, nil, "test_block")
207+
switch {
208+
case err == nil:
209+
_, hasDeadline := ctx.Deadline()
210+
t.Errorf("no error for call with %v wait time (deadline: %v)", timeout, hasDeadline)
211+
// default:
212+
// t.Logf("got expected error with %v wait time: %v", timeout, err)
211213
}
212214
cancel()
213215
}

rpc/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestServerRegisterName(t *testing.T) {
4545
t.Fatalf("Expected service calc to be registered")
4646
}
4747

48-
wantCallbacks := 7
48+
wantCallbacks := 8
4949
if len(svc.callbacks) != wantCallbacks {
5050
t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks))
5151
}

rpc/testservice_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func (s *testService) Sleep(ctx context.Context, duration time.Duration) {
7777
time.Sleep(duration)
7878
}
7979

80+
func (s *testService) Block(ctx context.Context) error {
81+
<-ctx.Done()
82+
return errors.New("context canceled in testservice_block")
83+
}
84+
8085
func (s *testService) Rets() (string, error) {
8186
return "", nil
8287
}

0 commit comments

Comments
 (0)