Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
net
RateLimit.ut.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2021 Reactive Markets Limited
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
// http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
16
#include "
RateLimit.hpp
"
17
18
#include <
toolbox/util/String.hpp
>
19
20
#include <boost/test/unit_test.hpp>
21
22
using namespace
std
;
23
using namespace
toolbox
;
24
25
BOOST_AUTO_TEST_SUITE
(
RateLimitSuite
)
26
27
BOOST_AUTO_TEST_CASE
(
RateLimitCase
)
28
{
29
BOOST_CHECK_EQUAL
(
to_string
(
RateLimit
(1, 5
s
)),
"1/50"
);
30
const
auto
rl
=
from_string<RateLimit>
(
"3/5"
);
31
BOOST_CHECK_EQUAL
(
rl
.limit(), 3);
32
BOOST_CHECK_EQUAL
(
rl
.interval().count(), 50);
33
}
34
35
BOOST_AUTO_TEST_CASE
(
RateWindowCase
)
36
{
37
const
auto
t
= MonoClock::now();
38
39
RateWindow
rw
{1
s
};
40
BOOST_CHECK_EQUAL
(
rw
.count(), 0);
41
42
rw
.add(
t
+ 0
s
, 1);
43
BOOST_CHECK_EQUAL
(
rw
.count(), 1);
44
rw
.add(
t
+ 100
ms
, 2);
45
BOOST_CHECK_EQUAL
(
rw
.count(), 3);
46
// The first bucket is cleaned.
47
rw
.add(
t
+ 1
s
, 1);
48
BOOST_CHECK_EQUAL
(
rw
.count(), 3);
49
rw
.add(
t
+ 1100
ms
, 1);
50
BOOST_CHECK_EQUAL
(
rw
.count(), 2);
51
rw
.add(
t
+ 2
s
, 2);
52
BOOST_CHECK_EQUAL
(
rw
.count(), 3);
53
rw
.add(
t
+ 3
s
, 3);
54
BOOST_CHECK_EQUAL
(
rw
.count(), 3);
55
56
rw
=
RateWindow
{5
s
};
57
BOOST_CHECK_EQUAL
(
rw
.count(), 0);
58
59
rw
.add(
t
+ 0
s
, 1);
60
BOOST_CHECK_EQUAL
(
rw
.count(), 1);
61
rw
.add(
t
+ 1
s
, 2);
62
BOOST_CHECK_EQUAL
(
rw
.count(), 3);
63
rw
.add(
t
+ 2
s
, 3);
64
BOOST_CHECK_EQUAL
(
rw
.count(), 6);
65
rw
.add(
t
+ 3
s
, 4);
66
BOOST_CHECK_EQUAL
(
rw
.count(), 10);
67
rw
.add(
t
+ 4
s
, 5);
68
BOOST_CHECK_EQUAL
(
rw
.count(), 15);
69
70
// Circular buffer wraps and replaces first entry.
71
rw
.add(
t
+ 5
s
, 6);
72
BOOST_CHECK_EQUAL
(
rw
.count(), 20);
73
74
// Skip two entries entirely.
75
rw
.add(
t
+ 8
s
, 7);
76
BOOST_CHECK_EQUAL
(
rw
.count(), 18);
77
78
// Skip every entry in the buffer.
79
rw
.add(
t
+ 13
s
, 8);
80
BOOST_CHECK_EQUAL
(
rw
.count(), 8);
81
}
82
83
BOOST_AUTO_TEST_SUITE_END
()
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(v.size(), 10U)
RateLimit.hpp
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(RateLimitCase)
Definition
RateLimit.ut.cpp:27
String.hpp
toolbox::RateLimit
Definition
RateLimit.hpp:28
toolbox::RateWindow
RateWindow maintains a sliding window of second time buckets for the specified interval.
Definition
RateLimit.hpp:60
std
STL namespace.
toolbox::util::to_string
std::string to_string(ValueT &&val)
Definition
String.hpp:54
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
Generated by
1.9.8