From 20954a8e3400568ec8e91a865eb065d690a1cbe0 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 17 Oct 2022 11:07:05 -0700 Subject: [PATCH] init/epoll_test: Improve this test Add a move constructor in the CatchDtor class. Check the .emplace() result. Simplify the destructor. Initialize handler_invoked. Explain the purpose of this test. Bug: 213617178 Change-Id: I4d6f97dbb2705a2f2dd78e449ae8de74e90b102f Signed-off-by: Bart Van Assche --- init/epoll_test.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/init/epoll_test.cpp b/init/epoll_test.cpp index 9236cd53e..3f8b5a4c6 100644 --- a/init/epoll_test.cpp +++ b/init/epoll_test.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace android { @@ -30,14 +31,10 @@ std::unordered_set sValidObjects; class CatchDtor final { public: - CatchDtor() { sValidObjects.emplace(this); } - CatchDtor(const CatchDtor&) { sValidObjects.emplace(this); } - ~CatchDtor() { - auto iter = sValidObjects.find(this); - if (iter != sValidObjects.end()) { - sValidObjects.erase(iter); - } - } + CatchDtor() { CHECK(sValidObjects.emplace(this).second); } + CatchDtor(const CatchDtor&) { CHECK(sValidObjects.emplace(this).second); } + CatchDtor(const CatchDtor&&) { CHECK(sValidObjects.emplace(this).second); } + ~CatchDtor() { CHECK_EQ(sValidObjects.erase(this), size_t{1}); } }; TEST(epoll, UnregisterHandler) { @@ -48,11 +45,13 @@ TEST(epoll, UnregisterHandler) { ASSERT_EQ(pipe(fds), 0); CatchDtor catch_dtor; - bool handler_invoked; + bool handler_invoked = false; auto handler = [&, catch_dtor]() -> void { auto result = epoll.UnregisterHandler(fds[0]); ASSERT_EQ(result.ok(), !handler_invoked); handler_invoked = true; + // The assert statement below verifies that the UnregisterHandler() call + // above did not destroy the current std::function<> instance. ASSERT_NE(sValidObjects.find((void*)&catch_dtor), sValidObjects.end()); };