Files
tasks/common/quotas/delayed_request_rate_limiter_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

73 lines
2.6 KiB
Go

package quotas_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/quotas"
)
// disallowingRateLimiter is a rate limiter whose Allow method always returns false.
type disallowingRateLimiter struct {
// RequestRateLimiter is an embedded field so that disallowingRateLimiter implements that interface. It doesn't
// actually delegate to this rate limiter, and this field should be left nil.
quotas.RequestRateLimiter
}
func (rl disallowingRateLimiter) Allow(time.Time, quotas.Request) bool {
return false
}
func TestNewDelayedRequestRateLimiter_NegativeDelay(t *testing.T) {
t.Parallel()
_, err := quotas.NewDelayedRequestRateLimiter(
quotas.NoopRequestRateLimiter,
-time.Nanosecond,
clock.NewRealTimeSource(),
)
assert.ErrorIs(t, err, quotas.ErrNegativeDelay)
}
func TestNewDelayedRequestRateLimiter_ZeroDelay(t *testing.T) {
t.Parallel()
timeSource := clock.NewEventTimeSource()
drl, err := quotas.NewDelayedRequestRateLimiter(disallowingRateLimiter{}, 0, timeSource)
require.NoError(t, err)
assert.False(t, drl.Allow(time.Time{}, quotas.Request{}), "expected Allow to return false because we "+
"immediately switched to the disallowing rate limiter due to the zero delay")
}
func TestDelayedRequestRateLimiter_Allow(t *testing.T) {
t.Parallel()
timeSource := clock.NewEventTimeSource()
drl, err := quotas.NewDelayedRequestRateLimiter(disallowingRateLimiter{}, time.Second, timeSource)
require.NoError(t, err)
timeSource.Advance(time.Second - time.Nanosecond)
assert.True(t, drl.Allow(time.Time{}, quotas.Request{}), "expected Allow to return true because the "+
"timer hasn't expired yet")
timeSource.Advance(time.Nanosecond)
assert.False(t, drl.Allow(time.Time{}, quotas.Request{}), "expected Allow to return false because the "+
"timer expired, and we switched to the disallowing rate limiter")
}
func TestDelayedRequestRateLimiter_Cancel(t *testing.T) {
t.Parallel()
timeSource := clock.NewEventTimeSource()
drl, err := quotas.NewDelayedRequestRateLimiter(disallowingRateLimiter{}, time.Second, timeSource)
require.NoError(t, err)
timeSource.Advance(time.Second - time.Nanosecond)
assert.True(t, drl.Cancel(), "expected Cancel to return true because the timer was stopped before it "+
"expired")
timeSource.Advance(time.Nanosecond)
assert.True(t, drl.Allow(time.Time{}, quotas.Request{}), "expected Allow to return true because the "+
"timer was stopped before it could expire")
assert.False(t, drl.Cancel(), "expected Cancel to return false because the timer was already stopped")
}