FB_LOG_EVERY_MS should use wall time instead of cpu time
[folly.git] / folly / test / LoggingTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
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
17 #include <folly/Logging.h>
18 #include <gflags/gflags.h>
19 #include <gtest/gtest.h>
20 #include <vector>
21
22 TEST(LogEveryMs, basic) {
23   std::vector<std::chrono::steady_clock::time_point> hist;
24
25   while (hist.size() < 10) {
26     FB_LOG_EVERY_MS(INFO, 10)
27       << "test msg " << (hist.push_back(std::chrono::steady_clock::now()),
28                          hist.size());
29   }
30
31   bool atLeastOneIsGood = false;
32   for (int i = 0; i < hist.size() - 1; ++i) {
33     auto delta = hist[i + 1] - hist[i];
34     if (delta > std::chrono::milliseconds(5) &&
35         delta < std::chrono::milliseconds(15)) {
36       atLeastOneIsGood = true;
37     }
38   }
39   EXPECT_TRUE(atLeastOneIsGood);
40 }
41
42 int main(int argc, char** argv) {
43   testing::InitGoogleTest(&argc, argv);
44   google::ParseCommandLineFlags(&argc, &argv, true);
45   return RUN_ALL_TESTS();
46 }