-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixture_test.go
42 lines (33 loc) · 1.03 KB
/
fixture_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package multilimiter_test
import (
"context"
"time"
"github.com/jrboelens/multilimiter"
)
func ContextWithCancel(timeout time.Duration) (context.Context, context.CancelFunc) {
if int64(timeout) <= 0 {
return context.WithCancel(context.Background())
}
return context.WithTimeout(context.Background(), timeout)
}
func Context(timeout time.Duration) context.Context {
ctx, _ := ContextWithCancel(timeout)
return ctx
}
func NewLimiter(rate float64, concurrency int) multilimiter.Limiter {
return multilimiter.DefaultLimiter(rate, concurrency)
}
func NewDefaultLimiter() multilimiter.Limiter {
return NewLimiter(DEFAULT_RATE, DEFAULT_CONCURRENCY)
}
type SlowRateLimiter struct {
multilimiter.TestableRateLimiter
tokens int64
}
func (me *SlowRateLimiter) Wait(ctx context.Context) error {
return me.TestableRateLimiter.XXX_TEST_Wait(me.tokens, ctx)
}
// This is just here to satisfy the interface
func (me *SlowRateLimiter) XXX_TEST_Wait(tokens int64, ctx context.Context) error {
return me.TestableRateLimiter.XXX_TEST_Wait(tokens, ctx)
}