Commit 12e65c2d authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Ensure sleep duration in logging RateLimiter test

Summary: [Folly] Ensure sleep duration in logging `RateLimiter` test to make sure that there is no chance of waking up from the sleep too early.

Reviewed By: Orvid

Differential Revision: D8330018

fbshipit-source-id: f1222e1da50f8360267f9e1c5e0f24897b7b8f47
parent 51196c8d
...@@ -13,21 +13,22 @@ ...@@ -13,21 +13,22 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <chrono>
#include <string>
#include <thread> #include <thread>
#include <folly/Chrono.h>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/logging/RateLimiter.h> #include <folly/logging/RateLimiter.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using folly::chrono::coarse_steady_clock;
using folly::logging::IntervalRateLimiter; using folly::logging::IntervalRateLimiter;
using std::chrono::duration_cast; using std::chrono::duration_cast;
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
void intervalTest( using irl_clock = IntervalRateLimiter::clock;
uint64_t eventsPerInterval,
coarse_steady_clock::duration interval) { void intervalTest(uint64_t eventsPerInterval, irl_clock::duration interval) {
SCOPED_TRACE(folly::to<std::string>( SCOPED_TRACE(folly::to<std::string>(
eventsPerInterval, eventsPerInterval,
" events every ", " events every ",
...@@ -36,8 +37,13 @@ void intervalTest( ...@@ -36,8 +37,13 @@ void intervalTest(
IntervalRateLimiter limiter{eventsPerInterval, interval}; IntervalRateLimiter limiter{eventsPerInterval, interval};
for (int iter = 0; iter < 4; ++iter) { for (int iter = 0; iter < 4; ++iter) {
if (iter != 0) { if (iter != 0) {
/* sleep override */ auto now = irl_clock::now();
std::this_thread::sleep_for(interval); auto const deadline = now + interval;
while (now < deadline) {
/* sleep override */
std::this_thread::sleep_for(now - deadline);
now = irl_clock::now();
}
} }
for (uint64_t n = 0; n < eventsPerInterval * 2; ++n) { for (uint64_t n = 0; n < eventsPerInterval * 2; ++n) {
if (n < eventsPerInterval) { if (n < eventsPerInterval) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment