]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/pzstd/utils/test/ThreadPoolTest.cpp
update sources to ceph Nautilus 14.2.1
[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 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).
8 */
9 #include "utils/ThreadPool.h"
10
11 #include <gtest/gtest.h>
12 #include <atomic>
13 #include <iostream>
14 #include <thread>
15 #include <vector>
16
17 using namespace pzstd;
18
19 TEST(ThreadPool, Ordering) {
20 std::vector<int> results;
21
22 {
23 ThreadPool executor(1);
24 for (int i = 0; i < 10; ++i) {
25 executor.add([ &results, i ] { results.push_back(i); });
26 }
27 }
28
29 for (int i = 0; i < 10; ++i) {
30 EXPECT_EQ(i, results[i]);
31 }
32 }
33
34 TEST(ThreadPool, AllJobsFinished) {
35 std::atomic<unsigned> numFinished{0};
36 std::atomic<bool> start{false};
37 {
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();
44 }
45 ++numFinished;
46 });
47 }
48 std::cerr << "Starting" << std::endl;
49 start.store(true);
50 std::cerr << "Finishing" << std::endl;
51 }
52 EXPECT_EQ(10, numFinished.load());
53 }
54
55 TEST(ThreadPool, AddJobWhileJoining) {
56 std::atomic<bool> done{false};
57 {
58 ThreadPool executor(1);
59 executor.add([&executor, &done] {
60 while (!done.load()) {
61 std::this_thread::yield();
62 }
63 // Sleep for a second to be sure that we are joining
64 std::this_thread::sleep_for(std::chrono::seconds(1));
65 executor.add([] {
66 EXPECT_TRUE(false);
67 });
68 });
69 done.store(true);
70 }
71 }