Use fml::ScopedCleanupClosure instead of DeathRattle. (flutter/engine#51834)

Closes https://github.com/flutter/flutter/issues/146105.

Originally when we authored these suites, `ScopedCleanupClosure` disallowed move-semantics, but that was fixed in https://github.com/flutter/engine/pull/45772, so there is no reason to have a copy of these in different tests.

/cc @jonahwilliams
This commit is contained in:
Matan Lurey
2024-04-02 15:33:48 -07:00
committed by GitHub
parent bf68661eba
commit 5084d0b432
2 changed files with 14 additions and 53 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/testing/testing.h" // IWYU pragma: keep.
#include "fml/closure.h"
#include "fml/synchronization/waitable_event.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
@@ -23,27 +24,6 @@ TEST(DescriptorPoolRecyclerVKTest, GetDescriptorPoolRecyclerCreatesNewPools) {
context->Shutdown();
}
namespace {
// Invokes the provided callback when the destructor is called.
//
// Can be moved, but not copied.
class DeathRattle final {
public:
explicit DeathRattle(std::function<void()> callback)
: callback_(std::move(callback)) {}
DeathRattle(DeathRattle&&) = default;
DeathRattle& operator=(DeathRattle&&) = default;
~DeathRattle() { callback_(); }
private:
std::function<void()> callback_;
};
} // namespace
TEST(DescriptorPoolRecyclerVKTest, ReclaimMakesDescriptorPoolAvailable) {
auto const context = MockVulkanContextBuilder().Build();
@@ -64,10 +44,10 @@ TEST(DescriptorPoolRecyclerVKTest, ReclaimMakesDescriptorPoolAvailable) {
// destroyed. That should give us a non-flaky signal that the pool has been
// reclaimed as well.
auto waiter = fml::AutoResetWaitableEvent();
auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
auto rattle = fml::ScopedCleanupClosure([&waiter]() { waiter.Signal(); });
{
UniqueResourceVKT<DeathRattle> resource(context->GetResourceManager(),
std::move(rattle));
UniqueResourceVKT<fml::ScopedCleanupClosure> resource(
context->GetResourceManager(), std::move(rattle));
}
waiter.Wait();
}
@@ -98,10 +78,10 @@ TEST(DescriptorPoolRecyclerVKTest, ReclaimDropsDescriptorPoolIfSizeIsExceeded) {
// See note above.
for (auto i = 0u; i < 2; i++) {
auto waiter = fml::AutoResetWaitableEvent();
auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
auto rattle = fml::ScopedCleanupClosure([&waiter]() { waiter.Signal(); });
{
UniqueResourceVKT<DeathRattle> resource(context->GetResourceManager(),
std::move(rattle));
UniqueResourceVKT<fml::ScopedCleanupClosure> resource(
context->GetResourceManager(), std::move(rattle));
}
waiter.Wait();
}
@@ -126,10 +106,10 @@ TEST(DescriptorPoolRecyclerVKTest, ReclaimDropsDescriptorPoolIfSizeIsExceeded) {
for (auto i = 0u; i < 2; i++) {
auto waiter = fml::AutoResetWaitableEvent();
auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
auto rattle = fml::ScopedCleanupClosure([&waiter]() { waiter.Signal(); });
{
UniqueResourceVKT<DeathRattle> resource(context->GetResourceManager(),
std::move(rattle));
UniqueResourceVKT<fml::ScopedCleanupClosure> resource(
context->GetResourceManager(), std::move(rattle));
}
waiter.Wait();
}

View File

@@ -6,6 +6,7 @@
#include <functional>
#include <memory>
#include <utility>
#include "fml/closure.h"
#include "fml/synchronization/waitable_event.h"
#include "gtest/gtest.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
@@ -20,39 +21,19 @@ TEST(ResourceManagerVKTest, CreatesANewInstance) {
EXPECT_NE(a, b);
}
namespace {
// Invokes the provided callback when the destructor is called.
//
// Can be moved, but not copied.
class DeathRattle final {
public:
explicit DeathRattle(std::function<void()> callback)
: callback_(std::move(callback)) {}
DeathRattle(DeathRattle&&) = default;
DeathRattle& operator=(DeathRattle&&) = default;
~DeathRattle() { callback_(); }
private:
std::function<void()> callback_;
};
} // namespace
TEST(ResourceManagerVKTest, ReclaimMovesAResourceAndDestroysIt) {
auto const manager = ResourceManagerVK::Create();
auto waiter = fml::AutoResetWaitableEvent();
auto dead = false;
auto rattle = DeathRattle([&waiter]() { waiter.Signal(); });
auto rattle = fml::ScopedCleanupClosure([&waiter]() { waiter.Signal(); });
// Not killed immediately.
EXPECT_FALSE(waiter.IsSignaledForTest());
{
auto resource = UniqueResourceVKT<DeathRattle>(manager, std::move(rattle));
auto resource = UniqueResourceVKT<fml::ScopedCleanupClosure>(
manager, std::move(rattle));
}
waiter.Wait();