File: rate_test.go

package info (click to toggle)
golang-github-ulule-limiter 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 240 kB
  • sloc: sh: 114; makefile: 15
file content (59 lines) | stat: -rw-r--r-- 950 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package limiter_test

import (
	"reflect"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/ulule/limiter/v3"
)

// TestRate tests Rate methods.
func TestRate(t *testing.T) {
	is := require.New(t)

	expected := map[string]limiter.Rate{
		"10-S": {
			Formatted: "10-S",
			Period:    1 * time.Second,
			Limit:     int64(10),
		},
		"356-M": {
			Formatted: "356-M",
			Period:    1 * time.Minute,
			Limit:     int64(356),
		},
		"3-H": {
			Formatted: "3-H",
			Period:    1 * time.Hour,
			Limit:     int64(3),
		},
		"2000-D": {
			Formatted: "2000-D",
			Period:    24 * time.Hour,
			Limit:     int64(2000),
		},
	}

	for k, v := range expected {
		r, err := limiter.NewRateFromFormatted(k)
		is.NoError(err)
		is.True(reflect.DeepEqual(v, r))
	}

	wrongs := []string{
		"10 S",
		"10:S",
		"AZERTY",
		"na wak",
		"H-10",
	}

	for _, w := range wrongs {
		_, err := limiter.NewRateFromFormatted(w)
		is.Error(err)
	}

}