Files
tasks/common/persistence/client/quotas_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -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"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/quotas"
"go.temporal.io/server/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 i := 0; i < 2; i++ {
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 i := 0; i < 2; i++ {
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 i := 0; i < 6; i++ {
if !limiter.Allow(requestTime, apiRequest) {
wasLimited = true
s.True(limiter.Allow(requestTime, operatorRequest))
}
}
s.True(wasLimited)
}