#include <folly/Conv.h>
#include <folly/Format.h>
+#include <folly/Optional.h>
#include <folly/String.h>
#include <glog/logging.h>
AutoTimer& operator=(AutoTimer&&) = default;
~AutoTimer() {
- log(destructionMessage_);
+ if (destructionMessage_) {
+ log(destructionMessage_.value());
+ }
}
DoubleSeconds log(StringPiece msg = "") {
return duration;
}
- const std::string destructionMessage_;
+ Optional<std::string> destructionMessage_;
std::chrono::time_point<Clock> start_ = Clock::now();
DoubleSeconds minTimeToLog_;
Logger logger_;
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <gtest/gtest.h>
+#include <gtest/gtest.h>
#include <folly/experimental/AutoTimer.h>
using namespace folly;
ASSERT_EQ(std::chrono::duration<double>(2), timer.log("foo"));
ASSERT_EQ(std::chrono::duration<double>::zero().count(), StubLogger::t);
}
+
+TEST(TestAutoTimer, MovedObjectDestructionDoesntLog) {
+ const std::vector<std::string> expectedMsgs = {
+ "BEFORE_MOVE", "AFTER_MOVE", "END"};
+ int32_t current = 0;
+ SCOPE_EXIT {
+ EXPECT_EQ(3, current);
+ };
+
+ auto timer = [&expectedMsgs, ¤t] {
+ auto oldTimer = folly::makeAutoTimer(
+ "END",
+ std::chrono::duration<double>::zero(),
+ [&expectedMsgs, ¤t](
+ StringPiece msg, const std::chrono::duration<double>&) {
+ EXPECT_EQ(expectedMsgs.at(current), msg);
+ current++;
+ });
+ oldTimer.log("BEFORE_MOVE");
+ auto newTimer = std::move(oldTimer); // force the move-ctor
+ return newTimer;
+ }();
+ timer.log("AFTER_MOVE");
+}