File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed
Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change 77#include " rate_limit.hpp"
88
99#include < chrono>
10+ #include < iostream>
1011
1112using std::chrono::duration_cast;
1213using std::chrono::microseconds;
@@ -32,25 +33,42 @@ bool rate_limiter::allow()
3233
3334 std::lock_guard<std::mutex> const lock (mtx_);
3435
36+ std::cout << " START\n ----------\n " ;
3537 if (now_s != index_) {
38+ std::cout << " New second\n " ;
3639 if (index_ == now_s - 1 ) {
40+ std::cout << " Following second\n " ;
3741 precounter_ = counter_;
3842 } else {
43+ std::cout << " Not following second\n " ;
3944 precounter_ = 0 ;
4045 }
4146 counter_ = 0 ;
4247 index_ = now_s;
48+ } else {
49+ std::cout << " Same second\n " ;
4350 }
4451
52+ std::cout << " max_per_second_ is " << max_per_second_ << std::endl;
53+ ;
54+ std::cout << " counter_ is " << counter_ << std::endl;
55+ ;
56+ std::cout << " precounter_ is " << precounter_ << std::endl;
57+ ;
58+ std::cout << " index_ is " << index_ << std::endl;
59+ ;
60+
4561 constexpr uint64_t mil = 1000 ;
4662 uint32_t const count =
4763 (precounter_ * (mil - (now_ms % mil))) / mil + counter_;
4864
4965 if (count >= max_per_second_) {
66+ std::cout << " Not allowed\n " ;
5067 return false ;
5168 }
5269
5370 counter_++;
71+ std::cout << " Allowed\n " ;
5472
5573 return true ;
5674}
Original file line number Diff line number Diff line change @@ -47,27 +47,33 @@ class rate_limiter : public dds::rate_limiter {
4747
4848TEST (RateLimitTest, OnlyAllowedMaxPerSecond)
4949{
50+ auto first_round_time = system_clock::now ();
51+ auto second_round_time = first_round_time + std::chrono::seconds (5 );
52+
53+ std::cout << " first_round_time is " << first_round_time.time_since_epoch ().count () << std::endl;
54+ std::cout << " second_round_time is " << second_round_time.time_since_epoch ().count () << std::endl;
55+
5056 std::unique_ptr<mock::timer> timer = std::make_unique<mock::timer>();
5157 // Four calls within the same second
52- timer->responses .push (system_clock::duration ( 1708963615 ));
58+ timer->responses .push (first_round_time. time_since_epoch ( ));
5359
5460 mock::rate_limiter rate_limiter (2 );
5561 rate_limiter.set_timer (std::move (timer));
5662
5763 int allowed = 0 ;
58- for (int i = 0 ; i < 100 ; i++) {
64+ for (int i = 0 ; i < 10 ; i++) {
5965 if (rate_limiter.allow ()) {
6066 allowed++;
6167 }
6268 }
6369 EXPECT_EQ (2 , allowed);
6470
6571 timer = std::make_unique<mock::timer>();
66- timer->responses .push (system_clock::duration ( 1709963630 ));
72+ timer->responses .push (second_round_time. time_since_epoch ( ));
6773 rate_limiter.set_timer (std::move (timer));
6874
6975 allowed = 0 ;
70- for (int i = 0 ; i < 100 ; i++) {
76+ for (int i = 0 ; i < 10 ; i++) {
7177 if (rate_limiter.allow ()) {
7278 allowed++;
7379 }
You can’t perform that action at this time.
0 commit comments