Merge "init/epoll_test: Improve this test"

This commit is contained in:
Treehugger Robot 2022-10-18 21:08:00 +00:00 committed by Gerrit Code Review
commit 6314ba7f76

View file

@ -21,6 +21,7 @@
#include <unordered_set> #include <unordered_set>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/logging.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace android { namespace android {
@ -30,14 +31,10 @@ std::unordered_set<void*> sValidObjects;
class CatchDtor final { class CatchDtor final {
public: public:
CatchDtor() { sValidObjects.emplace(this); } CatchDtor() { CHECK(sValidObjects.emplace(this).second); }
CatchDtor(const CatchDtor&) { sValidObjects.emplace(this); } CatchDtor(const CatchDtor&) { CHECK(sValidObjects.emplace(this).second); }
~CatchDtor() { CatchDtor(const CatchDtor&&) { CHECK(sValidObjects.emplace(this).second); }
auto iter = sValidObjects.find(this); ~CatchDtor() { CHECK_EQ(sValidObjects.erase(this), size_t{1}); }
if (iter != sValidObjects.end()) {
sValidObjects.erase(iter);
}
}
}; };
TEST(epoll, UnregisterHandler) { TEST(epoll, UnregisterHandler) {
@ -48,11 +45,13 @@ TEST(epoll, UnregisterHandler) {
ASSERT_EQ(pipe(fds), 0); ASSERT_EQ(pipe(fds), 0);
CatchDtor catch_dtor; CatchDtor catch_dtor;
bool handler_invoked; bool handler_invoked = false;
auto handler = [&, catch_dtor]() -> void { auto handler = [&, catch_dtor]() -> void {
auto result = epoll.UnregisterHandler(fds[0]); auto result = epoll.UnregisterHandler(fds[0]);
ASSERT_EQ(result.ok(), !handler_invoked); ASSERT_EQ(result.ok(), !handler_invoked);
handler_invoked = true; 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()); ASSERT_NE(sValidObjects.find((void*)&catch_dtor), sValidObjects.end());
}; };