44
55#include " flutter/testing/testing.h" // IWYU pragma: keep.
66#include " fml/synchronization/waitable_event.h"
7+ #include " impeller/renderer/backend/vulkan/command_pool_vk.h"
78#include " impeller/renderer/backend/vulkan/resource_manager_vk.h"
89#include " impeller/renderer/backend/vulkan/test/mock_vulkan.h"
910
@@ -13,26 +14,28 @@ namespace testing {
1314TEST (CommandPoolRecyclerVKTest, GetsACommandPoolPerThread) {
1415 auto const context = MockVulkanContextBuilder ().Build ();
1516
16- // Record the memory location of each pointer to a command pool.
17- int * pool1 = nullptr ;
18- int * pool2 = nullptr ;
19-
20- // Create a command pool in two threads and record the memory location.
21- std::thread thread1 ([&]() {
22- auto const pool = context->GetCommandPoolRecycler ()->Get ();
23- pool1 = reinterpret_cast <int *>(pool.get ());
24- });
25-
26- std::thread thread2 ([&]() {
27- auto const pool = context->GetCommandPoolRecycler ()->Get ();
28- pool2 = reinterpret_cast <int *>(pool.get ());
29- });
30-
31- thread1.join ();
32- thread2.join ();
33-
34- // The two command pools should be different.
35- EXPECT_NE (pool1, pool2);
17+ {
18+ // Record the memory location of each pointer to a command pool.
19+ //
20+ // These pools have to be held at this context, otherwise they will be
21+ // dropped and recycled and potentially reused by another thread, causing
22+ // flaky tests.
23+ std::shared_ptr<CommandPoolVK> pool1;
24+ std::shared_ptr<CommandPoolVK> pool2;
25+
26+ // Create a command pool in two threads and record the memory location.
27+ std::thread thread1 (
28+ [&]() { pool1 = context->GetCommandPoolRecycler ()->Get (); });
29+
30+ std::thread thread2 (
31+ [&]() { pool2 = context->GetCommandPoolRecycler ()->Get (); });
32+
33+ thread1.join ();
34+ thread2.join ();
35+
36+ // The two command pools should be different.
37+ EXPECT_NE (pool1, pool2);
38+ }
3639
3740 context->Shutdown ();
3841}
0 commit comments