Files
tasks/common/persistence/client/quotas_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
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).
2026-03-19 10:36:40 -07:00

190 lines
4.0 KiB
Go

package client
import (
"reflect"
"slices"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/api/workflowservice/v1"
"github.com/hanzoai/tasks/common/headers"
"github.com/hanzoai/tasks/common/quotas"
"github.com/hanzoai/tasks/common/testing/temporalapi"
)
type (
quotasSuite struct {
suite.Suite
*require.Assertions
}
)
func TestQuotasSuite(t *testing.T) {
s := new(quotasSuite)
suite.Run(t, s)
}
func (s *quotasSuite) SetupSuite() {
}
func (s *quotasSuite) TearDownSuite() {
}
func (s *quotasSuite) SetupTest() {
s.Assertions = require.New(s.T())
}
func (s *quotasSuite) TearDownTest() {
}
func (s *quotasSuite) TestCallerTypeDefaultPriorityMapping() {
for _, priority := range CallerTypeDefaultPriority {
index := slices.Index(RequestPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestAPITypeCallOriginPriorityOverrideMapping() {
for _, priority := range APITypeCallOriginPriorityOverride {
index := slices.Index(RequestPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestBackgroundTypeAPIPriorityOverrideMapping() {
for _, priority := range BackgroundTypeAPIPriorityOverride {
index := slices.Index(RequestPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestRequestPrioritiesOrdered() {
for idx := range RequestPrioritiesOrdered[1:] {
s.True(RequestPrioritiesOrdered[idx] < RequestPrioritiesOrdered[idx+1])
}
}
func (s *quotasSuite) TestCallOriginDefined() {
var service workflowservice.WorkflowServiceServer
definedAPIs := make(map[string]struct{})
temporalapi.WalkExportedMethods(&service, func(m reflect.Method) {
definedAPIs[m.Name] = struct{}{}
})
for api := range APITypeCallOriginPriorityOverride {
_, ok := definedAPIs[api]
s.True(ok)
}
}
func (s *quotasSuite) TestPriorityNamespaceRateLimiter_DoesLimit() {
namespaceMaxRPS := func(namespace string) int { return 1 }
hostMaxRPS := func() int { return 1 }
operatorRPSRatioFn := func() float64 { return 0.2 }
burstRatio := func() float64 { return 1 }
limiter := newPriorityNamespaceRateLimiter(
namespaceMaxRPS,
hostMaxRPS,
RequestPriorityFn,
operatorRPSRatioFn,
burstRatio,
)
request := quotas.NewRequest(
"test-api",
1,
"test-namespace",
"api",
-1,
"frontend",
)
requestTime := time.Now()
wasLimited := false
for range 2 {
if !limiter.Allow(requestTime, request) {
wasLimited = true
}
}
s.True(wasLimited)
}
func (s *quotasSuite) TestPerShardNamespaceRateLimiter_DoesLimit() {
perShardNamespaceMaxRPS := func(namespace string) int { return 1 }
hostMaxRPS := func() int { return 1 }
operatorRPSRatioFn := func() float64 { return 0.2 }
burstRatio := func() float64 { return 1 }
limiter := newPerShardPerNamespacePriorityRateLimiter(
perShardNamespaceMaxRPS,
hostMaxRPS,
RequestPriorityFn,
operatorRPSRatioFn,
burstRatio,
)
request := quotas.NewRequest(
"test-api",
1,
"test-namespace",
"api",
1,
"frontend",
)
requestTime := time.Now()
wasLimited := false
for range 2 {
if !limiter.Allow(requestTime, request) {
wasLimited = true
}
}
s.True(wasLimited)
}
func (s *quotasSuite) TestOperatorPrioritized() {
rateFn := func() float64 { return 5 }
operatorRPSRatioFn := func() float64 { return 0.2 }
burstRatio := func() float64 { return 1 }
limiter := newPriorityRateLimiter(
rateFn,
RequestPriorityFn,
operatorRPSRatioFn,
burstRatio,
)
operatorRequest := quotas.NewRequest(
"DescribeWorkflowExecution",
1,
"test-namespace",
headers.CallerTypeOperator,
-1,
"DescribeWorkflowExecution")
apiRequest := quotas.NewRequest(
"DescribeWorkflowExecution",
1,
"test-namespace",
headers.CallerTypeAPI,
-1,
"DescribeWorkflowExecution")
requestTime := time.Now()
wasLimited := false
for range 6 {
if !limiter.Allow(requestTime, apiRequest) {
wasLimited = true
s.True(limiter.Allow(requestTime, operatorRequest))
}
}
s.True(wasLimited)
}