-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimit_test.go
More file actions
33 lines (24 loc) · 898 Bytes
/
ratelimit_test.go
File metadata and controls
33 lines (24 loc) · 898 Bytes
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
// Copyright (C) 2021-2022 Amuzed GmbH finn@amuzed.io.
// This file is part of the project AMUZED.
// AMUZED can not be copied and/or distributed without the express.
// permission of Amuzed GmbH.
package ratelimit_test
import (
"testing"
"time"
ratelimit "github.com/BoostyLabs/rate-limit"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestRateLimiter(t *testing.T) {
config := ratelimit.Config{LimitForBet: time.Second * 30}
rateLimiter := ratelimit.NewRateLimiter(config)
key := uuid.New()
_ = rateLimiter.SetLimit(key)
isAllowed := rateLimiter.IsAllowed(key, time.Now().UTC())
assert.False(t, isAllowed)
isAllowed = rateLimiter.IsAllowed(key, time.Now().UTC().Add(rateLimiter.GetDuration()-time.Second))
assert.False(t, isAllowed)
isAllowed = rateLimiter.IsAllowed(key, time.Now().UTC().Add(rateLimiter.GetDuration()))
assert.True(t, isAllowed)
}