[#1656] qos: Cover gRPC interceptors with tests
Some checks failed
DCO action / DCO (pull_request) Successful in 32s
Vulncheck / Vulncheck (pull_request) Successful in 1m13s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m33s
Build / Build Components (pull_request) Successful in 1m45s
Tests and linters / Run gofumpt (pull_request) Successful in 3m14s
Tests and linters / Staticcheck (pull_request) Successful in 3m45s
Tests and linters / Tests with -race (pull_request) Successful in 5m0s
Tests and linters / Tests (pull_request) Successful in 5m50s
Tests and linters / gopls check (pull_request) Successful in 6m6s
Tests and linters / Lint (pull_request) Failing after 14m43s

Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
This commit is contained in:
Ekaterina Lebedeva 2025-03-18 08:37:50 +03:00
parent 54ef71a92f
commit 75f7c8d034

160
internal/qos/grpc_test.go Normal file
View file

@ -0,0 +1,160 @@
package qos
import (
"context"
"errors"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-qos/limiting"
"git.frostfs.info/TrueCloudLab/frostfs-qos/tagging"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
const (
okKey = "ok"
)
var (
errTest = errors.New("mock")
errResExhausted = new(apistatus.ResourceExhausted)
releaseFunc func()
ctxNoTag = ctxKey("")
)
type ctxKey string
type mockGRPCServerStream struct {
grpc.ServerStream
ctx context.Context
}
func (m *mockGRPCServerStream) Context() context.Context {
return m.ctx
}
type limiter struct{}
func (l *limiter) Acquire(key string) (limiting.ReleaseFunc, bool) {
if key != okKey {
return nil, false
}
return releaseFunc, true
}
// MaxActiveRPCLimiter && SetCriticalIOTag
func TestUnaryServerInterceptor_MaxActiveRPCLimiter_Fail(t *testing.T) {
interceptor := NewMaxActiveRPCLimiterUnaryServerInterceptor(func() limiting.Limiter { return &limiter{} })
pCtx := context.WithValue(context.Background(), ctxNoTag, true)
called := false
handler := func(ctx context.Context, req any) (any, error) {
called = true
return nil, errTest
}
// fail: get apistatus.ResourceExhausted
_, err := interceptor(pCtx, nil, &grpc.UnaryServerInfo{FullMethod: ""}, handler)
require.Error(t, err)
require.Equal(t, err.Error(), errResExhausted.Error())
require.False(t, called)
}
func TestUnaryServerInterceptor_MaxActiveRPCLimiter_PassCritical(t *testing.T) {
interceptor := NewMaxActiveRPCLimiterUnaryServerInterceptor(func() limiting.Limiter { return &limiter{} })
ctx := tagging.ContextWithIOTag(context.Background(), IOTagCritical.String())
called := false
handler := func(ctx context.Context, req any) (any, error) {
called = true
return nil, errTest
}
released := false
releaseFunc = func() {
released = true
}
_, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{FullMethod: ""}, handler)
require.EqualError(t, err, errTest.Error())
require.True(t, called)
require.False(t, released)
}
func TestUnaryServerInterceptor_MaxActiveRPCLimiter_Pass(t *testing.T) {
interceptor := NewMaxActiveRPCLimiterUnaryServerInterceptor(func() limiting.Limiter { return &limiter{} })
pCtx := context.WithValue(context.Background(), ctxNoTag, true)
called := false
handler := func(ctx context.Context, req any) (any, error) {
called = true
return nil, errTest
}
released := false
releaseFunc = func() {
released = true
}
_, err := interceptor(pCtx, nil, &grpc.UnaryServerInfo{FullMethod: okKey}, handler)
require.EqualError(t, err, errTest.Error())
require.True(t, called && released)
}
func TestStreamServerInterceptor_MaxActiveRPCLimiter_Fail(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxNoTag, true)
interceptor := NewMaxActiveRPCLimiterStreamServerInterceptor(func() limiting.Limiter { return &limiter{} })
called := false
handler := func(srv any, stream grpc.ServerStream) error {
called = true
return errTest
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, &grpc.StreamServerInfo{
FullMethod: "",
}, handler)
require.Error(t, err)
require.Equal(t, err.Error(), errResExhausted.Error())
require.False(t, called)
}
func TestStreamServerInterceptor_MaxActiveRPCLimiter_PassCritical(t *testing.T) {
interceptor := NewMaxActiveRPCLimiterStreamServerInterceptor(func() limiting.Limiter { return &limiter{} })
ctx := tagging.ContextWithIOTag(context.Background(), IOTagCritical.String())
called := false
handler := func(srv any, stream grpc.ServerStream) error {
called = true
return errTest
}
released := false
releaseFunc = func() {
released = true
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, &grpc.StreamServerInfo{FullMethod: okKey}, handler)
require.EqualError(t, err, errTest.Error())
require.True(t, called)
require.False(t, released)
}
func TestStreamServerInterceptor_MaxActiveRPCLimiter_Pass(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxNoTag, true)
interceptor := NewMaxActiveRPCLimiterStreamServerInterceptor(func() limiting.Limiter { return &limiter{} })
called := false
handler := func(srv any, stream grpc.ServerStream) error {
called = true
return errTest
}
released := false
releaseFunc = func() {
released = true
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, &grpc.StreamServerInfo{FullMethod: okKey}, handler)
require.EqualError(t, err, errTest.Error())
require.True(t, called && released)
}
func TestSetCriticalIOTagUnaryServerInterceptor_Pass(t *testing.T) {
interceptor := NewSetCriticalIOTagUnaryServerInterceptor()
handler := func(ctx context.Context, req any) (any, error) {
if tag, ok := tagging.IOTagFromContext(ctx); ok && tag == IOTagCritical.String() {
return nil, nil
}
return nil, errTest
}
_, err := interceptor(context.Background(), nil, nil, handler)
require.NoError(t, err)
}