]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/pzstd/utils/test/ThreadPoolTest.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / zstd / contrib / pzstd / utils / test / ThreadPoolTest.cpp
1 /**
2 * Copyright (c) 2016-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9 #include "utils/ThreadPool.h"
10
11 #include <gtest/gtest.h>
12 #include <atomic>
13 #include <thread>
14 #include <vector>
15
16 using namespace pzstd;
17
18 TEST(ThreadPool, Ordering) {
19 std::vector<int> results;
20
21 {
22 ThreadPool executor(1);
23 for (int i = 0; i < 10; ++i) {
24 executor.add([ &results, i ] { results.push_back(i); });
25 }
26 }
27
28 for (int i = 0; i < 10; ++i) {
29 EXPECT_EQ(i, results[i]);
30 }
31 }
32
33 TEST(ThreadPool, AllJobsFinished) {
34 std::atomic<unsigned> numFinished{0};
35 std::atomic<bool> start{false};
36 {
37 ThreadPool executor(5);
38 for (int i = 0; i < 10; ++i) {
39 executor.add([ &numFinished, &start ] {
40 while (!start.load()) {
41 // spin
42 }
43 ++numFinished;
44 });
45 }
46 start.store(true);
47 }
48 EXPECT_EQ(10, numFinished.load());
49 }
50
51 TEST(ThreadPool, AddJobWhileJoining) {
52 std::atomic<bool> done{false};
53 {
54 ThreadPool executor(1);
55 executor.add([&executor, &done] {
56 while (!done.load()) {
57 std::this_thread::yield();
58 }
59 // Sleep for a second to be sure that we are joining
60 std::this_thread::sleep_for(std::chrono::seconds(1));
61 executor.add([] {
62 EXPECT_TRUE(false);
63 });
64 });
65 done.store(true);
66 }
67 }