mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/stretchr/testify/suite"
|
|
"github.com/hanzoai/tasks/common/quotas"
|
|
"go.uber.org/mock/gomock"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type (
|
|
// rateLimitInterceptorSuite struct {
|
|
rateLimitInterceptorSuite struct {
|
|
suite.Suite
|
|
*require.Assertions
|
|
|
|
controller *gomock.Controller
|
|
mockRateLimiter *quotas.MockRequestRateLimiter
|
|
}
|
|
)
|
|
|
|
func TestRateLimitInterceptorSuite(t *testing.T) {
|
|
suite.Run(t, &rateLimitInterceptorSuite{})
|
|
}
|
|
|
|
func (s *rateLimitInterceptorSuite) SetupTest() {
|
|
s.Assertions = require.New(s.T())
|
|
s.controller = gomock.NewController(s.T())
|
|
s.mockRateLimiter = quotas.NewMockRequestRateLimiter(s.controller)
|
|
}
|
|
|
|
func (s *rateLimitInterceptorSuite) TestInterceptWithTokenConfig() {
|
|
methodName := "TEST/METHOD"
|
|
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, map[string]int{methodName: 0})
|
|
// mock rate limiter should not be called.
|
|
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(0).Return(false)
|
|
|
|
handlerCalled := false
|
|
handler := func(ctx context.Context, req any) (any, error) {
|
|
handlerCalled = true
|
|
return nil, nil
|
|
}
|
|
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: methodName}, handler)
|
|
s.NoError(err)
|
|
s.True(handlerCalled)
|
|
}
|
|
|
|
func (s *rateLimitInterceptorSuite) TestInterceptWithNoTokenConfig() {
|
|
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, nil)
|
|
// mock rate limiter is set to blocking.
|
|
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(1).Return(false)
|
|
|
|
handlerCalled := false
|
|
handler := func(ctx context.Context, req any) (any, error) {
|
|
handlerCalled = true
|
|
return nil, nil
|
|
}
|
|
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{}, handler)
|
|
s.Error(err)
|
|
s.False(handlerCalled)
|
|
}
|
|
|
|
func (s *rateLimitInterceptorSuite) TestInterceptWithNonZeroTokenConfig() {
|
|
methodName := "TEST/METHOD"
|
|
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, map[string]int{methodName: 100})
|
|
// mock rate limiter is set to non-blocking.
|
|
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(1).Return(true)
|
|
|
|
handlerCalled := false
|
|
handler := func(ctx context.Context, req any) (any, error) {
|
|
handlerCalled = true
|
|
return nil, nil
|
|
}
|
|
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: methodName}, handler)
|
|
s.NoError(err)
|
|
s.True(handlerCalled)
|
|
}
|