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

73 lines
2.6 KiB
Go

package quotas_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hanzoai/tasks/common/clock"
"github.com/hanzoai/tasks/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")
}