-
Notifications
You must be signed in to change notification settings - Fork 554
[Feature] Add timeout for apiserver grpc server #3427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
c677b80
9cbf138
33cf5da
d34686f
ebb5ba4
cade329
a8fee22
d4a554a
f1b5baf
f8ad90f
4c897aa
aa7fcb5
6425633
79a6349
54c8d57
897ad01
47f872a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,13 @@ import ( | |
"io" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
klog "k8s.io/klog/v2" | ||
) | ||
|
||
|
@@ -20,9 +23,28 @@ type mockHandler struct { | |
called bool | ||
} | ||
|
||
func (h *mockHandler) Handle(_ context.Context, _ interface{}) (interface{}, error) { | ||
// Handle simulates the behavior of a gRPC handler with an optional delay. | ||
// If the delay completes before the context expires, it returns "test_response" along with predefined error. | ||
// If the context is canceled or the deadline is exceeded before the delay completes, | ||
// it returns a corresponding gRPC status error instead. | ||
func (h *mockHandler) Handle(ctx context.Context, _ interface{}, delay time.Duration) (interface{}, error) { | ||
h.called = true | ||
return "test_response", h.returnErr | ||
|
||
select { | ||
case <-time.After(delay): | ||
return "test_response", h.returnErr | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case <-ctx.Done(): | ||
var grpcCode codes.Code | ||
switch ctx.Err() { | ||
case context.Canceled: | ||
grpcCode = codes.Canceled | ||
case context.DeadlineExceeded: | ||
grpcCode = codes.DeadlineExceeded | ||
default: | ||
grpcCode = codes.Unknown | ||
} | ||
return nil, status.Error(grpcCode, ctx.Err().Error()) | ||
} | ||
Comment on lines
+33
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this to mimic the grpc IO handler for testing |
||
} | ||
|
||
func TestAPIServerInterceptor(t *testing.T) { | ||
|
@@ -61,7 +83,7 @@ func TestAPIServerInterceptor(t *testing.T) { | |
req, | ||
info, | ||
func(ctx context.Context, req interface{}) (interface{}, error) { | ||
return tt.handler.Handle(ctx, req) | ||
return tt.handler.Handle(ctx, req, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! Thanks! |
||
}, | ||
) | ||
|
||
|
@@ -96,7 +118,7 @@ func TestAPIServerInterceptorContextPassing(t *testing.T) { | |
func(receivedCtx context.Context, req interface{}) (interface{}, error) { | ||
// Verify context value is passed through | ||
assert.Equal(t, "test_value", receivedCtx.Value(testContextKey("test_key"))) | ||
return handler.Handle(receivedCtx, req) | ||
return handler.Handle(receivedCtx, req, 0) | ||
}, | ||
) | ||
} | ||
|
@@ -153,7 +175,7 @@ func TestAPIServerInterceptorLogging(t *testing.T) { | |
"test_request", | ||
info, | ||
func(receivedCtx context.Context, req interface{}) (interface{}, error) { | ||
return handler.Handle(receivedCtx, req) | ||
return handler.Handle(receivedCtx, req, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add a comment besides constants There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! Thanks! |
||
}, | ||
) | ||
|
||
|
@@ -192,3 +214,62 @@ func TestAPIServerInterceptorLogging(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestTimeoutInterceptor(t *testing.T) { | ||
tests := []struct { | ||
expectedError error | ||
name string | ||
timeout time.Duration | ||
handlerDelay time.Duration | ||
expectedCalled bool | ||
}{ | ||
{ | ||
name: "handler completes before timeout", | ||
timeout: 100 * time.Millisecond, | ||
handlerDelay: 50 * time.Millisecond, | ||
expectedError: nil, | ||
expectedCalled: true, | ||
}, | ||
{ | ||
name: "handler exceeds timeout", | ||
timeout: 50 * time.Millisecond, | ||
handlerDelay: 100 * time.Millisecond, | ||
expectedError: status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), | ||
expectedCalled: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Create test context and request | ||
ctx := context.Background() | ||
req := "test_request" | ||
handler := &mockHandler{} | ||
|
||
// Create the interceptor with the specified timeout | ||
interceptor := TimeoutInterceptor(tt.timeout) | ||
|
||
// Call the interceptor | ||
resp, err := interceptor( | ||
ctx, | ||
req, | ||
&grpc.UnaryServerInfo{FullMethod: "TestTimeoutMethod"}, | ||
func(ctx context.Context, req interface{}) (interface{}, error) { | ||
return handler.Handle(ctx, req, tt.handlerDelay) | ||
}, | ||
) | ||
|
||
// Verify response and error | ||
if tt.expectedError == nil { | ||
// Verify handler was called | ||
assert.Equal(t, tt.expectedCalled, handler.called, "handler call status should match expected") | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, "test_response", resp, "response should match expected") | ||
} else { | ||
require.Error(t, err) | ||
require.Equal(t, tt.expectedError, err, "A matching error is expected") | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is automatically updated when running
make test