[Impeller] Hold the CommandPoolVK at a higher scope. (flutter/engine#46013)

Fixes https://github.com/flutter/flutter/issues/134982.

I could never reproduce the flakes locally, but this should prevent `pool1 == pool2`.
This commit is contained in:
Matan Lurey 2023-09-18 18:25:25 -07:00 committed by GitHub
parent ac6eb5de3d
commit 2b4d2616aa

View File

@ -4,6 +4,7 @@
#include "flutter/testing/testing.h" // IWYU pragma: keep.
#include "fml/synchronization/waitable_event.h"
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
#include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
@ -13,26 +14,28 @@ namespace testing {
TEST(CommandPoolRecyclerVKTest, GetsACommandPoolPerThread) {
auto const context = MockVulkanContextBuilder().Build();
// Record the memory location of each pointer to a command pool.
int* pool1 = nullptr;
int* pool2 = nullptr;
{
// Record the memory location of each pointer to a command pool.
//
// These pools have to be held at this context, otherwise they will be
// dropped and recycled and potentially reused by another thread, causing
// flaky tests.
std::shared_ptr<CommandPoolVK> pool1;
std::shared_ptr<CommandPoolVK> pool2;
// Create a command pool in two threads and record the memory location.
std::thread thread1([&]() {
auto const pool = context->GetCommandPoolRecycler()->Get();
pool1 = reinterpret_cast<int*>(pool.get());
});
// Create a command pool in two threads and record the memory location.
std::thread thread1(
[&]() { pool1 = context->GetCommandPoolRecycler()->Get(); });
std::thread thread2([&]() {
auto const pool = context->GetCommandPoolRecycler()->Get();
pool2 = reinterpret_cast<int*>(pool.get());
});
std::thread thread2(
[&]() { pool2 = context->GetCommandPoolRecycler()->Get(); });
thread1.join();
thread2.join();
thread1.join();
thread2.join();
// The two command pools should be different.
EXPECT_NE(pool1, pool2);
// The two command pools should be different.
EXPECT_NE(pool1, pool2);
}
context->Shutdown();
}