2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
9 #include "utils/ThreadPool.h"
11 #include <gtest/gtest.h>
17 using namespace pzstd;
19 TEST(ThreadPool, Ordering) {
20 std::vector<int> results;
23 ThreadPool executor(1);
24 for (int i = 0; i < 10; ++i) {
25 executor.add([ &results, i ] { results.push_back(i); });
29 for (int i = 0; i < 10; ++i) {
30 EXPECT_EQ(i, results[i]);
34 TEST(ThreadPool, AllJobsFinished) {
35 std::atomic<unsigned> numFinished{0};
36 std::atomic<bool> start{false};
38 std::cerr << "Creating executor" << std::endl;
39 ThreadPool executor(5);
40 for (int i = 0; i < 10; ++i) {
41 executor.add([ &numFinished, &start ] {
42 while (!start.load()) {
43 std::this_thread::yield();
48 std::cerr << "Starting" << std::endl;
50 std::cerr << "Finishing" << std::endl;
52 EXPECT_EQ(10, numFinished.load());
55 TEST(ThreadPool, AddJobWhileJoining) {
56 std::atomic<bool> done{false};
58 ThreadPool executor(1);
59 executor.add([&executor, &done] {
60 while (!done.load()) {
61 std::this_thread::yield();
63 // Sleep for a second to be sure that we are joining
64 std::this_thread::sleep_for(std::chrono::seconds(1));