Skip to content

Commit

Permalink
Fix test timeout for SingletonThreadLocalTest.OneSingletonPerThread
Browse files Browse the repository at this point in the history
Summary:
Use a latch to keep threads alive rather than an atomic spinlock. Threads need to be kept alive so that SingletonThreadLocal does not re-use objects across threads.

On local dev machine test is now taking ~1.5 seconds, as opposed to ~5.5 seconds.

The test run has been flaky because of timeouts. The atomic spinlock is my best guess as to why it would time out.

Reviewed By: yfeldblum

Differential Revision: D57073363

fbshipit-source-id: 65d9130e7f8d4209df1e0678e99a1fc8a4816778
  • Loading branch information
skrueger authored and facebook-github-bot committed May 8, 2024
1 parent 990a29b commit ec5c986
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions folly/test/SingletonThreadLocalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <dlfcn.h>
#endif

#include <latch>
#include <thread>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -59,15 +60,16 @@ TEST(SingletonThreadLocalTest, TryGet) {

TEST(SingletonThreadLocalTest, OneSingletonPerThread) {
static constexpr std::size_t targetThreadCount{64};
std::atomic<std::size_t> completedThreadCount{0};
std::latch allSingletonsCreated(targetThreadCount);
Synchronized<std::unordered_set<Foo*>> fooAddresses{};
std::vector<std::thread> threads{};
auto threadFunction = [&fooAddresses, &completedThreadCount] {
auto threadFunction = [&fooAddresses, &allSingletonsCreated] {
fooAddresses.wlock()->emplace(&FooSingletonTL::get());
++completedThreadCount;
while (completedThreadCount < targetThreadCount) {
std::this_thread::yield();
}
// Prevent SingletonThreadLocal's internal cache from re-using
// objects across threads by
// keeping each thread alive until all the threads have a
// SingletonThreadLocal.
allSingletonsCreated.arrive_and_wait();
};
{
for (std::size_t threadCount{0}; threadCount < targetThreadCount;
Expand Down

0 comments on commit ec5c986

Please sign in to comment.