zrpc timeout & unit tests (#573)

* zrpc timeout & unit tests
This commit is contained in:
Kevin Wan
2021-03-19 18:41:26 +08:00
committed by GitHub
parent 3c6951577d
commit 4884a7b3c6
4 changed files with 119 additions and 3 deletions

View File

@@ -20,6 +20,17 @@ func TestUnaryTimeoutInterceptor(t *testing.T) {
assert.Nil(t, err)
}
func TestUnaryTimeoutInterceptor_panic(t *testing.T) {
interceptor := UnaryTimeoutInterceptor(time.Millisecond * 10)
assert.Panics(t, func() {
_, _ = interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "/",
}, func(ctx context.Context, req interface{}) (interface{}, error) {
panic("any")
})
})
}
func TestUnaryTimeoutInterceptor_timeout(t *testing.T) {
const timeout = time.Millisecond * 10
interceptor := UnaryTimeoutInterceptor(timeout)
@@ -39,3 +50,21 @@ func TestUnaryTimeoutInterceptor_timeout(t *testing.T) {
wg.Wait()
assert.Nil(t, err)
}
func TestUnaryTimeoutInterceptor_timeoutExpire(t *testing.T) {
const timeout = time.Millisecond * 10
interceptor := UnaryTimeoutInterceptor(timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer 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.Equal(t, context.DeadlineExceeded, err)
}