Fix context error in grpc (#962)

* Fix context error in rpc

* Add a test case

* Optimize judgment conditions

* Add customized breaker errors for the client and server

* Update method signature

* Delete customized breaker errors

* Delete the wrong test case
This commit is contained in:
chenquan
2021-08-28 12:07:14 +08:00
committed by GitHub
parent 519db812b4
commit dfb3cb510a
4 changed files with 33 additions and 15 deletions

View File

@@ -2,6 +2,8 @@ package serverinterceptors
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sync"
"testing"
"time"
@@ -66,5 +68,24 @@ func TestUnaryTimeoutInterceptor_timeoutExpire(t *testing.T) {
return nil, nil
})
wg.Wait()
assert.Equal(t, context.DeadlineExceeded, err)
assert.EqualValues(t, status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), err)
}
func TestUnaryTimeoutInterceptor_cancel(t *testing.T) {
const timeout = time.Minute * 10
interceptor := UnaryTimeoutInterceptor(timeout)
ctx, cancel := context.WithCancel(context.Background())
cancel()
var wg sync.WaitGroup
wg.Add(1)
_, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
FullMethod: "/",
}, func(ctx context.Context, req interface{}) (interface{}, error) {
defer wg.Done()
time.Sleep(time.Millisecond * 50)
return nil, nil
})
wg.Wait()
assert.EqualValues(t, status.Error(codes.Canceled, context.Canceled.Error()), err)
}